顺序栈和链栈-出入栈-C语言-数据结构与算法

1-顺序栈入栈
2-顺序栈出栈
3-链栈入栈
4-链栈出栈

//****************************************
//数据结构:顺序栈和链栈
//算法:出栈和入栈
//首次编辑时间:2020/05/27
//最后修改时间:2020/05/28
//****************************************
#include<stdio.h>
#include<stdlib.h>
 
typedef struct seqstack//顺序栈
{
	int data[20];
	int top;
}seqstack;

typedef struct linkstack//链栈
{
	int data;
	struct linkstack *next;
}node;

seqstack creat_seqstack();//创建顺序栈
void show_seqstack();//遍历打印显示顺序栈
seqstack push_seqstack();//顺序栈入栈
seqstack pop_seqstack();//顺序栈出栈

node* creat_linkstack();//创建链栈
void show_linkstack();//遍历打印显示链栈
node* push_linkstack();//链栈入栈
node* pop_linkstack();//链栈出栈

int main()
{
	seqstack st = creat_seqstack();
	node *top = creat_linkstack();
	while (1)
	{
		int action;
		printf("*****************************\n");
		printf("选择:\n");
		printf("0-结束退出\n");
		printf("1-顺序栈入栈\n");
		printf("2-顺序栈出栈\n");
		printf("3-链栈入栈\n");
		printf("4-链栈出栈\n");
		printf("-----------------------------\n");
		printf("原顺序栈:");
		show_seqstack(st);
		printf("\n");
		printf("原链栈:");
		show_linkstack(top);
		printf("\n");
		printf("-----------------------------\n");
		printf("输入选择序号:");
		scanf("%d", &action);
		printf("-----------------------------\n");
		switch (action)
		{
		case 0:return 0;
		case 1:st = push_seqstack(st);printf("新顺序栈:");show_seqstack(st);printf("\n");break;
		case 2:st = pop_seqstack(st);printf("新顺序栈:");show_seqstack(st);printf("\n");break;
		case 3:top = push_linkstack(top);printf("新链栈:");show_linkstack(top);printf("\n");break;
		case 4:top = pop_linkstack(top);printf("新链栈:");show_linkstack(top);printf("\n");break;
		default:printf("无效序号!");
		}
	}
}

seqstack creat_seqstack()//创建顺序栈
{
	seqstack st = { {1,2,3,4,5}, 4 };//创建时顺便放入5个数
	return st;
}

void show_seqstack(seqstack st)//遍历打印显示顺序栈
{
	while(st.top != -1)
		printf("%d ",st.data[st.top--]);
}

seqstack push_seqstack(seqstack st)//顺序栈入栈
{
	int value;
	printf("请输入入栈值:");
	scanf("%d", &value);
	st.data[++(st.top)] = value;
	return st;
}

seqstack pop_seqstack(seqstack st)//顺序栈出栈
{
	--st.top;
	return st;
}

node* creat_linkstack()//创建链栈
{
	node *top, *p;
	int i;
	top = (node*)malloc(sizeof(node));
	top->next = NULL;
	top->data = 1;//创建时顺便放入5个数
	for (i = 2;i < 6;i++)
	{
		p = (node*)malloc(sizeof(node));
		p->next = top;
		p->data = i;
		top = p;
	}
	return top;
}

void show_linkstack(node *top)//遍历打印显示链栈
{
	while (top != NULL)
	{
		printf("->%d", top->data);
		top = top->next;
	}
}

node* push_linkstack(node *top)//链栈入栈
{
	int value;
	node *p;
	printf("请输入入栈值:");
	scanf("%d", &value);
	p = (node*)malloc(sizeof(node));
	p->data = value;
	p->next = top;
	return p;
}

node* pop_linkstack(node *top)//链栈出栈
{
	node *d = top;
	top = top->next;
	free(d);
	return top;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序是一种基于数组实现的,它的特点是具有随机存取的特性。顺序的基本运算包括进、出和查看顶元素。进操作将元素插入到顶,出操作将顶元素删除并返回,查看顶元素操作返回顶的元素值,但不修改的状态。 在C语言中,顺序的存储结构可以使用一个一维数组来存放中的元素,同时使用一个指示器top来指示顶的位置。在进行进和出操作时,需要更新top的值,使其指向顶元素。 下面是一种常见的顺序的定义和基本操作的示例代码: ```c // 定义中元素的数据类型 typedef int StackElementType; // 定义顺序的存储结构 #define Stack_Size 100 // 的最大容量 typedef struct { StackElementType elem[Stack_Size]; // 用数组存放中元素 int top; // 顶指针 } SeqStack; // 初始化顺序 void Init_SeqStack(SeqStack *S) { S->top = -1; // 初始时为空,顶指针置为-1 } // 进操作 void Push_SeqStack(SeqStack *S, StackElementType x) { if (S->top == Stack_Size - 1) { printf("已满,无法进"); return; } S->top++; // 顶指针加1 S->elem[S->top] = x; // 将新元素放入顶位置 } // 出操作 StackElementType Pop_SeqStack(SeqStack *S) { if (S->top == -1) { printf("为空,无法出"); return -1; // 返回一个特殊值表示出错 } StackElementType x = S->elem[S->top]; // 获取顶元素的值 S->top--; // 顶指针减1 return x; // 返回顶元素的值 } // 查看顶元素 StackElementType GetTop_SeqStack(SeqStack *S) { if (S->top == -1) { printf("为空"); return -1; // 返回一个特殊值表示出错 } return S->elem[S->top]; // 返回顶元素的值 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值