数据结构—P4 栈

在这里插入图片描述

简介

在这里插入图片描述

stack)是限定仅在表尾进行插入和删除操作的线性表

允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom),不含任何数据元素的栈称为空栈;栈又称为后进先出(Last In First Out)的线性表,简称LIFO结构

栈的插入操作,叫作进栈、入栈
栈的删除操作,叫作出栈,也有的叫作弹栈

顺序栈

既然栈时线性表的特例,那么栈的顺序存储其实也是线性表顺序存储的简化,简称顺序栈

链式栈

栈的链式存储结构,简称为链式栈

顺序栈的实现

在这里插入图片描述

创建结构体

创建所需文件

在这里插入图片描述
编写Makfile

在这里插入图片描述

test:test.o sqstack.o
	gcc -o test test.o sqstack.o
test.o:test.c sqstack.h
	gcc -c test.c
sqstack.o:sqstack.c sqstack.h
	gcc -c sqstack.c
.PHONY:clean
clean:
	rm *.o

编写sqstack.h

在这里插入图片描述

//定义int类型的数据的别名为data_t
typedef int data_t;

//定义顺序栈结构体,并创建结构体的别名为sqstack
typedef struct{
	data_t *data;//数据指针
	int maxlen;//数据长度
	int top;//栈顶位置
}sqstack;

创建与销毁

编写sqstack.h

在这里插入图片描述
编写创建函数

在这里插入图片描述

sqstack* sqstack_create(int len){
	sqstack *s;

	//分配内存空间
	if((s = (sqstack *)malloc(sizeof(sqstack))) == NULL){
		printf("malloc is failed\n");
		return NULL;
	}
	if((s->data = (data_t *)malloc(len * sizeof(data_t))) == NULL){
		printf("malloc is failed\n");
		free(s);
		return NULL;
	}

	//表数据初始化
	memset(s->data,0,len*sizeof(data_t));
	s->maxlen = len;
	s->top = -1;

	//返回表
	return s;
}//创建表

编写销毁函数

在这里插入图片描述

int sqstack_free(sqstack *s){
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(s->data != NULL){
		free(s->data);
	}
	free(s);
}//释放表

编写清空函数

在这里插入图片描述

int sqstack_clear(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	//将栈顶指向-1清空栈
	s->top = -1;
	return 0;
}//清空表

状态属性

编写sqstack.h

在这里插入图片描述

创建是否为空函数

在这里插入图片描述

int sqstack_isempty(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}	
	//判断是否为空
	if(s->top == -1){
		return 1;
	}else{
		return 0;
	}
}//表是否为空



创建是否为满函数

在这里插入图片描述

int sqstack_isfull(sqstack *s){
	if(s = NULL){
		printf("s is NULL\n");
		return -1;
	}
	//判断是否为满
	if(s->top == s->maxlen-1){
		return 1;
	}else{
		return 0;
	}
}//表是否为满


创建栈顶判断函数

在这里插入图片描述

data_t sqtack_top(sqstack *s){
	return (s->data[s->top]);
}//栈顶判断

入栈出栈

编写sqstack.h

在这里插入图片描述

编写入栈程序

在这里插入图片描述

int sqstack_push(sqstack *s,data_t value){
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(s->top == s->maxlen-1){
		printf("stack is full\n");
	}
	s->top++;
	s->data[s->top] = value;

	return 0;
}//表入栈



编写出栈程序

在这里插入图片描述

data_t sqstack_pop(sqstack *s){
	s->top--;
	return (s->data[s->top+1]);
}//表出栈

查询操作

编写sqstack.h

在这里插入图片描述

编写查询全部信息

在这里插入图片描述

int sqstack_show(sqstack *s){
	int i;
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(sqstack_isempty(s)){
		printf("sqstack is empty\n");
	}	

	printf("sqstack:\n");
	for(i = 0;i <= s->top;i++){
		printf("%d  ",s->data[i]);
	}
	printf("\n");
	return 0;
}//查询并显示所有表内容

