11. 栈的概念与实现-顺序栈与链式栈

1. 栈

栈是限制在一端进行插入操作和删除操作的线性表(俗称堆栈),允许进行操作的一端称为“栈顶”,另一固定端称为“栈底”,当栈中没有元素时称为“空栈”。特点 :后进先出(LIFO)

2. 顺序栈

它是顺序表的一种,具有顺序表同样的存储结构,由数组定义,配合用数组下标表示的栈顶指针top(相对指针)完成各种操作。
结构体说明

typedef  int  data_t ; /*定义栈中数据元素的数据类型*/
typedef struct 
{	
       data_t  *data ; 	/*用指针指向栈的存储空间*/
       int  maxlen;	/*当前栈的最大元素个数*/
       int  top ; 	/*指示栈顶位置(数组下标)的变量*/
} sqstack; 		/*顺序栈类型定义*/

顺序栈实现框架
sqstack.h

typedef int data_t;

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

sqstack * stack_create(int len);// 创建栈,len代表最大元素个数
int stack_push(sqstack *s,data_t value);// 入栈
data_t stack_pop(sqstack *s); // 出栈
data_t stack_top(sqstack *s); // 栈顶元素
int stack_clear(sqstack *s); // 栈清空
int stack_free(sqstack *s); // 栈销毁
int stack_empty(sqstack *s); // 判读空栈
int stack_full(sqstack *s); // 判读栈满

sqstack.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqstack.h"


sqstack * stack_create(int len)// 创建栈,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");
		free(s);
		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;
}

/*
 *@ret 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+1 还是在栈中,并没有释放

}

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;
}

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;
}

int stack_empty(sqstack *s) // 判读空栈
{
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}

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

}

test.c

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

int main(int argc, const char *argv[])
{
	sqstack *s;
	
	s = stack_create(50);
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}

	stack_push(s,10);
	stack_push(s,20);
	stack_push(s,30);
	stack_push(s,40);
	stack_push(s,50);
	stack_push(s,100);

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

	stack_free(s);

	return 0;
}

3. 链式栈

插入操作和删除操作均在链表头部进行,链表尾部就是栈底,栈顶指针就是头指针
在这里插入图片描述

结构体说明

typedef  int  data_t ;	 /*定义栈中数据元素数据类型*/ 
typedef  struct  node_t { 
data_t data ;		   /*数据域*/ 
struct  node_t  *next ;   /*链接指针域*/ 
}  linkstack_t ; 		  /*链栈类型定义*/

实现框架
linkstack.h

typedef int data_t;

typedef struct node{
	data_t data;
	struct node *next;
}listnode,*linkstack;

linkstack stack_create();  // 创建
int stack_push(linkstack s,data_t value);// 入栈
data_t stack_pop(linkstack s); // 出栈
int stack_empty(linkstack s); // 判断空栈
data_t stack_top(linkstack s); // 栈顶元素
linkstack stack_free(linkstack s); // 是否栈

linkstack.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

linkstack stack_create()
{
	linkstack s;

	s = (linkstack)malloc(sizeof(listnode));
	if(s == NULL){
		printf("malloc failed\n");
		return NULL;
	}
	s->data = 0;
	s->next = NULL;

	return s;

}

int stack_push(linkstack s,data_t value)
{
	linkstack p;

	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}	

	p = (linkstack)malloc(sizeof(listnode));
	if(p == NULL){
		printf("malloc failed\n");
		return -1;
	}

	p->data = value;
	p->next = NULL;

	p->next = s->next;
	s->next = p;

	return 0;

}

data_t stack_pop(linkstack s)
{
	linkstack p;
	data_t t;

	p = s->next;
	s->next = p->next;

	t = p->data;

	//注意释放
	free(p);

	return t;
}

int stack_empty(linkstack s)
{
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}

	return (s->next == NULL ? 1 : 0);
}

data_t stack_top(linkstack s)
{
	return (s->next->data);
}

linkstack stack_free(linkstack s)
{
	linkstack p;

	if(s == NULL){
		printf("s is NULL\n");
		return NULL;
	}

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

	return NULL;
}

test.c

#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"

int main(int argc, const char *argv[])
{
	linkstack s;

	s = stack_create();
	if (s == NULL) 
		return -1;

	stack_push(s, 10);
	stack_push(s, 20);
	stack_push(s, 30);
	stack_push(s, 40);

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

	s = stack_free(s);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值