Refer to the "Big talk data Structure" p98~99--stack of chain storage structure.
Into the stack:
Out Stack:
To give a simple example:
The code and explanation are as follows (VS2012 test pass):
1#include <iostream>2#include <string>3 using namespacestd;4 5typedefstringStatus//If the status returned by the book is successful, the template class string in C + + is more convenient than the character array char[]6 7 //node of the stack8 //contains data, and a pointer to the next node next9typedefstructStacknodeTen { One Chardata; A structStacknode *Next; - }stacknode; - the //chained storage structure for stacks - //contains the pointer top to the top of the stack, and the number of elements in the stack count - //top points to the latest node in the stack, and the top point is the first element out of the stack. -typedefstructLinkstack + { -Stacknode *top; + intcount; A }linkstack; at - //request memory, initialize - //top points to null, which indicates initialization to empty stack -Linkstack *initlinkstack (Linkstack *s) - { -s=NewLinkstack; ins->top=NULL; -S->count=0; to returns; + } - the //into the stack *Status Push (Linkstack *s,Chare) $ {Panax NotoginsengStacknode *sn=NewStacknode;//Request a node space, define and initialize a pointer to the node SN -sn->data=e;//assigns the elements of the stack to the data of the new node thesn->next=s->top;//point the new node next to the previous node . +s->top=sn;//point the top pointer to the new node, and the top element of the stack is the newly-loaded element As->count++;//the number of elements in the stack plus 1 the return "Push OK";//return to Stack succeeded + } - $ //out of the stack $Status Pop (Linkstack *s,Char*e) - { -Stacknode *p;//defines a temporary node pointer. the if(s->top==NULL) - return "Pop Error";//if it is an empty stack, it fails to return directly to the stackWuyip=s->top;//assign the top pointer to the temporary pointer p the*e=p->data;//assigns the top element of the stack to the variable that the e points to, that is, the variable that needs to be modified in the keynote function - if(P->next==null)//if the last element is out of the stack Wus->top=null;//the stack is empty after the element is out of the stack - Else Abouts->top=s->top->next;//Otherwise top points to the next node of the stack node $ //this if else can also be written directly to s->top=s->top->next; because if the stack is the last element, Top->next would have been equal to null - Free(p);//releases the space that the temporary pointer p points to -s->count--;//stack of elements minus 1 - return "Pop OK";//returns the success of the stack A } + the intMain () - { $Linkstack *p=NULL; thep=Initlinkstack (p); the the //into the stack thecout<<"A"<<" "<<push (P,'A') <<" ";//into Stack a -cout<<p->count<<endl;//There are 1 elements after you enter stack a incout<<"B"<<" "<<push (P,'B') <<" ";//into Stack b thecout<<p->count<<endl;//There are 2 elements after you enter stack b the About //out of the stack the Chare; the Char*pe=&e; theCout<<pop (P,PE) <<" ";//out of Stack b +cout<<e<<" "<<p->count<<endl;//There are 1 elements after the stack b -Cout<<pop (P,PE) <<" "; thecout<<e<<" "<<p->count<<endl;//There are 0 elements after the stack aBayiCout<<pop (P,PE) <<endl;//stack empty, out of stack failed the}
Operation Result:
Compare sequential stacks and chain stacks:
On time:
They are both stacked and out-of-stack in the same time complexity, all O (1).
In Space:
The sequential stack needs to be implemented to determine a fixed length, and there may be problems with wasted memory space. But the location is convenient when accessing.
The link stack requires that each element have a pointer field, which increases memory overhead, but is unrestricted for the length of the stack.
Therefore, if the use of the stack element changes unpredictable, it is best to use the chain stack. Conversely, if it changes within a controllable range, it is recommended to use a sequential stack.
The chain storage structure of the stack and the stack-in-stack operation