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

栈的特点是后进先出或先进后出,简称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),如何修改代码

相关推荐
梁诚斌15 分钟前
使用OpenSSL接口读取pem编码格式文件中的证书
开发语言·c++
瓜子三百克33 分钟前
Objective-C 路由表原理详解
开发语言·ios·objective-c
明月看潮生1 小时前
青少年编程与数学 02-022 专业应用软件简介 07 国产工业软件
青少年编程·工业软件·编程与数学·应用软件·国产软件
幽蓝计划1 小时前
HarmonyOS NEXT仓颉开发语言实战案例:外卖App
开发语言·harmonyos
伍哥的传说1 小时前
鸿蒙系统(HarmonyOS)应用开发之实现电子签名效果
开发语言·前端·华为·harmonyos·鸿蒙·鸿蒙系统
ytttr8731 小时前
matlab通过Q学习算法解决房间路径规划问题
学习·算法·matlab
小张成长计划..2 小时前
数据结构-栈的实现
开发语言·数据结构
旷世奇才李先生2 小时前
Lua 安装使用教程
开发语言·lua
Zonda要好好学习2 小时前
Python入门Day2
开发语言·python
不良手残2 小时前
IDEA类和方法注释模板设置-保姆教程
java·开发语言