栈的相关基本操作
顺序栈的基本操作
两栈共享邻接空间
栈的链式存储
#顺序栈的基本操作
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//定义顺序栈
#define MAXSIZE 10
typedef int ElemType;
typedef struct
{
ElemType elem[MAXSIZE];
int top;
}SeqStack;
//置空栈
//首先建立栈空间,然后初始化栈顶指针
SeqStack * InitStack()
{
SeqStack *s;
s = (SeqStack*)malloc(sizeof(SeqStack));
s->top = -1;
return s;
}
//判栈空
int Empty(SeqStack* s)
{
if (s->top == -1)
return 1;
else
return 0;
}
//入栈
int Push(SeqStack* s, ElemType x)
{
if (s->top == MAXSIZE - 1)//栈满不能入栈,否则将造成上溢出
return 0;
else
{
s->top++;
s->elem[s->top] = x;
return 1;
}
}
//出栈
int Pop(SeqStack * s, ElemType *x)
{
if (Empty(s))
return 0;//栈空不能出栈
else
{
*x = s->elem[s->top];//栈顶元素存入*x,返回
s->top--;
return 1;
}
}
//取栈顶元素
ElemType GetTop(SeqStack * s)
{
if (Empty(s))
return 0;//栈空
else
return (s->elem[s->top]);
}
int main()
{
SeqStack* s = InitStack();
printf("%d", Empty(s));//1
int x1 = 1;
Push(s, x1);
int x2 = 2;
Push(s, x2);
printf("%d", Empty(s));//0
int x3;
Pop(s, &x3);
printf("%d", x3);//2
int t = GetTop(s);
printf("%d", t);//1
printf("%d", Empty(s));//0
}
#两栈共享
#include<stdio.h>
#include<stdlib.h>
//两栈共享
#define MAXSIZE 10
typedef int ElemType;
typedef struct
{
ElemType elem[MAXSIZE];
int lefttop;//左栈栈顶位置指示器
int righttop;//右栈栈顶位置指示器
}Dupsqstack;
//初始化
int InitDupstack(Dupsqstack* s)
{
//创建两个共享邻接空间的空栈由指针s指出
if ((s = (Dupsqstack*)malloc(sizeof(Dupsqstack))) == NULL)
return 0;
s->lefttop = -1;
s->righttop = MAXSIZE;
return 1;
}
//入栈
int PushDupStack(Dupsqstack* s, char status, ElemType x)
{
//把数据元素x压入左栈或右栈
if (s->lefttop + 1 == s->righttop)
return 0;//栈满
if (status == 'L')
s->elem[++s->lefttop] = x;//左栈进栈
if (status == 'R')
s->elem[--s->righttop] = x;//右栈进栈
else
return 0;//参数错误
return 1;
}
//出栈
ElemType PopDupStack(Dupsqstack* s, char status)
{
//从左栈(status='L')或右栈退出栈顶元素
if (status == 'L')
{
if (s->lefttop < 0)
return 0;//左栈为空
else
return (s->elem[s->lefttop--]);//左栈出栈
}
else if (status == 'R')
{
if (s->righttop > MAXSIZE - 1)
return 0;//右栈为空
else return (s->elem[s->righttop++]);//右栈出栈
}
else return 0;//参数错误
}
//取栈顶元素
ElemType GetDupsqTop(Dupsqstack* s, char status)
{
if (status == 'L')
{
if (s->lefttop < 0)
return 0;//左栈为空
else return (s->elem[s->lefttop]);//取左栈栈顶元素
}
else if (status == 'R')
{
if (s->righttop > MAXSIZE - 1)
return 0;//右栈为空
else return (s->elem[s->righttop]);//取右栈栈顶元素
}
}
int main()
{
Dupsqstack* d;
InitDupstack(&d);
int l0 = 1;
int r0 = 2;
PushDupStack(d, 'L', l0);
PushDupStack(d, 'R', r0);
int l = GetDupsqTop(d, 'L');
int r = GetDupsqTop(d, 'R');
printf("%d", l);
printf("%d", r);
}
#链栈的基本操作
//链栈的基本操作
#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
//定义链栈结点
typedef struct node
{
elemtype data;//数据域
struct node* next;//指针域
}node;
//初始化
node* Init()
{
node* l;
l = (node*)malloc(sizeof(node));
l->next = NULL;
return l;
}
//链栈的创建
void create(node* l)
{
node* p;//新建指针p
int i = 1;
int x = 1;
while (i != 0)
{
scanf("%d",&x);
if (x != -1)
{
p = (node*)malloc(sizeof(node));
p->data = x;
p->next = l->next;
l->next = p;//类似于头插法
}
else
{
i = 0;//输入-1结束
}
}
}
//进栈
void push(node* l, elemtype x)
{
node* p;//新建指针P
p = (node*)malloc(sizeof(node));
p->data = x;
p->next = l->next;//头插法进栈
l->next = p;
printf("元素%d进栈!\n", p->data);
}
//出栈
void pop(node* l)
{
node* p;
p = l->next;
if (p == NULL)
{
printf("该链栈为空,无法出栈\n");
}
else
{
int x;
x = p-> data;
printf("元素%d出栈!\n", p->data);
l->next = p->next;//删除结点p
free(p);
}
}
//链栈的打印
void print(node* l)
{
node* p;
p = l->next;
if (p == NULL)
printf("该链栈为空,无法打印!\n");
else
{
printf("该链栈的内容为:");
while (l->next != NULL)
{
printf("%d", l->next->data);
l = l->next;
}
printf("\n");
}
}
int main()
{
node* p;
p = Init();
printf("链栈初始化成功!\n");
printf("请输入新链栈的值:\n");
create(p);
print(p);
push(p, 1);
print(p);
pop(p);
print(p);
}
2021/12/1 第一篇博客,继续好好复习数据结构各个章节内容,坚持每天写代码!!!