栈的顺序表和单链表的实现

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

顺序栈

#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int data_t;
typedef struct node
{
	data_t *data;
	int maxlen;
	int top;
}stack_lk,*stack_plk;
extern void init_linkstack(stack_plk *H,int N);
extern void push_linkstack(stack_plk p,data_t val);
extern void pop_linkstack(stack_plk p,data_t *val);
extern bool is_empty_linkstack(stack_plk p);
extern bool is_full_linkstack(stack_plk p);
extern void gettop_linkstack(stack_plk p,data_t *val);
extern void clean_linkstack(stack_plk p);
#endif


#include "seqstack.h"
void init_linkstack(stack_plk *H,int N)
{
	*H = (stack_plk)malloc(sizeof(stack_lk));
	if(NULL == *H)
	{
		perror("Malloc failed");
		exit(-1);
	}
	(*H)->maxlen = N;
	(*H)->top = -1;
	(*H)->data = (data_t *)malloc(N * sizeof(data_t));
}
void push_linkstack(stack_plk p,data_t val)
{
	if(is_full_linkstack(p))
	{
		printf("The stack is full\n");
		return ;
	}
	p->top++;
	p->data[p->top] = val;
	return ;
}
void pop_linkstack(stack_plk p,data_t *val)
{
	if(is_empty_linkstack(p))
	{
		printf("The stack is empty\n");
		return ;
	}
	*val = p->data[p->top];
	p->top--;
}
bool is_empty_linkstack(stack_plk p)
{
	if(p->top == -1)
		return true;
	else
		return false;
}
bool is_full_linkstack(stack_plk p)
{
	if(p->top == p->maxlen-1)
		return true;
	else
		return false;
}
void gettop_linkstack(stack_plk p,data_t *val)
{
	*val = p->data[p->top];
}
void clean_linkstack(stack_plk p)
{
	free(p->data);
	free(p);
}


#include "seqstack.h"

int main(int argc,const char *argv[])
{
	stack_plk H;
	data_t val;
	init_linkstack(&H,10);
	push_linkstack(H,10);
	push_linkstack(H,20);
	push_linkstack(H,30);
	push_linkstack(H,40);
	push_linkstack(H,50);
	
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);

	clean_linkstack(H);
	return 0;
}

链式栈

#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int data_t;
typedef struct stack_node
{
	data_t data;
	struct stack_node *next;
}stack_lk,*stack_plk;
extern void init_linkstack(stack_plk *H);
extern void push_linkstack(stack_plk p,data_t val);
extern void pop_linkstack(stack_plk p,data_t *val);
extern bool is_empty_linkstack(stack_plk p);
extern void top_linkstack(stack_plk p,data_t *val);
extern void clear_linkstack(stack_plk p);
extern void free_linkstack(stack_plk p);
#endif


#include "linkstack.h"
void init_linkstack(stack_plk *H)
{
	*H = (stack_plk)malloc(sizeof(stack_lk));
	if(NULL == *H)
	{
		perror("Malloc failed");
		exit(-1);
	}
	(*H)->next = NULL;
	(*H)->data = 0;
}
void push_linkstack(stack_plk p,data_t val)
{
	stack_plk q;
	q = (stack_plk)malloc(sizeof(stack_lk));
	if(NULL == p)
	{
		perror("Malloc failed");
		exit(-1);
	}
	q->data = val;
	q->next = p->next;
	p->next = q;
}
void pop_linkstack(stack_plk p,data_t *val)
{
	stack_plk q;
	if(is_empty_linkstack(p))
	{
		printf("Stacklink is empty\n");
		*val = 999999999;
		return ;
	}
	q = p->next;
	*val = q->data;
	p->next = q->next;
	free(q);
}
bool is_empty_linkstack(stack_plk p)
{
	if(NULL == p->next)
		return true;
	else
		return false;
}
void top_linkstack(stack_plk p,data_t *val)
{
	*val = p->next->data;
}
void clear_linkstack(stack_plk p)
{
	stack_plk q;
	q = p->next;
	while(q)
	{
		p->next = q->next;
		printf("%d \n",q->data);
		free(q);
		q = p->next;
	}
}
void free_linkstack(stack_plk p)
{
	stack_plk q;
	while(p)
	{
		q = p->next;
		printf("%d \n",p->data);
		free(p);
		p = q;
	}
}


#include "linkstack.h"

int main(int argc, const char *argv[])
{
	stack_plk H;
	data_t val;
	init_linkstack(&H);
	push_linkstack(H,10);
	push_linkstack(H,20);
	push_linkstack(H,30);
	push_linkstack(H,40);
	push_linkstack(H,50);

	clear_linkstack(H);
	
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);
	pop_linkstack(H,&val);
	printf("%d \n",val);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值