学习笔记
今天我看了数据结构中的顺序栈的构建以及顺序栈的共用。下边是我练习的代码:
#include "stdio.h"
#define MAX 10
typedef struct st
{
char ch[MAX];
int top[2];
}stack;
void init(stack *p)
{
p->top[0]=-1;
p->top[1]=MAX;
}
void push(stack *p)
{
char t;
int flag=0;
printf("please input stack 0:/n");
fflush(stdin);
scanf("%c",&t);
if(t!='-')
{ p->top[0]++;
while(p->top[0]<MAX&&t!='-')
{
p->ch[p->top[0]++]=t;
fflush(stdin);
flag++;
if(p->top[0]<MAX)
scanf("%c",&t);
}
p->top[0]--;
}
if(p->top[0]!=MAX-1)
{
printf("please input stack 1:/n");
fflush(stdin);
scanf("%c",&t);
if(t!='-')
{
p->top[1]--;
flag=MAX-flag;
while(p->top[1]>=0&&t!='-'&&flag>0)
{
p->ch[p->top[1]--]=t;
fflush(stdin);
flag--;
if(flag>0)
scanf("%c",&t);
}
p->top[1]++;
}
}
}
void pop(stack *p)
{
if(-1==p->top[0])
printf("stack 0 is empty/n");
else
{
printf("pop stack 0:/t");
while(p->top[0]>=0)
{
printf("%c/t",p->ch[p->top[0]--]);
}
}
if(MAX==p->top[1])
printf("/nstack 1 is empty");
else
{
printf("/n/npop stack 1:/t");
while(p->top[1]<MAX)
{
printf("%c/t",p->ch[p->top[1]++]);
}
}
}
main()
{
stack *p,st1;
p=&st1;
clrscr();
init(p);
push(p);
pop(p);
getch();
}
2006-8-13 21:00:06