顺序栈构造空栈、取栈顶元素、入栈、出栈
#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW 0
#define STACK_INIT_SIZE 100//存储空间初始分配增量
#define STACKINCREMRNT 10//存储空间分配增量
typedef int SElemType;
typedef int Status;
typedef struct {
SElemType *base;//在栈构造和销毁之后,base的值为NULL
//栈低指针,始终指向存储空间基址
SElemType *top;//栈顶指针,指向栈顶元素的下一个位置
int stacksize;//当前已分配的存储空间,以元素为单位。
//数组存储空间的长度。
}SqStack;
Status InitStack(SqStack *S, int n)
{
S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S->base)
exit(OVERFLOW);//存储分配失败
S->top = S->base;
S->stacksize = STACKINCREMRNT;
printf("输入栈中元素:");
for (int i = 0; i < n; i++)
{
scanf_s("%d", &*S->top);
S->top++;
}
return OK;
}
Status Show(SqStack S)
{
if (S.top == S.base)
{
printf("栈中无元素");
return ERROR;
}
SElemType*p;
p = S.top;
while (p > S.base)
{
p--;
printf("%5d", *p);
}
printf("\n");
}
Status Push(SqStack *S, SElemType e)
{//插入元素e为新的栈顶元素
if (S->top - S->base >= S->stacksize)
{//栈满,追加存储空间
S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMRNT) * sizeof(SElemType));
if (!S->base)
exit(OVERFLOW);//存储分配失败
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMRNT;
}
*S->top = e;
S->top++;
return OK;
}
SElemType Pop(SqStack *S)
{//出栈
SElemType e;
if (S->top == S->base)
return ERROR;
--S->top;
e = *S->top;
return e;
}
Status GetTop(SqStack S)
{//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
SElemType e;
if (S.top == S.base)
return ERROR;
e = *(S.top - 1);
printf("%d", e);//显示栈顶元素
return OK;
}
int main()
{
SqStack S;
SElemType e;
int n;
printf("输入你要建立的栈中元素的个数:");
scanf_s("%d", &n);
InitStack(&S, n);
printf("栈中元素是:");
Show(S);
printf("栈顶元素是:");
GetTop(S);
printf("\n");
printf("请输入你要入栈的元素:");
scanf_s("%d", &e);
Push(&S, e);
printf("入栈后:");
Show(S);
Pop(&S);
printf("出栈后:");
Show(S);
system("pause");
return 0;
}
链栈构造空栈、取栈顶元素、入栈、出栈
带头结点的单链表实现
链栈4要素:
1.栈空条件:S->next==NULL
2.栈满条件:不考虑
3.进栈e操作: 将包含e的结点插入到头结点之后
4.出栈操作:取出头结点之后结点的元素并删除
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW 0
typedef int Status;
typedef int SElemType;
typedef struct linknode {
SElemType data;//数据域
struct linknode*next;//指针域
}LiStack;
LiStack* InitStack(int n)
{
LiStack *S;
LiStack*p;
S = (LiStack*)malloc(sizeof(LiStack));
S->next = NULL;
printf("输入栈中元素:");
for (int i = n; i > 0; i--)
{
p = (LiStack*)malloc(sizeof(LiStack));//生成新节点
scanf_s("%d", &p->data);//输入元素值
p->next = S->next;
S->next = p;//插入到表头
}
return S;
}
Status Push(LiStack*S, SElemType e)
{
LiStack*p;
p = (LiStack*)malloc(sizeof(LiStack));
p->data = e;//新建元素e对应的结点p
p->next = S->next;//插入p结点作为开始结点
S->next = p;
return OK;
}
Status Pop(LiStack*S)
{
LiStack *p;
SElemType e;
if (S->next == NULL)
return FALSE;
p = S->next;//p指向开始结点
e = p->data;
S->next = p->next;//删除*P结点
free(p);//释放*p结点
return OK;
}
Status GetTop(LiStack*S)
{
SElemType e;
if (S->next == NULL)//栈空的情况
return FALSE;
e = S->next->data;
printf("%d", e);
return OK;
}
void PrintLiStack(LiStack *S)
{
LiStack *p;
p = S->next;
while (p != NULL)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LiStack *S;
SElemType e;
int n;
printf("请输入栈中的数据元素个数:");
scanf_s("%d", &n);
S = InitStack(n);
printf("栈中的数据元素是:");
PrintLiStack(S);
printf("栈顶元素是:");
GetTop(S);
printf("\n");
printf("请输入要入栈的元素:");
scanf_s("%d", &e);
Push(S, e);
printf("入栈后栈中元素是:");
PrintLiStack(S);
Pop(S);
printf("出栈后栈中元素是:");
PrintLiStack(S);
system("pause");
return 0;
}