1#include <iostream>2 using namespacestd;3 4 structNode5 {6 intdata;7Node *Next;8 };9 Ten /*Initialize the link stack*/ One voidInitchainstack (Node *top) A { -top->next=0; - } the - /*push data into the stack elem*/ - intPush (Node *top,intelem) - { +Node *p= (node*)malloc(sizeof(Node)); - if(p==0) + return false; AP->data=Elem; atP->next = top->Next; -top->next=p; - return true; - } - - /*out of the stack and return the stack value with x*/ in intPop (Node *top,int&x) - { toNode *temp = top->Next; + if(temp==0) - return false; theTop->next = temp->Next; *X=temp->data; $ Free(temp);Panax Notoginseng return true; - } the + /*return stack top data*/ A intGetTop (Node * top,int&x) the { + if(top->next==0) - return false; $X=top->next->data; $ return true; - } - the /*determine if the stack is empty*/ - intIsEmpty (Node *top)Wuyi { the if(top->next==0) - return true; Wu return false; - } About $ /*print stack all data elements*/ - voidPrintstack (Node *top) - { -Node *temp = top->Next; A while(temp!=0) + { thecout << Temp->data <<" "; -Temp=temp->Next; $ } thecout <<Endl; the } the the voidMain () - { inNode *node=NewNode; the initchainstack (node); the About for(intI=0;i<Ten; i++) the Push (node,i); the intx=0; the Pop (node,x); + GetTop (node,x); -cout << x <<Endl; thecout << IsEmpty (node) <<Endl;Bayi printstack (node); the}
Basic operations for C + + chain stacks