1. 程式人生 > >連結串列實現棧的基本操作

連結串列實現棧的基本操作

初始化:

void InitStack(LinkStack &s) {     s = NULL; }

進棧:

void Push(LinkStack &s,ElemType e) {     LinkStack p;     p = (LinkStack)malloc(sizeof(StackNode));     p->data = e;     p->next = s;     s = p;//check the TopPointer }

出棧:

ElemType Pop(LinkStack &s,ElemType &e)//the element will be save in e to return {     LinkStack p;     if(s==NULL)     {         return 0;     }     else     {         p = s;         e = s->data;         s = p->next;         free (p);         return 1;     } }

取棧頂:

ElemType GetTop(LinkStack s,ElemType &e){     if(s==NULL)     {         return 0;     }     else{         e = s->data;         return 1;     } } 完整程式碼:

#include <iostream>
#include <cstdlib>
using namespace std;
typedef int ElemType;
typedef struct StackNode
{
    ElemType data;
    struct StackNode *next;
}StackNode,*LinkStack;
//Initialize the LinkStack
void InitStack(LinkStack &s)
{
    s = NULL;
}
//Push
void Push(LinkStack &s,ElemType e)
{
    LinkStack p;
    p = (LinkStack)malloc(sizeof(StackNode));
    p->data = e;
    p->next = s;
    s = p;//check the TopPointer
}
//Pop
ElemType Pop(LinkStack &s,ElemType &e)//the element will be save in e to return
{
    LinkStack p;
    if(s==NULL)
    {
        return 0;
    }
    else
    {
        p = s;
        e = s->data;
        s = p->next;
        free (p);
        return 1;
    }
}
void Info(){
    cout<<"1:Push"<<endl;
    cout<<"2:Pop"<<endl;
    cout<<"3:GetTop"<<endl;
    cout<<"0:quit"<<endl;
}
//Get TopElem
ElemType GetTop(LinkStack s,ElemType &e){
    if(s==NULL)
    {
        return 0;
    }
    else{
        e = s->data;
        return 1;
    }
}

//StackEmpty
ElemType StackEmpty(LinkStack s)
{
    if(s==NULL)
    {
        return 1;
    }
    else
        return 0;
}
void StackDisplay(LinkStack s){
    cout<<"此時棧的狀態"<<endl;
    while(s!=NULL){
        cout<<s->data<<" ";
        s = s->next;
    }
    cout<<endl;
}
int main()
{
    LinkStack s;
    ElemType e;
    ElemType StLength;
    int choose;
    cout<<"您想進棧幾個元素:"<<endl;
    cin>>StLength;
    InitStack(s);
    cout<<"請輸入那些元素"<<endl;
    for(int i = 1;i <= StLength;i++){
        cin>>e;
        Push(s,e);
    }
////    cout<<"此時棧的狀態"<<endl;
////    while(s!=NULL){
////        cout<<s->data<<" ";
////        s = s->next;
////    }
    StackDisplay(s);
    Info();
    cin>>choose;
    while(choose)
    {
        switch(choose)
        {
            case 1:{
                cout<<"請輸入您要進棧的資料"<<endl;
                cin>>e;
                Push(s,e);
                StackDisplay(s);
                break;
            }
            case 2:{
                cout<<"您將要彈出一個元素的值是:"<<endl;
                Pop(s,e);
                cout<<e<<endl;
                StackDisplay(s);
                break;
            }
            case 3:{
                GetTop(s,e);
                cout<<"棧頂元素是:"<<e<<endl;
                StackDisplay(s);
                break;
            }
            default : break;
        }
        Info();
        cin>>choose;
    }
    return 0;
}