链栈的基本操作

C++链栈的基本操作

#include<iostream>
using namespace std;

typedef int StackElemtype;
typedef struct node
{
    StackElemtype data;
    struct node *next;
}LinkStackNode, *LinkStack;

void Initstack(LinkStack &s);
void Clear(LinkStack &s);
bool Isempty(LinkStack s);
void Push(LinkStack &s, StackElemtype e);
void Pop(LinkStack &s, StackElemtype &e);
void Gettop(LinkStack s, StackElemtype &e);
void Input(LinkStack &s);
void Output(LinkStack s);
void Interface();

int main()
{
    Interface();
    LinkStack s;
    Initstack(s);
    int n;
    while (1)
    {
        cout << "请输入操作序号:";
        cin >> n;
        switch (n)
        {
        case 1:
        {
            Clear(s);
            break;
        }
        case 2:
        {
            if (Isempty(s))
            {
                cout << "链栈为空!" << endl;
            }
            else
            {
                cout << "链栈不为空!" << endl;
            }
            break;
        }
        case 3:
        {
            StackElemtype e;
            cout << "请输入入栈元素:";
            cin >> e;
            Push(s, e);
            break;
        }
        case 4:
        {
            StackElemtype e;
            Pop(s, e);
            cout << "元素" << e << "已出栈!" << endl;
            break;
        }
        case 5:
        {
            StackElemtype e;
            Gettop(s, e);
            cout << "栈顶元素为:" << e << endl;
            break;
        }
        case 6:
        {
            cout << "请依次输入数据,并以-1作为结束标记:" << endl;
            Input(s);
            break;
        }
        case 7:
        {
            cout << "链栈中的元素:" << endl;
            Output(s);
            break;
        }
        case 8:
        {
            Interface();
            break;
        }
        case 0:
        {
            exit(0);
            break;
        }
        default:
        {
            cout << "输入的操作序号不正确!请核对..." << endl;
        }
        }
    }
    return 0;
}

void Initstack(LinkStack &s)
{
    s = new LinkStackNode;
    s->next = NULL;
}

void Clear(LinkStack &s)
{
    StackElemtype e;
    while (!Isempty(s))
    {
        Pop(s, e);
    }
}

bool Isempty(LinkStack s)
{
    if (NULL == s)
    {
        cout << "链栈不存在!" << endl;
        exit(0);
    }
    if (NULL == s->next)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Push(LinkStack &s, StackElemtype e)
{
    LinkStackNode *temp = new LinkStackNode;
    temp->data = e;
    temp->next = s->next;
    s->next = temp;
}

void Pop(LinkStack &s, StackElemtype &e)
{
    if (Isempty(s))
    {
        cout << "链栈为空!" << endl;
        exit(1);
    }
    else
    {
        LinkStackNode *temp = s->next;
        s->next = temp->next;
        e = temp->data;
        delete temp;
    }
}

void Gettop(LinkStack s, StackElemtype &e)
{
    if (Isempty(s))
    {
        cout << "链栈为空!" << endl;
    }
    else
    {
        e = s->next->data;
    }
}

void Input(LinkStack &s)
{
    int e;
    while (1)
    {
        cin >> e;
        if (e == -1)
        {
            break;
        }
        Push(s, e);
    }
}

void Output(LinkStack s)
{
    LinkStackNode *temp = s->next;
    while (temp)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

void Interface()
{
    cout << "***************欢迎使用链栈系统***************" << endl;
    cout << "1:清空链栈                  2:判断链栈是否为空" << endl;
    cout << "3:入栈                      4:出栈" << endl;
    cout << "5:获取栈顶元素              6:输入链栈" << endl;
    cout << "7:输出链栈                  8:菜单" << endl;
    cout << "0:退出" << endl;
    cout << "*************************************************" << endl;
    cout << "输入0-8之间的数:" << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值