1. Stack
1.1 the definition of the stack
Stacks are a kind of special linear table. The peculiarity is that the operation of inserting and deleting data elements can only be done at one end of the linear table. As shown below:
Conclusion: LIFO (last in first), abbreviated as the LIFO linear table.
The basic operation of the stack is six kinds:
Constructed empty stack: Initstack (S),
Stack empty: Stackempty (S),
Stack full: Stackfull (S),
Into the stack: Push (s,x), can be visualized as indentation, when the stack will be more than one element
Back stack: Pop (S), can be visualized as a pop-up, pop-up after the stack does not have this element.
Take the top element of the stack: Stacktop (S), different and pop-up, just use the value of the top element of the stack, the element is still on the top of the stack will not change.
Because the stack is also a linear table, the storage structure of the linear table is also applicable to the stack, usually the stack has a sequential stack and chain stacks two storage structures, the two storage structures, which makes the basic operation of the stack algorithm is also different.
What we want to know is that there are "overflow" and "underflow" concepts in the order stack. Sequential stacks like a box, we put a stack of books in it, and when we want to use the book, we can only start with the first one. (Will you turn the box over?) really smart ^ ^), then when we put the book in this stack over the top of the box is not put down (not counted, hem), then is "overflow", "overflow" That is, the top of the stack pointing to the outside of the stack, is obviously a mistake. On the contrary, when there is no book in the stack, we go to take, see no books, take the box to look at the bottom of the box, or not, this is "underflow." The underflow itself can indicate that the stack is an empty stack, so it can be used as a condition for controlling the transfer.
The chain stack has no overflow limit, it's like a fixed chain, you can freely increase the chain (node) at the end of the activity without overflow, the chain stack does not need to attach head nodes in the head, because the stack is operating in the head, if the addition of the head node, is equal to the node after the first node to operate, Instead of making the algorithm more complex, so long as there is a list of the head pointer can be.
1.2 sequential storage of stacks
Object-oriented encapsulation using C + +:
[CPP] View Plain copy print? Test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include < iostream> using namespace std; #define MAX 10 // maximum stack content class stack { private: int arr[MAX]; int top; public: stack () { initstack (); } /************************************************************************/ /* Initialization Stack */ /*********************************************************** / void initstack () { top=-1; } /****************************************************** / /* into Stack */ /***************************************** / void push (int a) { top++; if (Top < max) { arr[top] =a; } else { cout<< "stack full!! " <<top; } } /****************** / /* out Stack */ /************************************************************************/ &nBsp;int pop () { if (IsEmpty ()) { cout<< "stack is empty "; return NULL; } else { int data=arr[top]; arr[top]=NULL; top--; return data; } } /************************************************************************/ /* is empty */ /*********************************************** / bool isempty () { if (top == -1) return true;   else return false; } }; int main () { stack a; A.push (3); a.push (a); a.push (1); cout<< "Pop:" < <a.pop (); return 0; }
Test.cpp:Defines the entry point for the console application.
#include "stdafx.h" #include <iostream> using namespace std;
#define MAX//MAXIMUM stack CONTENT class Stack {private:int Arr[max];
int top;
Public:stack () {initstack (); }/************************************************************************//* initialization stack *//************************************************************************/void inIt
Stack () {top=-1; }/************************************************************************//* into stack *//************************************************************************/void push (
int a) {top++;
if (Top < MAX) {arr[top]=a; else {cout<< "STACK full!!"
<<top; }
}
/***************************************************************//////* out Stack//*******************************
/int Pop () {if (IsEmpty ()) {cout<< "STACK is EMPTY";
return NULL;
else {int data=arr[top];
Arr[top]=null;
top--;
return data; }/************************************************************************//* is empty
*/
/************************************************************************/
BOOL IsEmpty () {if (top = 1) return true;
else return false;
}
};
int main () {stack A;
A.push (3);
A.push (10);
A.push (1);
cout<< "Pop:" <<a.pop ();
return 0;
}
Conclusion: Because the stack inserts and deletes the operation to have its particularity, therefore uses the sequential storage structure to represent the stack does not have to insert deletes the data element to need to move the problem, but the stack capacity difficult to expand the weakness still not to get rid of.
1.3-Stack chain storage
If the number of elements in the stack changes to a larger range or does not know the number of stack elements, you should consider using a chain-type storage structure. The stacks that people will represent with chained storage structures are called "chain stacks." Chain stacks are usually represented by a single linked list of headless nodes. As shown in the figure:
The operation of Stack is a special case of linear table operation.
Simple in C implementation:
[CPP] View Plain copy print? Test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include "stdlib.h" #include < iostream> using namespace std; //macro definition #define true 1 #define FALSE 0 # define ok 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define STACKEMPTY -3 #define &NBSP;LT (a,b) ((a) < (b)) #define &NBSP;N&NBSP;=&NBSP;100&NBSP;&NBSP;&NBSP;&NBSP; typedef int status; typedef int elemtype; typedef struct lnode{ ElemType data; struct LNode *next; }lnode, * linklist; typedef struct stack{ linklist top; } stack; /****************************** / /* Interface: */ /************************************************************************/ Void InitStack ( stack &s); Void push (stack &s,elemtype e); Void Pop (STACK &s, elemtype *e); elemtype gettop (stack s,elemtype *e); int stackempty (stack s); /****************************************************** / /* */ /************************************************** / Void initstack (stack &s) { S.top=NULL; } /************************************************* / /* into stack */ /************************************* / Void push (stack &s,elemtype e) { LinkList p; p = (linklist ) malloc (sizeof (Lnode)); if (!p) exit (OVERFLOW); p->data = e; p->next = S.top; S.top = p; } /********* / /* out stack */ /*** / Void pop (stack &s, elemtype *e) { LinkList p; if (Stackempty (S)) exit (stackempty); *e = S.top->data; p = S.top; S.top = p->next; free (p); } /*****/ /* get stack top element content */ /************************************************************************/ ElemType GetTop (stack s, elemtype *e) { if (Stackempty (S)) exit (stackempty); *e = S.top->data; } /************************************************************************/ /* Judge whether Stack S is empty */ /********************************************************************** **/ int stackempty (stack s) { if ( S.top==null) return TRUE; return FALSE; }