C语言-栈的实现

stack.h

cpp 复制代码
#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


#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

cpp 复制代码
#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

cpp 复制代码
#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");
}
相关推荐
北冥湖畔的燕雀2 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
QX_hao3 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白3 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J5 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
朱嘉鼎5 小时前
状态机的介绍
c语言·单片机
爱喝白开水a6 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
Neverfadeaway6 小时前
【C语言】深入理解函数指针数组应用(4)
c语言·开发语言·算法·回调函数·转移表·c语言实现计算器
武子康6 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
一碗绿豆汤6 小时前
c语言-流程控制语句
c语言
杰克尼6 小时前
JavaWeb_p165部门管理
java·开发语言·前端