栈的基本操作

2023-06-20 0 666

结构体定义:

静态
typedef struct{
	int data[MAXSIZE];
	int top;
}SqStack;
链栈
typedef struct LStack{
	int data;
	struct LStack *next;
}LStack,*LinkStack; 
共享栈
typedef struct{
	int data[MAXSIZE];
	int top[2];
}ShareStack;

基本操作

////静态栈基本操作 
//判栈空
bool StackEmpty(SqStack a){
	if(a.top==-1){
		return true;
	}else{
		return false;
	}
} 

//进栈
bool Push(SqStack &a,int x){
	if(a.top == MAXSIZE-1)return false;
	a.data[++a.top] = x;
	return true;
}

//出栈
bool Pop(SqStack &a,int &e){
	if(a.top == -1)return false;
	e = a.data[a.top--];
	return true;
} 

//读栈顶
bool GetTop(SqStack a,int &e){
	if(a->top==-1)return false;
	e = a.data[a.top];
	return true;
} 
 
////链栈基本操作
//初始化  
void InitLStack(LinkStack &a){
	a = (LStack*)malloc(sizeof(LStack));
	a->next = NULL;
}

//进栈
//链栈无容量限制 想压多少压多少 无需判定是否溢出 
void pushL(LinkStack &a,int x){
	LinkStack s;
	s = (LStack*)malloc(sizeof(LStack));
	s->next = a->next;
	s->data = x;
	a->next = s;
} 

//出栈
bool PopL(LinkStack &a,int &e){
   if(a->next == NULL)return false;
   LinkStack s = a->next;
   e = s->data;
   a = s->next;
   free(s);
   return true;
} 

//判栈空
bool EmptyStackL(LinkStack a){
	if(a->next == NULL)return true;
	else return false;
} 

//读栈顶
bool GetTopL(LinkStack a,int &e){
	if(a->next == NULL) return false;
	else{
		e = a->next->data;
		return true;
	}
}

//销毁栈
void DestroyStackL(LinkList &a){
	LinkStack b=a,s;
	while(b){
	  s = b->next;
	  free(b);
	  b = s;
	}
	free(a);
} 

////共享栈
//初始化
void InitStackS(ShareStack &a){
	a->top[0] = -1;
	a->top[1] = MAXSIZE-1;
} 

//入栈(最后一个参数用来选择插入的是哪一个栈) 
bool PushS(ShareStack &a,int x,int t){
	if(t!=0 || t!=1){
		printf("栈号无效!");
		return false;
	}
	if(a->top[1] - a->top[0] == 1)return false;
	if(t==1){
		a->top[0]++;
		a->data[a->top[0]] = x;
	}else{
		a->top[1]--;
		a->data[a->top[1]] = x;
	}
	return true;
} 

//出栈
bool PopS(ShareStack &a,int &e,int t){
	if(t!=0 || t!=1){
		printf("栈号无效!");
		return false;
	}
	if(a->top[0] == -1 || a->top[1] == MAXSIZE-1)return false;
	if(t==1){
		e = a->data[a->top[0]]; 
		a->top[0]--;	
	}else{
		e = a->data[a->top[1]]; 
		a->top[1]++;	
	} 
	return true;
}
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

本站部分资源收集自互联网,我站仅分享内容,仅供用户个人研究,不提供商业使用,如有侵权下架处理,觉得资源内容不错请您在官方渠道购买正版,您在阅读的同时下载的内容本站不承担任何法律责任!Some resources of this website are collected from the Internet. We only share the content, which is only for personal research of users, and not for commercial use. If there is infringement and off shelf processing, please purchase the authentic version of the resource content from the official channel if you think it is good. We will not bear any legal responsibility for the content you download while reading!

橙凰素材-精品源码下载|正版主题更新 学习交流 栈的基本操作 https://b.bqzmz.com/2023/06/20/%e6%a0%88%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c/

为用户提供极致的体验

上一篇: 链表题目
下一篇: 队列的基本操作
常见问题
  • 正版主题是指站长在指定渠道购买的官方主题,提供作者联系,资源支持,长期更新等等内容
查看详情
  • 非常抱歉,请联系网站页脚的客服,我们将在核实后下架处理并返还给您此资源的收益(会扣去网站运营的费用),谢谢理解!
查看详情
  • 网络资源是站长从互联网分享收集而来的内容,本站不会做测试,直接分享,不包源码可用性,运营性,如有需求点击右边的联系正版,谢谢!
查看详情
  • 如果资源不是最新版本,请在资源下方评论区进行催更或提交工单处理,我们会尽快更新并通过邮箱通知您。
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务