数据结构——栈

【申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com
 
 
栈(stack) 是限定仅在表位进行插入或删除操作的线性表。因此,对栈来说,表尾端有其特殊含义,称为栈定(top),

响应地,表头端称为栈底。不含元素的空表称为栈空。栈特点:先进先出。栈的实现方法有很多种,可以采用静态数组、

链表等。

 

参考c和c++书中代码写了下面的代码:

 

一、采用静态数组实现

stak.h

/*
**一个堆栈模块的接口
*/
#define STACK_TYPE int  /*堆栈所存储值的类型*/

/*
** push
**把一个新值压入到堆栈中。它的参数是需要被压入的值
*/
void push(STACK_TYPE value);

/*
** pop
**从堆栈弹出一个值,并将其丢弃
*/
void pop(void);

/*
** top
**返回堆栈顶部元素的值,但不对堆栈进行修改
*/
STACK_TYPE top(void);

/*
** is_empty
** 如果堆栈为空,返回TRUE,否则返回FLASE
*/
int is_empty(void);

/*
** is_full
** 如果堆栈已满,返回TRUE,否则返回FLASE
*/
int is_full(void);

/*
** 打印全部堆栈数据
*/
void print_value(void);

stak.c

/*
** 采用静态数组实现,最大值为100
*/

#include "stack.h"
#include <assert.h>
#include <stdio.h>

#define   STACK_SIZE    100 /*堆栈中值数量的最大限制*/

/*
**存储堆栈中值的数组和一个指向堆栈顶部元素的指针
*/
static  STACK_TYPE  stack[STACK_SIZE];
static  int         top_element = -1;

/*
** push
*/
void push(STACK_TYPE value)
{
	if(is_full())
	{
		printf("the stack is full!\n");
	}
	top_element += 1;
	stack[top_element] = value;
}

/*
** pop
*/
void pop(void)
{
	if(is_empty())
	{
		printf("the stack is empty!\n");
	}
	top_element -= 1;
}

/*
** top
*/
STACK_TYPE top(void)
{
	if(is_empty())
	{
		printf("the stack is empty!\n");
	}
	return stack[top_element];
}

/*
** is_empty
*/
int is_empty(void)
{
	return top_element == -1;
}

/*
** is_full
*/
int is_full(void)
{
	return top_element == STACK_SIZE-1;
}

/*
** print all value
*/
void print_value(void)
{
	int i=0;
	if(is_empty())
	{
		printf("the stack is empty!\n");
	}
	for(i=0; i<top_element+1; i++)
		printf("%d  ", stack[i]);
		printf("\n");
}

int main()
{
	push(10);
	push(20);
	push(30);
	print_value();
	printf("%d\n", top());
	pop();
	print_value();
	pop();
	print_value();
	pop();
	print_value();
	return 0;
}


二、采用链表实现

Link_Stack.c

/*
** 一个用链表实现的堆栈。这个堆栈没有长度限制
*/

#include <stack.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>

#define 	FALSE     0

typedef struct STACK_NODE{
		STACK_TYPE  value;
		struct STACK_NODE *next;
}StackNode;
/*
** 指向堆栈中第一个节点的指针
*/
static StackNode *stack;

/*
** create_stack
*/
void create_stack(size_t size)
{

}

/*
** destroy_stack
*/
void destroy_stack(void)
{
	while(!is_empty())
	pop();
}

/*
** push
*/
void push(STACK_NODE value)
{
	StackNode *new_node;
	new_node = malloc(sizeof(StackNode));
	assert(new_node != NULL);
	new_node->value = value;
	new_node->next = stack;
	stack = new_node;
}

/*
** pop
*/
void pop(void)
{
	StackNode *first_node;
	
	assert(!is_empty());
	first_node = stack;
	stack = first_node->next;
	free(first_node);
}

/*
** is_empty
*/
int is_empty(void)
{
	return stack == NULL;
}

/*
** is_full
*/
int is_full(void)
{
	return FALSE;
}


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值