编写test.c

在这里插入图片描述

//入栈出栈测试程序
int pushpop_test(){
	sqstack *s;
	int value;

	s = sqstack_create(100);
	if(s == NULL){
		return -1;
	}

	while(1){
		printf("input(-1quit):");
		scanf("%d",&value);
		if(value == -1){
			break;
		}else{
			sqstack_push(s,value);
			sqstack_show(s);
		}
	}

	while(!sqstack_isempty(s)){
		sqstack_pop(s);
		sqstack_show(s);
	}

	sqstack_free(s);
	
}

测试程序

在这里插入图片描述


编写查询指定位置内容

在这里插入图片描述

int sqstack_poslocate(sqstack *s,int pos){
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(sqstack_isempty(s)){
		printf("sqstack is empty\n");
	}	
	printf("sqstack:\n%d\n",s->data[pos]);
	return 0;
}//查询指定位置内容


编写查询指定内容位置

在这里插入图片描述

int sqstack_valuelocate(sqstack *s,data_t value){
	int i;
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(sqstack_isempty(s)){
		printf("sqstack is empty\n");
	}	
	for(i = 0;i <= s->top;i++){
		if(s->data[i] == value){
			printf("pos:\n%d\n",i);
		}
	}
}//查询指定内容位置

编写test.c

在这里插入图片描述

//查询测试程序
int locate_test(){
	sqstack *s;
	int input,pos,value;

	s = sqstack_create(100);
	if(s == NULL){
		return -1;
	}

	while(1){
		printf("input(-1quit):");
		scanf("%d",&input);
		if(input == -1){
			break;
		}else{
			sqstack_push(s,input);
			sqstack_show(s);
		}
	}

	while(1){
		printf("pos(-1quit):");
		scanf("%d",&pos);
		if(pos == -1){
			break;
		}else{
			sqstack_poslocate(s,pos);
		}
	}

	while(1){
		printf("value(-1quit):");
		scanf("%d",&value);
		if(value == -1){
			break;
		}else{
			sqstack_valuelocate(s,value);
		}
	}
	sqstack_free(s);
}

测试程序

在这里插入图片描述

修改操作

编写sqstack.h
在这里插入图片描述

编写修改指定位置内容

在这里插入图片描述

int sqstack_posmodify(sqstack *s,int pos,data_t value){
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(sqstack_isempty(s)){
		printf("sqstack is empty\n");
	}	
	s->data[pos] = value;
	return 0;
}//修改指定位置内容



编写修改指定内容为指定内容

在这里插入图片描述

int sqstack_valuemodify(sqstack *s,data_t rvalue,data_t value){
	int i;
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(sqstack_isempty(s)){
		printf("sqstack is empty\n");
	}	
	for(i = 0;i <= s->top;i++){
		if(s->data[i] == rvalue){
			s->data[i] = value;
		}
	}
	return 0;
}//修改指定内容到指定内容

编写test.c

在这里插入图片描述

//修改测试程序
int modify_test(){
	sqstack *s;
	int input,pos,rvalue,value;

	s = sqstack_create(100);
	if(s == NULL){
		return -1;
	}

	while(1){
		printf("input(-1quit):");
		scanf("%d",&input);
		if(input == -1){
			break;
		}else{
			sqstack_push(s,input);
			sqstack_show(s);
		}
	}

	while(1){
		printf("pos(-1quit):");
		scanf("%d",&pos);
		if(pos == -1){
			break;
		}
		printf("value:");
		scanf("%d",&value);
		sqstack_posmodify(s,pos,value);
		sqstack_show(s);
	}
	while(1){
		printf("rvalue(-1quit):");
		scanf("%d",&rvalue);
		if(rvalue == -1){
			break;
		}
		printf("value:");
		scanf("%d",&value);
		sqstack_valuemodify(s,rvalue,value);
		sqstack_show(s);
	}

}

测试程序

在这里插入图片描述

链式栈的实现

在这里插入图片描述

创建结构体

创建文件

