【数据结构】顺序栈与链栈

栈的特点是后进先出或先进后出,简称LIFO或FILO,通常top时刻表示栈顶的位置序号,一般空栈时top=-1;入栈栈顶指针加1,s->top++;出栈栈顶指针减1,s->top--

【顺序栈】

定义:

typedef struct

{

int date[MAX];

int top;

} SeqStack,*Stack;

置空栈:

Stack Init_SeqStack()//置空栈

{

Stack s;

s=(SeqStack*)malloc(sizeof(SeqStack));

if(!s)

{

printf("空间不足\n");

return NULL;

}

else

{

s->top = -1;

return s;

}

}

判断是否为空栈:

int Empty_Stack(Stack s)//判断是否空栈

{

if (s->top == -1)

return 1;

else

return 0;

}

进栈:

int Push_SeqStack(SeqStack* s,int x)//进栈

{

if (s->top == MAX - 1)

{

printf("栈满!");

return 0;

}

else

{

s->top++;

s->date[s->top] = x;

return 1;

}

}

出栈:

int Pop_SeqStack(SeqStack* s,int* x)//出栈

{

if (s->top == -1)

return 0;

else

{

*x = s->date[s->top];//(上面定义了一个指针,用指针来接受数据)

s->top--;

return 1;

}

}

输出栈中元素:

int printStack(Stack s)//输出

{

int top = s->top;

while (top != -1)

printf("%4d", s->date[top--]);

printf("\n");

return 0;

}

int GetTop(Stack s)

{

int top = s->top;

return s->date[top];

}

输出栈顶:

int GetTop(Stack s)

{

int top = s->top;

return s->date[top];

}

以下为一个顺序栈,实现栈的入栈、出栈并将其中元素输出功能

cs 复制代码
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
	int date[MAX];
	int top;
} SeqStack,*Stack; //SeqStack*==Stack
Stack Init_SeqStack()//置空栈 
{
	Stack s;
	s=(SeqStack*)malloc(sizeof(SeqStack));
	if(!s)
	{
		printf("空间不足\n");
		return NULL;
	}
	else
	{
		s->top = -1;
		return s;
	}
}
int Empty_Stack(Stack s)//判断是否空栈 
{
	if (s->top == -1)
		return 1;
	else
		return 0;
}
int Push_SeqStack(Stack s,int x)//进栈 
{
	if (s->top == MAX - 1)
	{
		printf("栈满!");
		return 0;
	}
	else
	{
		s->top++;
		s->date[s->top] = x;
		return 1;
	}
}
int Pop_SeqStack(Stack s,int* x)//出栈  
{
	if (s->top == -1)
		return 0;
	else
	{
		*x = s->date[s->top];//(上面定义了一个指针,用指针来接受数据) 
		s->top--;
		return 1;
	}

}
int printStack(Stack s)//输出 
{
	int top = s->top;
	while (top != -1)
		printf("%4d", s->date[top--]);
	printf("\n");
	return 0;
}
int GetTop(Stack s)
{
	
	int top = s->top;
	return s->date[top];
}
int main()
{
	Stack s;
	int x;
	s=Init_SeqStack();
	Empty_Stack(s);
	printf("请输入入栈数字:\n");
	scanf("%d", &x);
	while (x != 0)
	{
		Push_SeqStack(s,x);
		printf("请输入入栈数字:\n");
		scanf("%d", &x);
	}
	printStack(s);
	printf("栈顶:%d\n",GetTop(s));
	printf("出栈测试:");
	Pop_SeqStack(s,&x);//这个函数上面x定义的为指针,所以要加取地址符 
	printStack(s);
	return 0;
}

【链栈】

定义:

typedef struct node

{

int date;

struct node* next;

}StackNode, * LinkStack;

实现链栈的入栈、出栈并将其中元素输出功能

cs 复制代码
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int date;
	struct node* next;
}StackNode, * LinkStack;
void Init_LinkStack(LinkStack &p)//初始化 
{
	p = (LinkStack)malloc(sizeof(StackNode));
	p->date = 0;
	p->next = NULL;
}
int Empty_LinkStack(LinkStack top)
{
	if (top == NULL)
		return 1;
	else
		return 0;
}
void Push_LinkStack(LinkStack &top, int x)//入栈 
{
	LinkStack s;
	s = (LinkStack)malloc(sizeof(StackNode));
	s->date = x;
	s->next = top->next;
	top->next = s;
}
LinkStack Pop_LinkStack(LinkStack &top, int &x)//出栈 
{
	LinkStack p;
	if (top->next == NULL)
		return NULL;
	else
	{
		p = top->next;
		x = p->date;
		top->next = p->next;
		free(p);
		return top;
	}
}
int GetTop(LinkStack s, int x)//栈顶
{
	if (s->next == NULL)
		return NULL;
	x = s->next->date;
	return x;
}

void printStack(LinkStack top)//输出 
{
	while (top != NULL)
	{
		printf("%4d", top->date);
		top = top->next;
	}
	printf("\n");
}
int main()
{
	LinkStack top;
	int x=0;
	Init_LinkStack(top);
	Empty_LinkStack(top);
	printf("请输入入栈元素:\n");
	scanf("%d", &x);
	while (x != 0)
	{
		Push_LinkStack(top, x);
		printf("请输入入栈元素:\n");
		scanf("%d", &x);
	}
	printStack(top);
	printf("栈顶为:%d\n", GetTop(top, x));
	printf("出栈测试:\n");
	Pop_LinkStack(top, x);
	if (top != NULL)
	{
		printStack(top);
	}
	else
		printf("栈空,无法输出!");
	return 0;
}

注意:链栈中的top是我所定义的一个指针,与顺序栈不同

求助:在链栈的代码中,输出栈中元素时,一直会在输出栈中元素的前端出现一个0(我在初始化函数中,将p->date赋初值为0),如何修改代码

相关推荐
小奶包他干奶奶2 小时前
Webpack学习——Loader(文件转换器)
前端·学习·webpack
小奶包他干奶奶2 小时前
Webpack学习——原理理解
学习·webpack·devops
励志成为美貌才华为一体的女子3 小时前
强化学习PPO和GRPO逻辑学习
学习
tobebetter95273 小时前
How to manage python versions on windows
开发语言·windows·python
meichaoWen3 小时前
【Vue3】vue3的全面学习(一)
前端·javascript·学习
FFF团团员9093 小时前
树莓派学习笔记3:LED和Button
笔记·学习
9***P3344 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger4 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz4 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*4 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven