栈的基本操作

堆栈(简称栈)是一种最常用和最重要的数据结构,是一种只能在一端进行插入或删除数据操作的线性表。表中允许进行插入、删除操作的一端称为栈顶。栈顶当前位置是动态的,栈顶当前位置由一个称为栈顶指针的位置指示器表示。表的另一端称为栈底。当栈中没有数据元素时,称为空栈。栈的插入操作通常称为进栈或入栈。栈的删除操作通常称为退栈或出栈。

栈的主要特点是“后进先出”,及后入栈的元素先弹出。每次进栈的数据元素都放在原当前栈顶元素之上,成为新的栈顶元素,每次出栈的数据都是原当前栈顶元素。


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef int ElemType;
typedef struct Stack *link;
typedef struct Stack Snode;

struct Stack   // 初始化设置
{
    ElemType data;
    struct Stack *next;
};

link init()  //初始化栈
{
    link p;
    p=NULL;
    return p;
}

link push (link Head,ElemType x) //入栈
{
    link p;
    p=(link)malloc(sizeof(Snode));
    if(p==NULL)
    {
        cout<<"\nMemory Error\n";
        return Head;
    }
    p->data=x;
    p->next=Head;
    return p;
}


link pop(link Head)  //出栈
{
    link p;
    p=Head;
    if(p==NULL)
        cout<<"\nStack is Empty\n";
    else
    {
        p=p->next;
        free(Head);
    }
    return p;
}

int lenth(link Head)  //获得栈内元素个数
{
    int len=0;
    link p;
    p=Head;
    while(p!=NULL)
    {
        len++;
        p=p->next;
    }
    return len;
}


ElemType gettop (link Head)   //取栈顶元素
{
    if(Head==NULL)
    {
        cout<<"Stack is Empty!\n";
        return -1;
    }
    else
        return Head->data;
}

int empty (link Head)//判断栈是否为空
{
    if(Head==NULL)
        return 1;
    else
        return 0;
}

void display (link Head)  //显示栈元素
{
    link p;
    p=Head;
    if(p==NULL)
        cout<<"\nStack is empty\n";
    else
        do
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        while(p!=NULL);
}

link setnull(link Head)   //释放栈
{
    link p;
    p=Head;
    while(p!=NULL)
    {
        p=p->next;
        free(Head);
        Head=p;
    }
    return Head;
}

int main()
{
    int i,x;
    link head1;
    head1=init();
    while(i!=6)
    {
        cout<<"\n1.Input a stack data";
        cout<<"\n2.Output a stack data";
        cout<<"\n3.Empty or Not";
        cout<<"\n4.Display a top of stack";
        cout<<"\n5.Display and the length of stack";
        cout<<"\n6.Exit and Free Stack\n";
        cout<<"\n Stack is: ";
        display(head1);
        cout<<"\n";
        cin>>i;
        switch(i)
        {
        case 1:
            while(1)
            {
                system("cls");
                cout<<"\n-.Input a stack data";
                cout<<"\n-.Output a stack data";
                cout<<"\n-.Empty or Not";
                cout<<"\n-.Display a top of stack";
                cout<<"\n-.Exit and Free Stack\n";
                cout<<"\n-.Stack is ";
                display (head1);
                cout<<"\n";
                cout<<"\nInput a number : Until Enter -1:\n";
                cin>>x;
                if(x==1)break;
                head1=push(head1,x);
            }
            break;
        case 2:
            head1=pop(head1);
            break;
        case 3:
            if(empty(head1))
                cout<<"\nStack is Empty\n";
            else
                cout<<"\nStack is not Empty\n";
            system("pause");
            break;
        case 4:
            cout<<"\n The Top is  "<<gettop(head1)<<endl;
            system("pause");
            break;
        case 5:
            cout<<"\nThe Length of Stack is  "<<lenth(head1)<<endl;
            system("cls");
            break;
        }
    }
    system("cls");
    head1=setnull(head1);
    display(head1);
    getchar();
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值