Guest User

Untitled

a guest
Aug 14th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. #define MaxStackSize 10
  7.  
  8. typedef struct
  9. {
  10.     char *data;
  11. }element;
  12.  
  13. typedef struct
  14. {
  15.     element stack[MaxStackSize];
  16.     int length;
  17. }StackADT;
  18.  
  19. bool isFull(StackADT *S)
  20. {
  21.     if(S->length >= (MaxStackSize-1))
  22.         return true;
  23.     else
  24.         return false;
  25. }
  26.  
  27. bool isEmpty(StackADT *S)
  28. {
  29.     if(S->length==-1)
  30.         return true;
  31.     else
  32.         return false;
  33. }
  34.  
  35. void Push(StackADT *S,element item)
  36. {
  37.     if(isFull(S))
  38.         printf("stack is full \n");
  39.     else
  40.     {
  41.         printf("item=%s\n",item);
  42.         S->stack[++(S->length)]=item;
  43.     }
  44. }
  45.  
  46. int main()
  47. {
  48.     //初始化STACK
  49.     StackADT s1;
  50.     s1.length=-1;
  51.  
  52.     //手動加入stack元素
  53.     element item0;
  54.     item0.data="test0";
  55.     Push(&s1,item0);
  56.  
  57.     element item1;
  58.     item1.data="test1";
  59.     Push(&s1,item1);
  60.  
  61.     element item2;
  62.     item2.data="test2";
  63.     Push(&s1,item2);
  64.  
  65.     element item3;
  66.     item3.data="test3";
  67.     Push(&s1,item3);
  68.  
  69.     //自動加入stack元素
  70.     int j;
  71.     for(j=4;j<8;j++)
  72.     {
  73.         element item;
  74.         char buf[10];
  75.         sprintf(buf,"test%d",j);
  76.         //strcpy(item.data,buf);
  77.         item.data=buf;
  78.         Push(&s1,item);
  79.     }
  80.  
  81.     //顯示stack目前長度
  82.     printf("s1.length=%d\n",s1.length);
  83.  
  84.     //顯示stack內容
  85.     int i;
  86.     for (i=s1.length;i>=0;i--)
  87.         printf("%s\n",s1.stack[s1.length-i]);
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment