链表栈的基本操作

本文详细介绍了 C++ 中基于链表实现的栈(Stack)数据结构的定义与操作,包括初始化、尾插、头删、打印、清除、获取栈顶元素、栈空判断、栈长度等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	#ifndef _STACKLIST_H  
	#define _STACKLIST_H  
	  
	#include<iostream>  
	#include<assert.h>  
	using namespace std;  
	  
	#define ElemType int  
	  
	typedef struct Node  
	{  
	    ElemType data;  
	    struct Node *next;  
	}Node,*PNode;  
	  
	typedef struct Stack  
	{  
	    PNode top;  
	    PNode rear;  
	    size_t size;  
	}Stack;  
	  
	int StackEmpty(Stack *st);  
	void InitList(Stack *st);//初始化  
	bool push(Stack *st,ElemType x);//尾插  
	bool pop(Stack *st);//头删  
	void ShowStack(Stack *st);//打印  
	void clear(Stack *st);//清除  
	void destroy(Stack *st);//摧毁  
	int length(Stack *st);//长度  
	ElemType GetTop(Stack *st);//栈顶元素  
	  
	#endif  




#include "StackList.h"  
	  
	int StackEmpty(Stack *st)  
	{  
	    if(st->size == 0)  
	    {  
	        return 1;  
	    }  
	    return 0;  
	}  
	  
	void InitList(Stack *list)//初始化  
	{  
	    Node* s = (Node*)malloc(sizeof(Node));  
	    assert(s != NULL);  
	    s->next = NULL;  
	    list->rear = list->top = s;  
	    list->size = 0;  
	}  
	  
	bool push(Stack *st, ElemType x)//尾插  
	{  
	    Node *p = (Node*)malloc(sizeof(Node));  
	    assert(p != NULL);  
	    p->data = x;  
	    p->next = st->top->next;  
	    st->top->next = p;   
	    st->size++;  
	    return true;  
	}  
	  
	void ShowStack(Stack *st)//打印  
	{  
	    Node *p = st->top->next ;  
	    cout<<"NULL"<<endl;  
	    while(p != NULL)  
	    {  
	        cout<<p->data <<endl;  
	        p = p->next ;  
	    }  
	}  
	  
	bool pop(Stack *st)//头删  
	{  
	    if(StackEmpty(st))  
	    {  
	        cout<<"栈已空,不能出栈!"<<endl;  
	        return false;  
	    }  
	    Node *p = st->top->next;  
	    if(st->size == 1)  
	    {  
	        free(p);  
	        st->top->next = NULL;
		}
	    else  

	    {  
	        st->top->next = p->next ;  
	        free(p);  
	    }  
	    st->size--;  
	    return true;  
	}  
	  
	ElemType GetTop(Stack *st)//栈顶元素  
	{  
	    if(StackEmpty(st)) 
	{ 
 
	        printf("栈已空,没有栈顶元素!\n");  
	        return -1;  
	    }  
	    return st->top->next->data;  
	}  
	void clear(Stack *st)//清除  
	{  
	    Node *p = st->top->next;  
	    while(p != NULL)  
	    {  
	        st->top->next = p->next;  
           free(p);  
	        p = st->top->next;  
	    }  
	    st->rear  = st->top ;  
	    st->size  = 0;   
	}  
	  
	void destroy(Stack *st)//摧毁  
	{  
	    clear(st);  
	    free(st->top);  
	    st->top = st->rear = NULL;  
	}  
	  
	int length(Stack *st)//长度  
	{  
	    return st->size ;  
	}  


	#include "StackList.h"  
	  
	void main()  
	{  
	    Stack myStack;  
	    InitList(&myStack);  
	    int select = 1;  
	    ElemType item;  
	    ElemType pos;  
	    Node *p = NULL;  
	    while(select)  
	    {  
	        cout<<"*************************************"<<endl;  
	        cout<<"* [0] quit_system   [1] push        *"<<endl;  
	        cout<<"* [2] show_seqStack [3] pop         *"<<endl;  
	        cout<<"* [4] clear         [5] destroy     *"<<endl;  
	        cout<<"* [6] length        [7] getTop      *"<<endl;    
	        cout<<"*************************************"<<endl;  
	        cout<<"请选择:>";  
	        cin>>select;  
	        switch(select)  
	        {  
	        case 1:  
	            cout<<"请输入要插入的数据(-1结束):>";  
	            while(cin>>item,item!=-1)  
	            {  
	                push(&myStack,item);  
	            }  
	            break;  
	        case 2:  
	            ShowStack(&myStack);  
               break;  
	        case 3:  
	            pop(&myStack);  
	            break;  
	        case 4:  
	            clear(&myStack);  
	            break;  
	        case 5:  
	            destroy(&myStack);  
	            break;  
	        case 6:  
	            cout<<"栈空间为:"<<length(&myStack)<<endl;  
	            break;  
	        case 7:  
	            cout<<"栈顶元素为:"<<GetTop(&myStack)<<endl;  
	            break;  
	        default:  
	            break;  
	        }  
	    }  
	    destroy(&myStack);  
	}  

<img src="https://img-blog.youkuaiyun.com/20150507233301539?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpY2hhbmp1YW4z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<img src="https://img-blog.youkuaiyun.com/20150507233319744?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpY2hhbmp1YW4z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值