顺序栈的基本操作
1、定义:
- 作为一种限定性线性表,是将线性表的插入和删除运算限制为仅在表的一端进行。(先进后出)
2、栈的存储结构。
- 顺序栈:用顺序储存结构实现的栈。是利用一组地址连续的储存单元依次存放自栈底到栈顶的数据元素,同时由于栈的操作的特殊性,还必须附设一个位置的指针top来动态的指示栈顶元素在顺序栈中的位置。
3、结构示意图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/675e8fea7a732366ed214c722fd81f73.png)
4、*&解释
&是C++中的引用,可以直接改变原地址中的值。
*表示指针,表示地址传递
5、基本操作实现
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int ElementType;
typedef struct SeqStack
{
ElementType top;
ElementType elem[MAXSIZE];
} SeqStack;
void InitStack(SeqStack *&s)
{
s = (SeqStack *)malloc(sizeof(SeqStack));
s->top = -1;
}
void DestoryStack(SeqStack *&s)
{
free(s);
}
bool IsEmpty(SeqStack *s)
{
return (s->top == -1);
}
bool Push(SeqStack *&s,ElementType e)
{
if (s->top ==(MAXSIZE-1))
{
return false;
}
s->top++;
s->elem[s->top] = e;
return true;
}
bool Pop(SeqStack *&s , ElementType &e)
{
if (s->top == -1)
{
return false;
}
e = s->elem[s->top];
s->top--;
return true;
}
bool GetTop(SeqStack *s ,ElementType &e)
{
if (s->top == -1)
{
return false;
}
e = s->elem[s->top];
return true;
}
void conversion(int n)
{
SeqStack*s ;
InitStack(s);
int x;
while (n)
{
Push(s,n%2);
n = n/2;
}
while (!IsEmpty(s))
{
Pop(s,x);
printf("%d",x);
}
}
int main()
{
SeqStack *s ;
InitStack(s);
char x ;
Push(s,'A');
Push(s,'B');
Push(s,'C');
Pop(s,x);
printf("%c",x);
Push(s,'D');
Pop(s,x);
printf("%c",x);
Pop(s,x);
printf("%c",x);
Pop(s,x);
printf("%c",x);
conversion(67);
return 0;
}