Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #define MaxStackSize 10
- typedef struct
- {
- char *data;
- }element;
- typedef struct
- {
- element stack[MaxStackSize];
- int length;
- }StackADT;
- bool isFull(StackADT *S)
- {
- if(S->length >= (MaxStackSize-1))
- return true;
- else
- return false;
- }
- bool isEmpty(StackADT *S)
- {
- if(S->length==-1)
- return true;
- else
- return false;
- }
- void Push(StackADT *S,element item)
- {
- if(isFull(S))
- printf("stack is full \n");
- else
- {
- printf("item=%s\n",item);
- S->stack[++(S->length)]=item;
- }
- }
- int main()
- {
- //初始化STACK
- StackADT s1;
- s1.length=-1;
- //手動加入stack元素
- element item0;
- item0.data="test0";
- Push(&s1,item0);
- element item1;
- item1.data="test1";
- Push(&s1,item1);
- element item2;
- item2.data="test2";
- Push(&s1,item2);
- element item3;
- item3.data="test3";
- Push(&s1,item3);
- //自動加入stack元素
- int j;
- for(j=4;j<8;j++)
- {
- element item;
- char buf[10];
- sprintf(buf,"test%d",j);
- //strcpy(item.data,buf);
- item.data=buf;
- Push(&s1,item);
- }
- //顯示stack目前長度
- printf("s1.length=%d\n",s1.length);
- //顯示stack內容
- int i;
- for (i=s1.length;i>=0;i--)
- printf("%s\n",s1.stack[s1.length-i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment