Basic operations for C + + chain stacks

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.