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

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

【顺序栈】

定义:

typedef struct

{

int dateMAX;

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->dates-\>top = x;

return 1;

}

}

出栈:

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

{

if (s->top == -1)

return 0;

else

{

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

s->top--;

return 1;

}

}

输出栈中元素:

int printStack(Stack s)//输出

{

int top = s->top;

while (top != -1)

printf("%4d", s->datetop--);

printf("\n");

return 0;

}

int GetTop(Stack s)

{

int top = s->top;

return s->datetop;

}

输出栈顶:

int GetTop(Stack s)

{

int top = s->top;

return s->datetop;

}

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

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),如何修改代码

相关推荐
吃我一窝jio4 小时前
Python与Go线程模型深度对比
java·开发语言·python
eggwyw5 小时前
C++ union 的一种妙用法
开发语言·c++
starzy19907 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
布莱克6059 小时前
Swift 循环与控制转移详解:从 for-in 到标签跳转
开发语言·ssh·swift·循环
瑞克-桑切斯9 小时前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes
敲上瘾10 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
老花眼猫10 小时前
C语言绘图方法:(10)椭圆缩放旋转
c语言·经验分享·青少年编程·课程设计
联犀10 小时前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
lcj251112 小时前
[C++] C++11
开发语言·c++·笔记
kaixin_啊啊12 小时前
【三种鲑科鱼类比较基因组学分析鉴定出参与细胞内寄生菌鲑立克次体抗性的功能候选基因】论文学习
学习