在这里插入图片描述

编写Makefile

在这里插入图片描述

test:test.o lkstack.o
	gcc -o test test.o lkstack.o
test.o:test.c lkstack.h
	gcc -c test.c
lkstack.o:lkstack.c lkstack.h
	gcc -c lkstack.c
.PHONY:clean
clean:
	rm *.o

编写sqstack.h

在这里插入图片描述

//定义int类型的数据的别名为data_t
typedef int data_t;

//定义链式栈机构体,并创建别名为listnode和lkstack
typedef struct node{
	data_t data;
	struct node *next;
}listnode,*lkstack;

创建与销毁

编写sqstack.h
在这里插入图片描述

编写创建函数
在这里插入图片描述

lkstack lkstack_create(){
	lkstack s;
	if((s = (lkstack)malloc(sizeof(listnode))) == NULL){
		printf("malloc is failed\n");
		return NULL;
	}

	s->data = 0;
	s->next = NULL;

	return s;
}//创建表

编写销毁函数
在这里插入图片描述

lkstack lkstack_free(lkstack s){
	lkstack p;
	if(s = NULL){
		printf("s is NUll\n");
		return NULL;
	}

	while(s != NULL){
		p = s;
		s = s->next;
		free(p);
	}

	return NULL;
}//释放表

状态属性

编写sqstack.h
在这里插入图片描述

编写是否为空函数
在这里插入图片描述

int lkstack_isempty(lkstack s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	if(s->next == NULL){
		return 1;
	}else{
		return 0;
	}
}//表是否为空

编写栈顶函数
在这里插入图片描述

int lkstack_top(lkstack s){
	lkstack p;
	int num = 0;
	p = s;
	while(p->next != NULL){
		num++;
		p = p->next;
	}
	return num;
}//栈顶判断

入栈出栈

编写sqstack.h
在这里插入图片描述

编写表入栈函数
在这里插入图片描述

int lkstack_push(lkstack s,data_t value){
	lkstack p;
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if((p = (lkstack)malloc(sizeof(listnode))) == NULL){
		printf("malloc is failed\n");
		return -1;
	}
	p->data = value;
	p->next = s->next;
	s->next = p;

	return 0;
}//表入栈

编写表出栈函数
在这里插入图片描述

data_t lkstack_pop(lkstack s){
	lkstack p;
	data_t t;

	p = s->next;
	s->next = p->next;
	t = p->data;
	free(p);
	p = NULL;

	return t;
}//表出栈

查询操作

编写sqstack.h
在这里插入图片描述

编写显示所有内容
在这里插入图片描述

int lkstack_show(lkstack s){
	lkstack p;
	if(s == NULL){
		printf("s is NULL");
		return -1;
	}
	if(lkstack_isempty(s)){
		printf("lkstack is empty\n");
	}

	p = s;
	printf("lkstack:\n");
	while(p->next != NULL){
		printf("%d  ",p->next->data);
		p = p->next;
	}
	printf("\n");

}//查询并显示所有表内容

编写test.c
在这里插入图片描述

#include <stdio.h>
#include "lkstack.h"

int pushpop_test(){
	lkstack s;
	int value;
	s = lkstack_create();
	if(s == NULL){
		return -1;
	}

	while(1){
		printf("input(-1quit):");
		scanf("%d",&value);
		if(value == -1){
			break;
		}else{
			lkstack_push(s,value);
			lkstack_show(s);
		}
	}	

	while(!lkstack_isempty(s)){
		lkstack_pop(s);
		lkstack_show(s);
	}
} 




int main(int argc, const char *argv[])
{
	pushpop_test();
	return 0;
}

测试程序
在这里插入图片描述

更多内容

数据结构 P1简介与分类

数据结构 P2线性顺序表

数据结构 P3线性单链表

数据结构 P4 栈

数据结构 P5 队列

数据结构 P6 树和二叉树

数据结构 P7 基础查找

数据结构 P8 哈希表

数据结构 P9 基础排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CagePan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值