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