数据结构--栈

顺序栈--静态分配内存

cpp 复制代码
/*
栈 是限定仅在表尾进行插入或删除操作的线性表
对于栈来说,表尾端称为栈顶,表头端称为栈底,不含元素的空表称为空i怕
栈为先进后出
栈是限制插入和删除操作只能在一个位置进行的表,该位置是表的末端 
对于栈的基本操作只有进栈和出栈 
*/ 
//栈的顺序结构实现 
#include <stdio.h>
#define MAXSIZE 100

typedef int ElemType;
typedef struct{
	ElemType data[MAXSIZE];
	int top; 
}Stack; 

//栈的初始化
void initStack(Stack *s)
{
	s->top = -1;
}
//判断栈是否为空
int isEmpty(Stack *s)
{
	if(s->top == -1)
	{
		printf("栈为空\n");
		return 1;
	}
	else
	{
		return 0;
	}
}
//压栈
int push(Stack *s,ElemType e)
{
	if(s->top >= MAXSIZE - 1)
	{
		printf("满了\n");
		return 0;	
	}	
	s->top ++;
	s->data[s->top] = e;
	return 1;
} 
//出栈
ElemType pop(Stack* s,ElemType *e)
{
	if(s->top == -1)
	{
		printf("空的\n");
		return 0;	
	}	
	*e = s->data[s->top];
	s->top --;
	return 1;
} 
//获取栈顶元素
int getTop(Stack* s,ElemType *e)
{
	if(s->top == -1)
	{
		printf("空的\n");
		return 0;
	}
	*e = s->data[s->top];
	return 1;
}
cpp 复制代码
int main()
{
	Stack s;
	initStack(&s);
	push(&s,10);
	push(&s,20);
	ElemType e1;
	getTop(&s,&e1);
	printf("栈顶元素为:%d\n",e1);
	ElemType e2;
	pop(&s,&e2);
	printf("pop出的元素为:%d\n",e2);
	getTop(&s,&e1);
	printf("栈顶元素为:%d\n",e1);
	return 0;
} 

栈---动态分配内存

cpp 复制代码
typedef int ElemType;
typedef struct{
	ElemType *data;
	int top; 
}Stack; 

//栈的初始化
Stack* initStack()
{
	Stack *s = (Stack*)malloc(sizeof(Stack));
	s->data = (ElemType*)malloc(sizeof(ElemType) * MAXSIZE);
	s->top = -1;
	return s;
}

出栈和入栈部分上面的代码都可以用

栈--链式结构

cpp 复制代码
//栈的链式结构实现
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100

typedef int ElemType;
typedef struct stack{
	ElemType data;
	struct stack *next; 
}Stack; 

//栈的初始化
Stack* initStack()
{
	Stack *s = (Stack*)malloc(sizeof(Stack));
	s->data = 0;
	s->next = NULL;
	return s;
}
//判断栈是否为空
int isEmpty(Stack *s)
{
	if(s->next == NULL)
	{
		printf("栈为空\n");
		return 1;
	}
	else
	{
		return 0;
	}
}
//压栈
int push(Stack *s,ElemType e)
{
	Stack *p = (Stack*)malloc(sizeof(Stack));
	p->data = e;
	p->next = s->next;
	s->next = p;
	return 1;
} 
//出栈
ElemType pop(Stack* s,ElemType *e)
{
	if(s->next == NULL)
	{
		printf("空的\n");
		return 0;	
	}	
	*e = s->next->data;
	Stack* q = s->next;
	s->next = q->next;
	free(q);
	return 1;
} 
//获取栈顶元素
int getTop(Stack* s,ElemType *e)
{
	if(s->next == NULL)
	{
		printf("空的\n");
		return 0;
	}
	*e = s->next->data;
	return 1;
}
cpp 复制代码
int main()
{
	Stack *s = initStack();
	push(s,10);
	push(s,20);
	ElemType e1;
	getTop(s,&e1);
	printf("栈顶元素为:%d\n",e1);
	ElemType e2;
	pop(s,&e2);
	printf("pop出的元素为:%d\n",e2);
	getTop(s,&e1);
	printf("栈顶元素为:%d\n",e1);
	return 0;
} 
相关推荐
alphaTao16 分钟前
LeetCode 每日一题 2026/10/12-2026/10/18
算法·leetcode
青岚_Nox21 分钟前
# C语言实战:函数完全指南——从基础概念到static/extern深度解析
算法
CCCCCCCCharlie32 分钟前
深入解析I/O底层:文件描述符与缓冲区真相
linux·开发语言·c++·算法
Cccp.1231 小时前
【leetcode】(十三)大数据处理
数据结构·算法·leetcode
学逆向的1 小时前
C 选择结构的执行效率分析
c语言·开发语言·算法·网络安全
wzdark1 小时前
动态规划算法的状态转移矩阵可视化分析5
算法·矩阵·动态规划
Pniubi1 小时前
力扣70爬楼梯
数据结构·算法·leetcode
人工智能培训1 小时前
具身智能环境交互与学习范式原理
大数据·linux·人工智能·学习·算法·机器学习
纪念 2292 小时前
C++算法(三)
开发语言·c++·算法
学不会pwn不改名2 小时前
时间复杂度与空间复杂度详解
算法