#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
const int N= 100;
const int add = 10;
int arr[201];
typedef struct{
int *base;
int *top;
int size;
}S;
void print(S *s)
{
int *i;
int cnt = 1;
// printf("打印已经执行");
for(i = s->base; i < s->top ;i++,cnt++)
{
printf("%d ",*(s->top-cnt));
}
}
void Init(S *s)
{
// printf("初\n");
s->base = (int *)malloc(sizeof (int));
// printf("初始化成功\n");
// if(!s->base)
// {
// return -1;
// }
s->size = N;
s->top = s->base;
//return 1;
}
int Push(S *s,int e)
{
if(s->top-s->base >= s->size)
{
s->base = (int *)malloc(sizeof (int)*(N+add));
if(!s->base) return -1;
s->top = s->base + s->size;
s->size = s->size + add;
}
*s->top = e;
s->top++;
// printf("压入操作已经执行\n") ;
// printf("%d",s->top - s->base);
print(s);
printf("The order of elements pushed into the stack is\n");
return 1;
}
int Create(S *s,int n)
{
Init(s);
int i;
//int tmp;
//s->size += n;
//printf("输入成功\n");
printf("Create successfully\n");
for(i = 1;i<=n;++i)
{
// printf("LLM\n");
int tmp;
scanf("%d",&tmp);
Push(s,tmp);
}
printf("Create successfully\n");
print(s);
return 1;
}
void Pop(S *s)
{
if(s->base== s->top)
{
printf("The Stack if empty");
}
else
{
s->top = s->top-1;
printf("%d",*s->top);
//s->top--;
}
}
void Getlength(S *s)
{
printf("%d",s->top-s->base);
}
void situation(S *s)
{
if(s->base == s->top)
{
printf("the stack is empty\n");
}
else if(s->base +s->size == s->top)
{
printf("the stack is full\n");
}
else
{
printf("the stack is available\n");
}
}
void algorithm(S *a,S *b)
{
Init(a);
int i;
// printf("Create successfully\n");
int ii = 0;
printf("Please input your stack length\n");
int n;
scanf("%d",&n);
for(i = 1;i<=n;++i)
{
// printf("LLM\n");
int tmp;
scanf("%d",&tmp);
arr[++ii] = tmp;
Push(a,tmp);
}
// printf("Create successfully\n");
print(a);
Init(b);
int jj = 201;
// printf("Please input your stack length\n");
scanf("%d",&n);
for(i = 1;i<=n;++i)
{
// printf("LLM\n");
int tmp;
scanf("%d",&tmp);
arr[--jj] = tmp;
Push(b,tmp);
}
printf("Create successfully\n");
print(b);
int pos = 1;
while(pos)
{
scanf("%d", &pos);
printf("1.judge the Stack situation\n");
printf("2.push the element left\n");
printf("3.push the elemnt right\n");
printf("4.pop the elemnt left\n");
printf("4.pop the elemnt right\n");
if(pos == 1)
{
}
else if(pos == 2)
{
}
}
}
S s;
S a;
S b;
int n;
int main(){
int pos = 1;
printf("1.Create Stack\n");
printf("2.push the element in Stack\n");
printf("3.Pop the top element in Stack\n");
printf("4.Get the length of Stack\n");
printf("5.judge if the Stack situation\n");
printf("6.Print the element of the Stack\n");
printf("7.the algorithm\n");
while(pos)
{
scanf("%d",&pos);
if(pos == 1)
{
printf("Please input your Stack Length\n");
// int n;
scanf("%d",&n);
printf("Please input your Stack element\n");
Create(&s,n);
}
if(pos == 2)
{
printf("Please in put the number which you want to push into Stack\n");
scanf("%d",&n);
Push(&s,n);
}
if(pos == 3)
{
printf("This process is pop the top element\n");
Pop(&s);
}
if(pos == 4)
{
Getlength(&s);
}
if(pos == 5)
{
situation(&s);
}
if(pos == 6)
{
print(&s);
}
printf("\n");
printf("1.Create Stack\n");
printf("2.push the element in Stack\n");
printf("3.Pop the top element in Stack\n");
printf("4.Get the length of Stack\n");
printf("5.judge if the Stack situation\n");
printf("6.Print the element of the Stack\n");
}
return 0;
}