栈的创建和基本操作
栈的创建一般有顺序栈和链栈,下面我将给大家一一介绍
顺序栈:顾名思义就是在数组中存储的栈,在内存中占据一块连续的内存空间
顺序栈代码实现:
#include <stdio.h>
#include <stdbool.h>
#define MaxSize 10 //假设顺序栈的最大长度是10
typedef struct Sqstack
{
int data[MaxSize];
int top;//指向栈顶
}Sqstack;
bool InitList(Sqstack* S)
{
S->top = 0;//指向栈底
return true;
}
bool Push(Sqstack* S)//进栈
{
if (S->top == MaxSize)//满栈,第一次满栈插入不了任何数据返回false
return false;
int i;
scanf("%d", &i);
while (i != 9999)//假设输出9999退出
{
if (S->top == MaxSize)//满栈了,第二次插入数据后满栈返回true
return true;
S->data[S->top] = i;
S->top++;
scanf("%d", &i);
}
return true;
}
bool Pop(Sqstack* S)//出栈
{
if (S->top == 0)//空栈
return false;
int n;
int i;
printf("请输入出栈个数:\n");
scanf("%d", &n);
if (n > S->top)//如果出栈个数大于总个数直接返回
return false;
for (i = 0; i < n; i++)
{
S->top--;//由于是静态数组存储无法手动释放内存
}
return true;
}
int GetTop(Sqstack S, int* x)//查找栈顶元素
{
if (S.top == 0)//空栈
{
printf("空栈\n");
return 0;
}
*x = S.data[S.top - 1];//必须减1
return *x;
}
bool destoryStack(Sqstack* S)
{
if (S->top == 0)//空栈
return false;
S->top = 0;//令栈顶为0
return true;
}
bool PrintStack(Sqstack S)//打印栈
{
if (S.top == 0)//空栈
return false;
int i;
for (i = 0; i < S.top; i++)
{
printf("%d ", S.data[i]);
}
return true;
}
int main()
{
Sqstack S;
InitList(&S);//初始化栈
Push(&S);//进栈
Pop(&S);//出栈
int x;
GetTop(S,&x);//查找栈顶指针
printf("栈顶指针为:%d\n", x);
int del;
printf("是否销毁栈:(0销毁,1不销毁)\n");
scanf("%d", &del);
destoryStack(&S);
PrintStack(S);//打印栈
return 0;
}
链栈:链栈是由链表来实现的一种栈操作,一般用不带头结点的单链表实现
链栈的代码实现:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct SNode
{
int data;//栈的数据
struct SNode* next;//栈里的下一个结点
}SNode,*LinkStack;
bool InitList(LinkStack* S)//初始化链表
{
(*S) = NULL;//表示当前栈为空
return true;
}
bool Push(LinkStack* S)//进栈
{
//第一次入栈
SNode* p = (SNode*)malloc(sizeof(SNode));
if (p == NULL)
return false;
int i;
scanf("%d", &i);
p->data = i;
p->next = NULL;
(*S) = p;
//后续入栈
scanf("%d", &i);//假设输入9999退出
while (i != 9999)
{
SNode* q = (SNode*)malloc(sizeof(SNode));
if (q == NULL)
return false;
q->data = i;
q->next = (*S);
(*S) = q;
scanf("%d", &i);
}
return true;
}
int Length(LinkStack S)//获取栈的长度
{
SNode* p = S;
int n = 0;
if (p == NULL)//空栈长度为0
return n;
while (p != NULL)
{
p = p->next;
n++;
}
return n;
}
bool Pop(LinkStack* S)//出栈
{
SNode* p = (*S);//指向栈顶
if (p == NULL)//空栈无法出栈
return false;
int n,i;
int len = Length(*S);//获取栈长度
printf("请输入要出栈几次\n");
scanf("%d", &n);
if (n > len)//出栈次数超过栈长度
return false;
for (i = 0; i < n; i++)
{
SNode* temp = p;
(*S) = (*S)->next;
free(temp);
p = (*S);
}
return true;
}
SNode* GetTop(LinkStack S)
{
SNode* p = S;
if (p == NULL)
return NULL;
return p;
}
bool Destory(LinkStack* S)
{
SNode* p = (*S);//指向栈顶
if (p == NULL)
return false;
while ((*S) != NULL)
{
SNode* temp = p;
(*S) = (*S)->next;
free(temp);
p = (*S);
}
return true;
}
bool PrintList(LinkStack S)//打印栈
{
if (S == NULL)//空栈
return false;
SNode* p = S;//指向栈顶
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
return true;
}
int main()
{
LinkStack S;
InitList(&S);//初始化
Push(&S);//进栈
Pop(&S);//出栈
//GetTop(S);//查找栈顶指针
//Destory(&S);//销毁栈
PrintList(S);//打印栈
return 0;
}