C语言-栈的实现
创始人
2025-01-07 06:06:08
0

stack.h

#ifndef STACK_H #define STACK_H #include  #include  #include    #define data_t uint32_t typedef struct Node{ 	data_t data; 	struct Node* next; }Node; typedef struct stack{ 	Node* top; 	uint32_t count; }link_stack; link_stack* init_link_stack(); Node* init_node(); link_stack* push_stack(link_stack* p,data_t data); _Bool isEmpty(link_stack* p); void disp_stack(link_stack* p); link_stack* pop_stack(link_stack *p); void clear_stack(link_stack* p); #endif 

stack.c 

#include "stack.h" // 初始化栈 link_stack*  init_link_stack(){ 	link_stack *p=calloc(1,sizeof(link_stack)); 	if(p==NULL){ 	printf("初始化栈失败!\n"); 	exit(0); 	} 	return p; } // 初始化结点 Node* init_node(){ 	Node* p=calloc(1,sizeof(Node)); 	if(p==NULL){ 		printf("初始化结点失败!\n"); 		exit(0); 	} 	return p; } //入栈push link_stack* push_stack(link_stack* p,data_t data){ 	if(isEmpty(p)){ 		return NULL; 	} 	Node* temp=init_node(); 	temp->data=data; 	temp->next=p->top; 	p->top=temp; 	p->count++; 	return p; } // 出栈pop link_stack* pop_stack(link_stack* p){ 	if(isEmpty(p)){ 		return NULL; 	} 	if(p->count==0){ 		return NULL; 	} 	Node* temp=p->top; 	p->top=p->top->next; 	p->count--; 	free(temp); 	temp==NULL; 	return p; } // 遍历 void disp_stack(link_stack *p){ 	if(isEmpty(p)){ 		return; 	} 	if(p->count==0){ 		printf("栈为空!\n"); 		return ; 	} 	Node* temp=p->top; 		printf("```````````\n"); 	while(temp!=NULL){ 		printf("%d\n",temp->data); 		temp=temp->next; 	} 		printf("```````````\n"); } void clear_stack(link_stack* p){ 	if(isEmpty(p)){ 		return; 	} 	Node* temp=p->top; 	while(temp!=NULL){ 		Node* node=temp; 		temp=temp->next; 		free(node); 		node=NULL; 	} 	free(p); 	p=NULL; } // 判断栈是否为空 _Bool isEmpty(link_stack* p){ 	if(p==NULL){ 		return 1; 	} 	return 0; }  

main.c 

#include "stack.h"  void select_menu(); void menu();  link_stack* p; link_stack* temp; int judge=1; void main(){ 	while(judge){ 		menu(); 		select_menu(); 	} } void select_menu(){ 	printf("请选择:"); 	int select=0; 	scanf("%d",&select); 	switch(select){ 		case 0: 			 judge=0; 			 clear_stack(p); 			 break;	 		case 1: 			 p=init_link_stack(); 			 if(p!=NULL){ 				printf("初始化成功!\n"); 			 } 			 break; 		case 2: 			 if(isEmpty(p)){ 				printf("请先初始化!\n"); 			 }else{ 				data_t data=0; 				printf("data="); 				scanf("%d",&data); 				temp=push_stack(p,data); 				if(temp!=NULL){ 					printf("添加成功! \n"); 					disp_stack(p); 				} 			 } 			 break; 		case 3: 			 if(isEmpty(p)){ 			 	printf("请先初始化!\n"); 			 }else{ 				if(pop_stack(p)==NULL){ 					printf("栈内无元素!\n"); 				}else{ 					printf("pop成功!\n"); 					disp_stack(p); 				} 			 } 			 break; 		case 4: 			 disp_stack(p); 			 break; 		default: 			printf("输入错误!"); 	} } void menu(){ 	printf("--------------\n"); 	printf("0:退出\n"); 	printf("1:初始化stack\n"); 	printf("2:push\n"); 	printf("3:pop \n"); 	printf("4:遍历\n"); 	printf("--------------\n"); } 

上一篇:phpinfo

下一篇:C++代码_让室友坑我

相关内容

热门资讯

第三分钟了解!八仙竞技馆辅助器... 第三分钟了解!八仙竞技馆辅助器下载,欢乐掌心辅助,经验教程(有挂方略)-哔哩哔哩1、八仙竞技馆辅助器...
透视有挂!小程序牵手跑得有外g... 透视有挂!小程序牵手跑得有外g挂吗,欢乐情怀辅助挂-确实是真的有辅助教程(哔哩哔哩)1、小程序牵手跑...
透视规律!福建兄弟13水辅助,... 透视规律!福建兄弟13水辅助,wepoker免费脚本弱密码,透视方针(真的有挂)-哔哩哔哩1、让任何...
今天下午!阿拉游戏中心辅助工具... 今天下午!阿拉游戏中心辅助工具(辅助)都是是有辅助安装(有挂方法)-哔哩哔哩1、很好的工具软件,可以...
第三分钟了解!多乐跑辅助,多乐... 第三分钟了解!多乐跑辅助,多乐辅助,妙计教程(有挂解密)-哔哩哔哩所有人都在同一条线上,像星星一样排...
网友热议!微乐家乡app下载,... 网友热议!微乐家乡app下载,贪玩互娱辅助-切实是真的有辅助插件(哔哩哔哩)一、微乐家乡app下载游...
黑科技技巧!广东雀神智能插件安... 黑科技技巧!广东雀神智能插件安装测试,wepoker怎么获得好牌,透视要领(有挂头条)-哔哩哔哩1、...
做出回应!老友福建麻将辅助(辅... 做出回应!老友福建麻将辅助(辅助)其实是真的辅助神器(确实有挂)-哔哩哔哩亲,关键说明,老友福建麻将...
第3分钟了解!鱼乐达人脚本辅助... 第3分钟了解!鱼乐达人脚本辅助,拱趴大菠萝运势,大纲教程(有挂方式)-哔哩哔哩1、下载好鱼乐达人脚本...
据悉!欢乐联盟免费辅助,广西友... 据悉!欢乐联盟免费辅助,广西友乐免费辅助-一直确实有辅助插件(哔哩哔哩)1、起透看视 广西友乐免费辅...