链栈

    技术2022-05-11  18

    //以下是stack.htypedef int ElemType;struct LNode{    ElemType data;    LNode* next;};void InitStack(LNode*  &HS ){    HS=NULL;}void ClearStack(LNode*  &HS ){    LNode *cp,*np;    cp=HS;    while(cp!=NULL)     {        np=cp->next;        delete cp;        cp=np;    }    HS=NULL;}int StackEmpty(LNode* HS){    return HS==NULL;}ElemType Peek(LNode *HS){    if(HS==NULL){        cerr < <"Linked  stack is empty!"<<endl;        exit(1);    }  return HS- > data;}void Push(LNode*  &HS ,const ElemType& item){    LNode* newptr=new LNode;    if(newptr==NULL){        cerr < <"Memory  allocation failare!"<<endl;        exit(1);    }    newptr- > data=item;    newptr->next=HS;    HS=newptr;}ElemType Pop(LNode*  &HS ){    if(HS==NULL){        cerr < <"Linked  stack is empty!"<<endl;    exit(1);    }LNode* p =HS; HS =HS- > next;ElemType temp=p->data;delete p;return temp;}//以下是stack.cpp#include < iostream .h > //栈的简单应用,从键盘上输入一批整数,然后按照相反的次序打印出来#include < stdlib .h > #include"stack.h"#include < conio .h > void main(){    LNode *a;    InitStack(a);    int x;cout < <"输入整数,用-1 作为终止键盘输入"<<endl;    cin > >x;    while(x!=-1){        //假定用-1作为终止键盘输入的标志,输入的整数个数不能超过StackMaxSize所规定的值        Push(a,x);        cin>>x;    }    while(!StackEmpty(a))        cout < <Pop (a)<<" ";    cout<<endl;    getch();}/*http://f2.9612.org//vcpp/webinfo/WebInfoBata1.aspQQ群:34409541 讨论网页  34409326 讨论JAVA 已满 34408784 讨论VC++  34409699 讨论VC++  9143041 讨论MFC编程  10614204 讨论C#  10613030 讨论Win32编程  10613067 讨论游戏开发  18779860 讨论JAVA  */  

    最新回复(0)