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、九分钟辅助挂!天天贵阳辅...
黑科技辅助“wepoker智能... 黑科技辅助“wepoker智能辅助插件”外挂透视辅助器(其实是真的有挂)1、玩家可以在wepoker...
第五分钟辅助!快玩炸翻天辅助工... 第五分钟辅助!快玩炸翻天辅助工具(辅助挂)本来真的有挂(详细辅助规律教程);1、上手简单,内置详细流...
黑科技辅助挂“贪玩蓝月辅助脚本... 黑科技辅助挂“贪玩蓝月辅助脚本”外挂透视辅助脚本(确实是有挂)1、让任何用户在无需贪玩蓝月辅助脚本A...
第9分钟了解!新九游辅助软件(... 第9分钟了解!新九游辅助软件(辅助挂)果然有挂(详细辅助安装教程)该软件可以轻松地帮助玩家将新九游辅...
黑科技辅助“大当家脚本一键挂机... 黑科技辅助“大当家脚本一键挂机”外挂透视辅助软件(切实是有挂)1.大当家脚本一键挂机 ai辅助创建新...
第三分钟辅助器!新漫游免费辅助... 第三分钟辅助器!新漫游免费辅助器(辅助挂)一直是真的有挂(详细辅助黑科技教程)一、新漫游免费辅助器软...
黑科技辅助“微信超级3+1挂辅... 黑科技辅助“微信超级3+1挂辅助”外挂透视辅助工具(总是是真的有挂)一、微信超级3+1挂辅助软件透明...
第五分钟辅助挂!随意玩有没有辅... 第五分钟辅助挂!随意玩有没有辅助(辅助挂)都是是真的有挂(详细辅助透明教程)随意玩有没有辅助辅助器中...
黑科技辅助挂“微乐小程序辅助器... 黑科技辅助挂“微乐小程序辅助器免费”外挂透视辅助软件(总是是有挂)1)微乐小程序辅助器免费辅助挂:进...