題目描述
思路分析
定義兩個指針,一個在最後,一個在最開始同時向中間指。棧滿就說明數組空間滿了,頭指針挨着尾指針了。
太簡單了,主要是給女朋友講的
源代碼
#include <stdio.h>
#include <stdlib.h>
#define max 100
typedef struct
{
char date[max];
int top1,top2;
}status;
status *initstack()//創建線性存儲結構的棧結構
{
status *s;
s=(status *)malloc(sizeof(status));//
s->top1=-1;
s->top2=max;
return s;
}
bool isFull(status *s){//判斷棧是否滿
if(s->top1>=s->top2-1)
return true;
return false;
}
bool top1IsNone(status *s){//判斷棧一是否爲空
if(s->top1==-1)
return true;
return false;
}
bool top2IsNone(status *s){//判斷棧二是否爲空
if(s->top2==max)
return true;
return false;
}
void Push1(status *s,char e)//棧一進棧操作
{
if(isFull(s))
{
printf("棧已滿");
}else{
s->top1++;
s->date[s->top1]=e;
}
}
void Push2(status *s,char e)//棧一進棧操作
{
if(isFull(s))
{
printf("棧已滿");
}else{
s->top2--;
s->date[s->top2]=e;
}
}
char Pop1(status *s)//棧一出棧操作
{
if(top1IsNone(s)){
printf("棧爲空");
}else{
char e;
e=s->date[s->top1];
s->top1--;
return e;
}
}
char Pop2(status *s)//棧一出棧操作
{
if(top2IsNone(s)){
printf("棧爲空");
}else{
char e;
e=s->date[s->top2];
s->top2++;
return e;
}
}
int main()//測試
{
int i=0,n=0;
status *s;
s=initstack();
char a;
scanf("%c",&a);
Push1(s,a);
printf("%c\n",Pop1(s));
printf("%c\n",Pop2(s));
return 0;
}