小猪的数据结构辅助教程——3.2 栈与队列中的链栈
标签(空格分隔): 数据结构
1.本节引言:
嗯,本节没有学习路线图哈,因为栈我们一般都用的是顺序栈,链栈还是顺带提一提吧,
栈因为只是栈顶来做插入和删除操作,所以较好的方法是将栈顶放在单链表的头部,栈顶
指针与单链表的头指针合二为一~所以本节只是讲下链栈的存储结构和基本操作!
2.链栈的存储结构与示意图
存储结构:
typedef struct StackNode
{
SElemType data; //存放的数据
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top; //Top指针
int count; //栈元素计数器
}LinkStack;
示意图:
由上图可以发现一点,链栈不会出现栈满的情况…
3.链栈基本操作的代码实现
代码实现:
#include <stdio.h>
#define STACK_INIT_SIZE 10 //存储空间的初始分配量
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int SElemType;
//存储结构
typedef struct StackNode
{
SElemType data; //存放的数据
struct StackNode *next;
}StackNode,*LinkStackPtr;
typedef struct LinkStack
{
LinkStackPtr top; //Top指针
int count; //栈元素计数器
}LinkStack;
//1.构建一个空栈
Status InitStack(LinkStack *S)
{
S ->top = (LinkStackPtr)malloc(sizeof(StackNode));
if(!S ->top)return ERROR;
S ->top = NULL;
S ->count = 0;
return OK;
}
//2.将栈置空
Status ClearStack(LinkStack *S)
{
LinkStackPtr p,q;
p = S->top;
while(p)
{
q = p;
p = p ->next;
free(q);
}
S ->count = 0;
return OK;
}
//3.判断栈是否为空
Status StackEmpty(LinkStack S)
{
return S.count == 0?TRUE:FALSE;
}
//4.获得栈中的元素个数
int StackLength(LinkStack S)
{
return S.count;
}
//5.获得栈顶元素
Status GetTop(LinkStack *S,SElemType *e)
{
LinkStackPtr p;
if(StackEmpty(*S))return ERROR;
*e = S ->top->data;
p = S ->top;
return OK;
}
//6. 往链栈中插入元素(入栈)
Status PushStack(LinkStack *S,SElemType e)
{
LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode));
if(!s)return ERROR;
s ->data = e;
s ->next = S ->top;
S ->top = s;
S ->count++;
return OK;
}
//7.删除栈顶元素*出栈(
Status PopStack(LinkStack *S,SElemType *e)
{
LinkStackPtr p;
if(StackEmpty(*S))return ERROR;
* e = S ->top ->data;
p = S ->top; //获取栈顶结点
S ->top = S ->top ->next; //栈顶指针下移一位
free(p); //释放结点p
S ->count--;
return OK;
}
//8.遍历栈
Status StackTraverse(LinkStack S)
{
LinkStackPtr p;
p = S.top;
while(p)
{
visit(p->data);
p=p->next;
}
printf("\n");
return OK;
}
//定义一个打印元素的方法
Status visit(SElemType c)
{
printf("%d ",c);
return OK;
}
int main()
{
int i,e;
LinkStack s;
InitStack(&s);
printf("初始化链栈,接着插入10个元素~\n");
for(i = 1;i <= 10;i++)
{
PushStack(&s,i);
}
printf("此时栈中的元素依次为:\n");
StackTraverse(s);
printf("此时栈中有%d个元素\n",StackLength(s));
PopStack(&s,&e);
printf("出栈,出栈结点为:%d\n",e);
printf("此时栈中的元素依次为:\n ");
StackTraverse(s);
ClearStack(&s);
printf("清空栈,此时栈中有%d个元素!\n\n",StackLength(s));
return 0;
}
运行结果:
代码比较简单,就不解释了~
4.本节代码下载:
https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/Stack/Stack3.c