Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<conio.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX_STACK 100
- typedef int infotype;
- typedef struct{
- infotype top;
- int content[MAX_STACK];
- }Stack;
- void init(Stack *S)
- {
- (*S).top=-1;
- }
- void push(Stack *S, int value)
- {
- if((*S).top < (MAX_STACK - 1))
- {
- (*S).top++;
- (*S).content[(*S).top] = value;
- }
- else
- printf("\nStack penuh...");
- }
- infotype pop(Stack *S)
- {
- infotype a;
- if ((*S).top > -1)
- {
- a=(*S).content[(*S).top];
- (*S).top--;
- return(a);
- }
- else
- {
- printf("\nStack kosong...");
- return 0;
- }
- }
- void show(Stack S)
- {
- if (S.top > -1)
- {
- while (S.top != -1)
- {
- printf("\n %d", S.content[S.top]);
- S.top--;
- }
- }
- else
- printf("\nStack kosong...");
- }
- void randomContent(Stack *S, int n)
- {
- int x;
- if((*S).top < (MAX_STACK - 1))
- {
- (*S).top=n;
- (*S).top++;
- x=rand()%100;
- (*S).content[(*S).top] = x;
- }
- else
- printf("\nStack penuh...");
- }
- void main()
- {
- Stack myStack;
- char pilih;
- infotype newVal,maks,a;
- init(&myStack);
- do
- {
- system("cls");
- printf("\n++++++ MENU ++++++");
- printf("\n1. PUSH");
- printf("\n2. POP");
- printf("\n3. ShowAll");
- printf("\n4. PUSH yg diacak");
- printf("\n\nPilihan : ");
- pilih = getch();
- switch(pilih)
- {
- case '1' :
- printf("\nMasukkan nilai yang akan di-Push : ");
- scanf("%d",&newVal);
- push(&myStack, newVal);
- getch();
- break;
- case '2' :
- a=pop(&myStack);
- printf("\nIsi stack yang di-POP : %d",a);
- getch();
- break;
- case '3' :
- show(myStack);
- getch();
- break;
- case '4' :
- printf("\nMasukkan maksimal bilangan : ");
- scanf("%d",&maks);
- randomContent(&myStack,maks);
- getch();
- break;
- }
- }while(pilih != 27);
- }
Advertisement
Add Comment
Please, Sign In to add comment