第三章 栈和队列 —— 链栈的基本操作

链栈的基本操作

  • 清空链栈
  • 判断链栈是否为空
  • 求链栈长度
  • 获取栈顶元素
  • 入栈
  • 出栈
  • 显式链栈
    在这里插入图片描述

注意:
以下构建的链栈带有头结点。

代码如下:

#include <iostream>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define VOERFLOW -1
using namespace std;

typedef int ElemType;
typedef int Status;
//定义链栈结点

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LNode,*LinkStack;

//初始化
Status InitStack(LinkStack &s)
{
    s=(LNode*)malloc(sizeof(LNode));
    s->next=NULL;
    return OK;
}

//输入链栈(相当于前插法)
Status Pushstack(LinkStack &s)
{
    LinkStack p;
    int n,e;
    cout<<"请输入要入栈的元素个数:"<<endl;
    cin>>n;
    cout<<"请依次输入要入栈的元素:"<<endl;
    for (int i=0; i<n; i++)
    {
        cin>>e;
        p=(LNode*)malloc(sizeof(LNode));
        p->data=e;
        p->next=s->next;
        s->next=p;
    }
    cout<<"入栈成功"<<endl;
    return OK;
}

//输出链栈
Status DisplayStack(LinkStack s)
{
    LinkStack p;
    p=s->next;
    cout<<"链栈内元素为:";
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return OK;
}

//判断链栈是否为空
void EmptyStack(LinkStack s)
{
    if(s->next==NULL)
        cout<<"链栈为空"<<endl;
    else
        cout<<"链栈不为空"<<endl;
}

//清空链栈
Status ClearStack(LinkStack &s)
{
    LinkStack p,q;
    p=s->next;
    while(p!=NULL)
    {
        q=p;
        s->next=p->next;
        p=p->next;
        free(q);
    }
    cout<<"清空成功"<<endl;
    return OK;
}

//销毁链栈
Status DestroyStack(LinkStack &s)
{
     LinkStack p;
     while(s)
        {
            p = s;
            s = s->next;
            free(p);
        }
    return OK;
}

//求链栈长度
void StackLength(LinkStack s)
{
    int i;
    LinkStack p;
    p=s->next;
    i=0;
    while(p!=NULL)
    {
        i++;
        p=p->next;
    }
    cout<<"链栈长度为:"<<i<<endl;
}

//取栈顶元素
ElemType GetTopelem(LinkStack s)
{
    if(s->next==NULL)
    {
        cout<<"空栈无栈顶元素"<<endl;
        return ERROR;
    }
    else
    {
        cout<<"栈顶元素为:"<<s->next->data<<endl;
    }
    return OK;
}

//出栈
Status Pop(LinkStack &s)
{
    if(s->next == NULL)
    {
        cout<<"空栈无法出栈"<<endl;
        return ERROR;
    }
    int e;
    LinkStack p;
    e = s->next->data;
    p = s->next;
    s->next = p->next;
    free(p);
    cout<<"栈顶元素"<<e<<"成功出栈"<<endl;
}


void show_help()
{
    cout<<"******* Data Structure ******"<<endl;
    cout<<"1----清空链栈"<<endl;
    cout<<"2----判断链栈是否为空"<<endl;
    cout<<"3----求链栈长度"<<endl;
    cout<<"4----获取栈顶元素"<<endl;
    cout<<"5----入栈"<<endl;
    cout<<"6----出栈"<<endl;
    cout<<"7----显式链栈"<<endl;
    cout<<"     退出,输入0"<<endl;

}
int main()
{
    int operate_code;
    show_help();
    LinkStack s;
    InitStack(s);
    while(1)
    {
        cout<<"";
        cin>>operate_code;
        if(operate_code==1)
        {
            ClearStack(s);
        }
        else if (operate_code==2)
        {
            EmptyStack(s);
        }
        else if (operate_code==3)
        {
            StackLength(s);
        }
        else if (operate_code==4)
        {
            GetTopelem(s);
        }
        else if (operate_code==5)
        {
            Pushstack(s);
        }
        else if (operate_code==6)
        {
            Pop(s);
        }
        else if (operate_code==7)
        {
            DisplayStack(s);
        }
        else if (operate_code==0)
        {
            break;
        }
        else
        {
            cout<<"\n操作码错误!!!"<<endl;
            show_help();
        }
    }
    DestroyStack(s);
    return 0;
}

