链式栈的存储形式
栈的链式存储结构:
栈的链式存储结构,简称为链栈;
栈顶放在单链表的头部;
链栈是不需要头结点的。
链栈不存在栈满的情况。
头文件
#ifndef LINKSTACK_H
#define LINKSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
struct Node //表示一个结点的信息
{
int data;
struct Node *next;
};
typedef struct Node node;
struct StackInfo //表示栈的信息
{
int length; //栈的长度
node *top; //栈顶指针 ,通过这个指针把两个结构体联系在一起。
用指针top指向结构体node,可以访问或者修改结构体里面的信息。
};
typedef struct StackInfo Stack;
int InitStack(Stack **s);
int push(Stack *s, int num);
int EmptyStack(Stack *s);
int GetTop(Stack *s);
int pop(Stack *s);
int ClearStack(Stack *s);
int DestroyStack(Stack **s);
#endif
调用子函数,函数文件
#include "LinkStack.h"
/*
函数描述:栈空间的初始化
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int InitStack(Stack **s)
{
if (NULL == s)
return FAILURE;
*s = (Stack *)malloc(sizeof(Stack) * 1);
if (NULL == *s)
{
return FAILURE;
}
(*s)->top = NULL;
(*s)->length = 0; //长度初始化
return SUCCESS;
}
/*
函数描述:元素进栈
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int push(Stack *s, int num)
{
if (NULL == s)
return FAILURE;
node *n = (node *)malloc(sizeof(node)); //给存储元素的结点申请空间
if (NULL == n)
{
return FAILURE;
}
n->data = num;
n->next = s->top; //把栈顶指针赋予为下一个结点 让下一个结点指向下面的结点。
s->top = n; //把新结点赋予给栈顶指针 ,相当于栈顶指针向上移动。
s->length++;
return SUCCESS;
}
/*
函数描述:判断栈是否为空
函数参数:栈空间的地址
函数返回值:空返回TRUE,不空返回FALSE
*/
int EmptyStack(Stack *s)
{
if (NULL == s)
return FAILURE;
return (s->top == NULL) ? TRUE : FALSE;
}
/*
函数描述:获取栈顶元素
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回栈顶元素
*/
int GetTop(Stack *s)
{
if (NULL == s)
return FAILURE;
return (s->top == NULL) ? FAILURE : (s->top->data);
}
/*
函数描述:把链式栈内的元素出栈
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回出栈的元素
*/
int pop(Stack *s)
{
if (NULL == s)
return FAILURE;
if (NULL == s->top) //空栈
return FAILURE;
int e = s->top->data;
node *n = s->top;
s->top = n->next; //相当于栈顶指针向下移动一位,指向下面的元素
free(n);
return e;
}
/*
函数描述:清空链式栈
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int ClearStack(Stack *s)
{
if (NULL == s)
return FAILURE;
while (s->top)
{
node *n = s->top;
s->top = n->next;
free(n);
}
return SUCCESS;
}
/*
函数描述:清除链式表
函数参数:栈空间的地址
函数返回值:失败返回FAILURE,成功返回SUCCESS
*/
int DestroyStack(Stack **s)
{
if (NULL == s)
return FAILURE;
free(*s);
*s = NULL;
return SUCCESS;
}
主函数执行文件
#include "LinkStack.h"
int main()
srand(time(NULL));
Stack *s = NULL;
int ret;
ret = InitStack(&s);
if (SUCCESS == ret)
{
printf("初始化成功!\n");
}
else
{
printf("初始化失败!\n");
}
int i, num;
for (i = 0; i < 10; i++)
{
num = rand() % 20;
ret = push(s, num);
if (FAILURE == ret)
{
printf("%d进栈失败\n", num);
}
else
{
printf("%d进栈成功!\n", num);
}
}
ret = EmptyStack(s);
if (TRUE == ret)
{
printf("栈是空\n");
}
else
{
printf("栈不是空\n");
}
ret = GetTop(s);
if (FAILURE == ret)
{
printf("没有栈顶元素\n");
}
else
{
printf("栈顶元素是%d\n", ret);
}
for (i = 0; i < 5; i++)
{
ret = pop(s);
if (FAILURE == ret)
{
printf("出栈失败!\n");
}
else
{
printf("%d出栈成功!\n", ret);
}
}
ret = ClearStack(s);
if (SUCCESS == ret)
{
printf("清空成功!\n");
}
else
{
printf("清空失败!\n");
}
ret = EmptyStack(s);
if (TRUE == ret)
{
printf("栈是空\n");
}
else
{
printf("栈不是空\n");
}
ret = DestroyStack(&s);
if (SUCCESS == ret)
{
printf("销毁成功!\n");
}
else
{
printf("销毁失败!\n");
}
for (i = 0; i < 10; i++)
{
num = rand() % 20;
ret = push(s, num);
if (FAILURE == ret)
{
printf("%d进栈失败\n", num);
}
else
{
printf("%d进栈成功!\n", num);
}
}
return 0;
}