資料結構-共享棧的基本操作實現
阿新 • • 發佈:2019-02-17
/*main*/ #include<stdio.h> #include<stdlib.h> #define MaxSize 4 #define TRUE 1 #define ERROR 0 #define OK 1 #define FALSE 0 #define OVERFLOW -2 typedef int ElemType; typedef int status; typedef struct Stack{ ElemType Data[MaxSize]; int top1; int top2; }*SqStack,Stack; #include"SeqStack.h" int main() { Stack * s=(SqStack)malloc(sizeof(Stack));; Create(s); print(s); putchar(10); int flag1,flag2;ElemType e,x; printf("請輸入要入棧的標號和元素:"); scanf("%d",&flag1); scanf("%d",&e); push(s,flag1,e); print(s); putchar(10); printf("請輸入要出棧的棧標號:"); scanf("%d",&flag2); Pop(s,flag2); putchar(10); print(s); return 0; } /*要包含的的標頭檔案*/ status Create(Stack *S) { S->top1=-1; S->top2=MaxSize; S->top1++; S->Data[S->top1]=2; S->top1++; S->Data[S->top1]=78; S->top2--; S->Data[S->top2]=90; S->top2--; S->Data[S->top2]=908; return OK; } status print(Stack *S) { if(S->top1==-1&&S->top2==MaxSize) return ERROR; printf("top1:"); for(int i=0;i<S->top1+1;i++) { printf("%d ",S->Data[i]); } printf("\ntop2:"); for(int i=S->top2;i<MaxSize;i++) { printf("%d ",S->Data[i]); } printf("\n"); } status push(Stack *S,int flag,ElemType e) { if(S->top1+1==S->top2) { printf("棧滿!\n"); return ERROR; } if(flag==1) { S->top1++; S->Data[S->top1]=e; } else if(flag==2) { S->top2--; S->Data[S->top2]=e; } return OK; } status Pop(Stack *S,int flag) { ElemType e; if(S->top1==-1&&S->top2==MaxSize) { printf("棧空!"); return ERROR; } if(flag==1) { e=S->Data[S->top1]; S->top1--; } else if(flag==2) { e=S->Data[S->top2]; S->top2++; } else printf("輸入錯誤!"); printf("pop出的元素:%d",e); return OK; }