andiklive

SD - Stack

Nov 9th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. #define MAX_STACK 100
  6.  
  7. typedef int infotype;
  8.  
  9. typedef struct{
  10.     infotype top;
  11.     int content[MAX_STACK];
  12. }Stack;
  13.  
  14. void init(Stack *S)
  15. {
  16.     (*S).top=-1;
  17. }
  18.  
  19. void push(Stack *S, int value)
  20. {
  21.     if((*S).top < (MAX_STACK - 1))
  22.     {
  23.         (*S).top++;
  24.         (*S).content[(*S).top] = value;
  25.     }
  26.     else
  27.         printf("\nStack penuh...");
  28. }
  29.  
  30. infotype pop(Stack *S)
  31. {
  32.     infotype a;
  33.  
  34.     if ((*S).top > -1)
  35.     {
  36.         a=(*S).content[(*S).top];
  37.         (*S).top--;
  38.         return(a);
  39.     }
  40.     else
  41.     {
  42.         printf("\nStack kosong...");
  43.         return 0;
  44.     }
  45. }
  46.  
  47. void show(Stack S)
  48. {
  49.     if (S.top > -1)
  50.     {
  51.         while (S.top != -1)
  52.         {
  53.             printf("\n %d", S.content[S.top]);
  54.             S.top--;
  55.         }
  56.     }
  57.     else
  58.         printf("\nStack kosong...");
  59. }
  60.  
  61. void randomContent(Stack *S, int n)
  62. {
  63.     int x;
  64.     if((*S).top < (MAX_STACK - 1))
  65.     {
  66.         (*S).top=n;
  67.         (*S).top++;
  68.         x=rand()%100;
  69.         (*S).content[(*S).top] = x;
  70.     }
  71.     else
  72.         printf("\nStack penuh...");
  73. }
  74.  
  75. void main()
  76. {
  77.     Stack myStack;
  78.     char pilih;
  79.     infotype newVal,maks,a;
  80.     init(&myStack);
  81.     do
  82.     {
  83.         system("cls");
  84.         printf("\n++++++ MENU ++++++");
  85.         printf("\n1. PUSH");
  86.         printf("\n2. POP");
  87.         printf("\n3. ShowAll");
  88.         printf("\n4. PUSH yg diacak");
  89.         printf("\n\nPilihan : ");
  90.         pilih = getch();
  91.  
  92.         switch(pilih)
  93.         {
  94.         case '1' :
  95.             printf("\nMasukkan nilai yang akan di-Push : ");
  96.             scanf("%d",&newVal);
  97.             push(&myStack, newVal);
  98.             getch();
  99.             break;
  100.         case '2' :
  101.             a=pop(&myStack);
  102.             printf("\nIsi stack yang di-POP : %d",a);
  103.             getch();
  104.             break;
  105.         case '3' :
  106.             show(myStack);
  107.             getch();
  108.             break;
  109.         case '4' :
  110.             printf("\nMasukkan maksimal bilangan : ");
  111.             scanf("%d",&maks);
  112.             randomContent(&myStack,maks);
  113.             getch();
  114.             break;
  115.         }
  116.     }while(pilih != 27);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment