【数据结构】链栈

Lstack.h

#pragma once
#include<iostream>
#include <stdlib.h>
using namespace std;

#define  Elemtype int
typedef struct Nstack
{
    Elemtype data;
    Nstack* next;
}Node;//节点

typedef struct
{
    Node* top;
    int size;
}Lstack;//栈结构

void Init(Lstack *st)
{
    st->top = (Node*)malloc(sizeof(Node));
    if (!st->top)
        return;
    st->top->next= NULL;
    st->top = NULL;
    st->size = 0;
}


void Push(Lstack *st, Elemtype &x)
{
    Node* p = (Node*)malloc(sizeof(Node));
    if (!p)
        return ;
    p->data = x;
    p->next = st->top;
    st->top = p;
    st->size++;
}

bool Empty(Lstack *st)
{
    return st->size==0;
}

void Pop(Lstack *st, Elemtype&e)
{
    if (!Empty(st))
    {
        cout << "栈为空,不能删除!" << endl;
        return;
    }
    Node*cur = st->top;
    e = cur->data;
    st->top = st->top->next;
    free(cur);
    st->size--;
}



void Gettop(Lstack *st,Elemtype &e)
{
    if (!Empty(st))
    {
        cout << "栈为空,没有栈顶元素!" << endl;
        return;
    }
    Node* temp = st->top;
    e = st->top->data;
    cout << "栈顶元素:" << e << endl;
}


void Length(Lstack *st)
{
    cout << "栈长为:" << st->size << endl;
}

void Clear(Lstack *st)
{
    if (Empty(st))
        return;
    st->size = 0;
    Node* p=NULL;
    while (st->top!=NULL)
    {
        p = st->top;
        st->top = p->next;
        free(p);
    }
}

void Destroy(Lstack *st)
{
    Clear(st);
    free(st);
    st = NULL;
}

void show(Lstack *st)
{
    Node* p = st->top;
    while (p)
    {
        cout << p->data;
        p = p->next;
        cout << "-->";
    }
    cout << "栈底"<< endl;
}

mian.cpp

#include"Lstack.h"


void main()
{
    Lstack q;
    Elemtype item;
    int select = 1;
    while (select)
    {
        cout << "**********************************" << endl;
        cout << "* [1] init        [2] push       *" << endl;
        cout << "* [3] gettop      [4] length     *" << endl;
        cout << "* [5] pop         [6] clear      *" << endl;
        cout << "* [7] destroy     [8] show       *" << endl;
        cout << "* [0] quit                       *" << endl;
        cout << "**********************************" << endl;
        cout << "please chose:>";
        cin >> select;
        switch (select)
        {
        case 1:
            Init(&q);
            break;
        case 2:
            cout << "请输入要入栈的元素:";
            cin >> item;
            Push(&q, item);
            break;
        case 3:
            Gettop(&q, item);
            break;
        case 4:
            Length(&q);
            break;
        case 5:
            Pop(&q, item);
            cout << "出栈元素为:" << item << endl;
            break;
        case 6:
            Clear(&q);
            break;
        case 7:
            Destroy(&q);
            cout<<"栈已经被销毁!不能再插入数据!"<<endl;
            return;
        case 8:
            show(&q);
            break;
        case 0:
            cout << "退出成功!" << endl;
            break;
        default:
            break;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值