#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
typedef int datatype;
typedef struct
{
datatype stack[maxsize];
int top1;
int top2;
}seqstack;
seqstack *s;
void initstack()
{
if(!s);
else
{s->top1=-1;
s->top2=maxsize;
}
}
void push1(seqstack *q,datatype x)
{
if(q->top1==s->top2);
else
{
s->top1++;
s->stack[s->top1]=x;
}
}
void push2(seqstack *q,datatype x)
{
if(q->top2==s->top1);
else
{
s->top2--;
s->stack[s->top2]=x;
}
}
datatype pop(seqstack *q)
{
if(s->top1==0&&s->top2==maxsize-1) return 0;
if(q->top1==s->top1)
{
s->top1--;
return s->stack[s->top1+1];
}
if(q->top2==s->top2)
{
s->top2++;
return s->stack[s->top2-1];
}
}
int stackempty()
{
if(s->top1==-1&&s->top2==maxsize) return 1;
else return 0;
}
void display()
{
int t,r;
t=s->top1;
r=s->top2;
if(s->top1==-1 )printf("s1 is empty!");
else
{printf("\ns1:");
while(t!=-1)
{t--;
printf("%d ",s->stack[t]);
}
}
if(s->top2==maxsize)printf("s2 is empty!");
else
{
printf("\ns2:");
while(r!=maxsize)
{
r++;
printf("%d ",s->stack[r]);
}
}
}
int main()
{
initstack();
seqstack *s1,*s2;
s1=s;
s2=s;
s1->top2=-2;
s2->top1=-2;
int a[6]={1,2,3,4,5,6};
int i;
int b[5]={9,8,7,6,5};
for(i=0;i<6;i++) push1(s1,a[i]);
for(i=0;i<5;i++) push2(s2,b[i]);
printf("output the stack:");
display();
}
//还是写不好...
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
typedef int datatype;
typedef struct
{
datatype stack[maxsize];
int top[1];
}seqstack;
seqstack *initstack()
{
seqstack *s;
s=(seqstack *)malloc(sizeof (seqstack));
if(!s)
{printf("no enough room");}
else
{s->top[0]=-1;
s->top[1]=maxsize-1;
return s;
}
}
seqstack * push1(seqstack *s,datatype x)
{
if(s->top[0]==s->top[1])
{printf("1\n");return NULL;}
else
{
s->top[0]++;
s->stack[s->top[0]]=x;
return s;
}
}
seqstack * push2(seqstack *s,datatype x)
{
if(s->top[0]==s->top[1])
{printf("2\n");}
else
{
s->top[1]--;
s->stack[s->top[1]]=x;
return s;
}
}
datatype pop1(seqstack *s)
{
int y;
if(s->top[0]==-1)
{printf("1 is empty\n");return 0;}
else
{
s->top[0]--;
y=s->stack[s->top[0]+1];
return y;
}
}
datatype pop2(seqstack *s)
{
int y;
if(s->top[1]==maxsize-1)
{printf("2 is empty\n");return 0;}
else
{
s->top[1]++;
y=s->stack[s->top[1]-1];
return y;
}
}
int stackempty1(seqstack *s)
{
if(s->top[0]==-1) return 1;
else return 0;
}
int stackempty2(seqstack *s)
{
if(s->top[1]==maxsize-1) return 1;
else return 0;
}
main()
{
int i,a[6],b[5];
seqstack *s1,*s2;
s1=initstack();
s2=s1;
a[6]={1,2,3,4,5,6};
b[5]={9,8,7,6,5};
for(i=0;i<6;i++) push1(s1,a[i]);
for(i=0;i<5;i++) push2(s2,b[i]);
printf("output the stack:");
while(!stackempty1(s1)) printf("%d",pop1(s1));
printf("\n");
}