2018.08.08

 

链式栈是一种数据存储结构,可以通过单链表的方式来实现,使用链式栈的优点在于它能够克服用数组实现的顺序栈空间利用率不高的特点,需要为每个栈元素分配额外的指针空间用来存放指针域。

头文件:

#ifndef _SEQUENCE_H
#define _SEQUENCE_H

#define SUCCESS 10000
#define FAILURE 10001
#define TRUE    10002
#define FALSE   10003

typedef int ElemType;

struct node                   //需要定义两个结构体,一个存放结点信息,一个存放栈信息
{
	int data;
	struct node *next;
};
typedef struct node Node;

struct stack
{
	int count;
	Node *top;

};
typedef struct stack Stack;
int sequencelinkinit(Stack **s);
int sequencelinkempty(Stack *s);
int push(Stack **s, ElemType e);
int gettop(Stack *s);
int pop(Stack **s);
int clear(Stack **s);
int destroy(Stack **s);
#endif

 主函数:

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

int main()
{
	Stack *list;
	int ret, i;

	ret = sequencelinkinit(&list);
	if(ret == SUCCESS)
	{
		printf("init success!\n");
	}
	else
	{
		printf("init failue!\n");
	}
	
	ret = sequencelinkempty(list);
	if(ret == FALSE)
	{
		printf("sequencelink is not empty!\n");
	}
	else
	{
		printf("sequenclink is empty!\n");
	}
	
	for(i = 0; i < 10; i++)
	{
		ret = push(&list, i);
		if(ret == FAILURE)
		{
			printf("push failure!\n");
		}
		else
		{
			printf("push success!\n");
		}
	}
	ret = sequencelinkempty(list);
	if(ret == FALSE)
	{
		printf("sequencelink is not empty!\n");
	}
	else
	{
		printf("sequenclink is empty!\n");
	}
	
	ret = gettop(list);
	if(ret == FAILURE)
	{
		printf("Get Top Failure!\n");
	}
	else
	{
		printf("Get Top %d success!\n", ret);
	}
	for(i = 0; i < 5; i++)
	{
		ret = pop(&list);
		if(ret == FAILURE)
		{
			printf("pop failure!\n");
		}
		else
		{
			printf("pop %d success!\n",ret);
		}
	}
		
	ret = gettop(list);
	if(ret == FAILURE)
	{
		printf("Get Top Failure!\n");
	}
	else
	{
		printf("Get Top %d success!\n", ret);
	}
	
	ret = clear(&list);
	if(ret == SUCCESS)
	{
		printf("Clear Succes!\n");
	}
	else
	{
		printf("Clear Failure!\n");
	}
		
	ret = gettop(list);
	if(ret == FAILURE)
	{
		printf("Get Top Failure!\n");
	}
	else
	{
		printf("Get Top %d success!\n", ret);
	}
	
	/*ret = sequencelinkempty(list);
	if(ret == FALSE)
	{
		printf("sequencelink is not empty!\n");
	}
	else
	{
		printf("sequenclink is empty!\n");
	}*/
	ret = destroy(&list);
	if(ret == SUCCESS)
	{
		printf("destroy success!\n");
	}
	else
	{
		printf("destroy failure!\n");
	}
	
	for(i = 0; i < 10; i++)
	{
		ret = push(&list, i);
		if(ret == FAILURE)
		{
			printf("push failure!\n");
		}
		else
		{
			printf("push success!\n");
		}
	}

	return 0;
}

 子函数功能:

#include "sequencelink.h"
#include <stdlib.h>

int sequencelinkinit(Stack **s)                //由于栈中的链式结构并没有头结点,所以
{                                              //不需要额外的malloc空间给结点,只需一次	  

                                               //malloc就行
    if(s == NULL)                                      
	{
		return FAILURE;
	}
	(*s) = (Stack *)malloc(sizeof(Stack) * 1);
	if((*s) == NULL)
	{
		return FAILURE;
	}
	(*s)->top = NULL;
	(*s)->count = 0;
	return SUCCESS;
}
int sequencelinkempty(Stack *s)
{
	if(s == NULL)
	{
		return FAILURE;
	}
	return (s->top == NULL) ? TRUE:FALSE;
}
int push(Stack **s, ElemType e)
{
	if(s == NULL || (*s) == NULL)
	{
		return FAILURE;
	}
	Node *p = (Node *)malloc(sizeof(Node));
	if(p == NULL)
	{
		return FAILURE;
	}
	p->data = e;
	p->next = (*s)->top;
	(*s)->top = p;
	return SUCCESS;
}
int gettop(Stack *s)
{
	if(s == NULL || s->top == NULL)
	{
		return FAILURE;
	}
	return s->top->data;	
}

int pop(Stack  **s)
{
	if(s == NULL || (*s) == NULL)
	{
		return FAILURE;
	}
	ElemType e;
	Node *q = (*s)->top;
	e = q->data;
	(*s)->top = q->next;
	(*s)->count--;
	free(q);
	return e;
}
int clear(Stack **s)                           //清空时,不同于顺序栈的清空,顺序栈只需要
{                                              //令栈顶指针(下标)为-1即可。链式栈需要
	if(*s == NULL || s == NULL)                //释放全部的结点
	{
		return FAILURE;
	}
	Node *p;
	while((*s)->top)
	{
		p = (*s)->top;
		(*s)->top = p->next;
		free(p);
		(*s)->count--;
	}
	return SUCCESS;
}
int destroy(Stack **s)                  //在清空的前提下,释放结构体所占内存,即可销毁链式
{                                        //栈
	if((*s) == NULL || s == NULL)
	{
		return FAILURE;
	}
	free(*s);
	(*s) = NULL;
	return SUCCESS;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值