数据结构day7栈-顺序栈的实现

用指针比用数组好,这样用户可以自己指定空间的大小,有参与感。

全部代码:

main.c

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include "sqstack.h"


int main(int argc, char *argv[])
{
	sqstack *s;
	int i;

	s = stack_create(100);

	if(s == NULL)
		return -1;

	stack_push(s,100);
	stack_push(s,90);
	stack_push(s,80);
	stack_push(s,70);
	stack_push(s,60);
	
	for(i = 0; i <= s->top; i++)//显示栈中的元素
	{
	printf("%d\n", s->data[i]);
	}

	while(!stack_empty(s)) {
		printf("pop : %d\n",stack_pop(s));
	}

	stack_free(s);


	return 0;
}

sqstack.c

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
#include <string.h>

/* 要创建一个结构体的栈,和一个放数据的栈  */
sqstack * stack_create(int len){
	sqstack *s;

	if((s = (sqstack *)malloc(sizeof(sqstack))) == NULL) {
		printf("malloc sqstack failed\n");
		return NULL;

	}

	if(((s->data) = (data_t *)malloc(len * sizeof(data_t))) == NULL) {

		printf("malloc data failed\n");
		return NULL;
	}

	memset(s->data, 0, len * sizeof(data_t));
	s->maxlen = len;
	s->top = -1;
	return s;


}
int stack_push(sqstack *s, data_t value){
	if(s == NULL) {
		printf("s is NULL\n");
		return -1;

	}

	if(s->top == s->maxlen -1) {
		printf("stack is full\n");
	return -1;
	}

	s->top ++;
	s->data[s->top] = value;
	return 0;

}
/* 1-empty  */
int stack_empty(sqstack *s){
	if(s == NULL) {
		printf("s is NULL\n ");
		return -1;
	}
	return (s->top == -1 ? 1 : 0);


}
/* 1-full  */
int stack_full(sqstack *s){
	if(s == NULL) {
		printf("s is NULL\n");
		return -1;
	}

	return (s->top == s->maxlen - 1 ? 1 : 0);

}
data_t stack_pop(sqstack *s){
	s->top--;
	return (s->data[s->top+1]);

}

//显示top的值
data_t stack_top(sqstack *s){
	return (s->data[s->top]);

}


int stack_clear(sqstack *s){
	if(s == NULL)
	{
		printf("s is NULL\n");
		return -1;
	}
	
	s->top = -1;
	return 0;

}


//有几个malloc就有几个free

int stack_free(sqstack *s){
	if(s == NULL)
	{
		printf("s is NULL\n");
		return -1;
	}	
	if(s->data != NULL)//开辟栈可能失败
	{
		free(s->data);
	}
	free(s);
	return 0;
}

sqstack.h

cpp 复制代码
typedef int data_t;

typedef	struct{
	data_t *data;
	int maxlen;
	int top;

}sqstack;


sqstack * stack_create(int len);
int stack_push(sqstack *s, data_t value);
int stack_empty(sqstack *s);
int stack_full(sqstack *s);
data_t stack_pop(sqstack *s);
data_t stack_top(sqstack *s);
int stack_clear(sqstack *s);
int stack_free(sqstack *s);

运行结果

相关推荐
梦帮科技4 小时前
AI 音乐产品的发布工程:验证门、数据发布、回滚与生产运维纪律
数据结构·数据库·架构·node.js·音视频·动态规划·推荐算法
C++ 老炮儿的技术栈5 小时前
我们在设计tcp协议时,要传一个字符串过去,报文:头十长度十内容,是否要把‘\0‘也填入,长度是否包含‘\0‘
开发语言·数据结构·c++·mfc·c
青山木9 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
爱吃苹果的日记本11 小时前
数据结构第五课
数据结构·学习
Logic10112 小时前
C语言/数据结构贪心算法题解:买卖股票的最佳时机——一次交易最大利润(O(n)时间O(1)空间)
c语言·数据结构·贪心算法·数组·时间复杂度·算法题·股票交易
Logic10112 小时前
C语言/数据结构位运算题解:异或XOR找出货币交易中的“独特面值“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
ctlover12 小时前
数据结构:树
数据结构·python
景熙552314 小时前
14.Java 集合框架从入门到源码万字详解(含 ArrayList/LinkedList/HashMap 源码、泛型通配符、红黑树)
java·开发语言·数据结构·算法
SendTomo15 小时前
.ico透明图标在线生成下载平台深度解析
大数据·数据结构·数据库·数据仓库·个人开发
GG-_-Bond15 小时前
9.1kv存储持久化模拟面试
linux·c语言·数据结构·c++