这个和链表几乎一模一样,就是采用了前插法的链表。

我们之前在链表中构造头结点是因为:有了头结点无论插入或删除的位置是第一个结点还是其他结点,算法步骤都相同。但是对于链栈来讲它只能在栈顶进行插入和删除的操作,所以这里我们就没必要构造头结点了。

修改的方法也很简单:
<1> 初始化的时候不创建结点
<2> 把s->next 改成 s

代码如下:

#include <iostream>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define VOERFLOW -1
using namespace std;

typedef int ElemType;
typedef int Status;
//定义链栈结点

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
} LNode,*LinkStack;

//初始化
Status InitStack(LinkStack &s)
{
    s = NULL;
    return OK;
}

//输入链栈(相当于前插法)
Status Pushstack(LinkStack &s)
{
    LinkStack p;
    int n,e;
    cout<<"请输入要入栈的元素个数:"<<endl;
    cin>>n;
    cout<<"请依次输入要入栈的元素:"<<endl;
    for (int i=0; i<n; i++)
    {
        cin>>e;
        p=(LNode*)malloc(sizeof(LNode));
        p->data=e;
        p->next=s;
        s=p;
    }
    cout<<"入栈成功"<<endl;
    return OK;
}

//输出链栈
Status DisplayStack(LinkStack s)
{
    LinkStack p;
    p=s;
    cout<<"链栈内元素为:";
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return OK;
}

//判断链栈是否为空
void EmptyStack(LinkStack s)
{
    if(s==NULL)
        cout<<"链栈为空"<<endl;
    else
        cout<<"链栈不为空"<<endl;
}

//清空链栈
Status ClearStack(LinkStack &s)
{
    LinkStack p,q;
    p=s;
    while(p!=NULL)
    {
        q=p;
        s=p->next;
        p=p->next;
        free(q);
    }
    cout<<"清空成功"<<endl;
    return OK;
}

//销毁链栈
Status DestroyStack(LinkStack &s)
{
     LinkStack p;
     while(s)
        {
            p = s;
            s = s->next;
            free(p);
        }
    return OK;
}

//求链栈长度
void StackLength(LinkStack s)
{
    int i;
    LinkStack p;
    p=s ;
    i=0;
    while(p!=NULL)
    {
        i++;
        p=p->next;
    }
    cout<<"链栈长度为:"<<i<<endl;
}

//取栈顶元素
ElemType GetTopelem(LinkStack s)
{
    if(s==NULL)
    {
        cout<<"空栈无栈顶元素"<<endl;
        return ERROR;
    }
    else
    {
        cout<<"栈顶元素为:"<<s->data<<endl;
    }
    return OK;
}

//出栈
Status Pop(LinkStack &s)
{
    if(s == NULL)
    {
        cout<<"空栈无法出栈"<<endl;
        return ERROR;
    }
    int e;
    LinkStack p;
    e = s->data;
    p = s;
    s = p->next;
    free(p);
    cout<<"栈顶元素"<<e<<"成功出栈"<<endl;
}


void show_help()
{
    cout<<"******* Data Structure ******"<<endl;
    cout<<"1----清空链栈"<<endl;
    cout<<"2----判断链栈是否为空"<<endl;
    cout<<"3----求链栈长度"<<endl;
    cout<<"4----获取栈顶元素"<<endl;
    cout<<"5----入栈"<<endl;
    cout<<"6----出栈"<<endl;
    cout<<"7----显式链栈"<<endl;
    cout<<"     退出,输入0"<<endl;

}
int main()
{
    int operate_code;
    show_help();
    LinkStack s;
    InitStack(s);
    while(1)
    {
        cout<<"";
        cin>>operate_code;
        if(operate_code==1)
        {
            ClearStack(s);
        }
        else if (operate_code==2)
        {
            EmptyStack(s);
        }
        else if (operate_code==3)
        {
            StackLength(s);
        }
        else if (operate_code==4)
        {
            GetTopelem(s);
        }
        else if (operate_code==5)
        {
            Pushstack(s);
        }
        else if (operate_code==6)
        {
            Pop(s);
        }
        else if (operate_code==7)
        {
            DisplayStack(s);
        }
        else if (operate_code==0)
        {
            break;
        }
        else
        {
            cout<<"\n操作码错误!!!"<<endl;
            show_help();
        }
    }
    DestroyStack(s);
    return 0;
}


原创文章 85 获赞 46 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Deam_swan_goose/article/details/105068420