设计一个
#include <stdio.h>
#define Max 255
#define DataType char
struct stack
{
DataType data[Max];
DataType top;
};
int count=0;
struct stack in(struct stack S)
{
S.top=0;
return S;
}
struct stack push(DataType n,struct stack S)
{
S.data[S.top]=n;
S.top++;
return S;
}
struct stack pop(struct stack S)
{
S.top--;
return S;
}
int getTop(struct stack S)
{
return S.data[S.top-1];
}
int Empty(struct stack S)
{
if(S.top==0)
{
return 1;
}
else
{
return 0;
}
}
int Full(struct stack S)
{
if(S.top==3)
{
return 1;
}
else
{
return 0;
}
}
void display(struct stack S)
{
printf("\t");
struct stack S2=S;
while(S2.top)
{
printf("%c ",getTop(S2));
S2=pop(S2);
}
printf("\n\n");
}
int main()
{
struct stack S;
S=in(S);
DataType data[Max]={"ABCDE"};
S=push('A',S);
printf("A进栈\n");
printf("当前栈为:"); display(S);
S=push('B',S);
printf("B进栈\n");
printf("当前栈为:"); display(S);
S=push('C',S);
printf("C进栈\n");
printf("当前栈为:"); display(S);
printf("%c出栈\n",getTop(S));
S=pop(S);
printf("当前栈为:"); display(S);
printf("%c出栈\n",getTop(S));
S=pop(S);
printf("当前栈为:"); display(S);
printf("%c出栈\n",getTop(S));
S=pop(S);
printf("当前栈为:"); display(S);
S=push('D',S);
printf("D进栈\n");
printf("当前栈为:"); display(S);
printf("%c出栈\n",getTop(S));
S=pop(S);
printf("当前栈为:"); display(S);
S=push('E',S);
printf("E进栈\n");
printf("当前栈为:"); display(S);
printf("%c出栈\n",getTop(S));
S=pop(S);
printf("当前栈为:"); display(S);
display(S);
return 0;
}
栈,元素进栈的次序为A,B,C,D,E,请写出得到如下序列的操作序列:C,B,A,D,E.
#include<stdio.h> #include<malloc.h> //定义栈 typedef struct{ char data[100]; int top; int bottom; }stack; //创建栈 stack *StackCreate(){ stack *p=(stack*)malloc(sizeof(stack));//分配新空间 if(p==NULL)//分配失败 return 0; p->bottom=p->top=0;//分配成功 return p; } //入栈 void StackInput(stack *p,char str){ p->data[p->top]=str;//存入栈中 p->top++;//栈顶指针加1 } //出栈 char StackOutput(stack *p){ char str; if(p->top!=p->bottom){//栈非空 str=p->data[p->top-1];//栈顶内容输出 p->top--;//栈顶减1 return str; } } //输出 void StackPrint(stack *p){ while(p->top!=p->bottom){ printf("%c",p->data[p->top-1]); p->top--; } } //主函数 int main(){ int i; stack *p=StackCreate();//创建栈 StackInput(p,'A'); StackInput(p,'B'); StackInput(p,'C'); printf("%c",StackOutput(p)); printf("%c",StackOutput(p)); printf("%c",StackOutput(p)); StackInput(p,'D'); printf("%c",StackOutput(p)); StackInput(p,'E'); printf("%c",StackOutput(p)); }