Skip to content

Instantly share code, notes, and snippets.

@anugrahbsoe
Created August 29, 2015 04:08
Show Gist options
  • Save anugrahbsoe/9cc3f16f51f4ebeac7a7 to your computer and use it in GitHub Desktop.
Save anugrahbsoe/9cc3f16f51f4ebeac7a7 to your computer and use it in GitHub Desktop.
belajar stack
#include <stdio.h>
int S[10], top1, top2;
void awal();
void push1();
void push2();
void pop1();
void pop2();
void tampil();
void main()
{
int pil, kondisi=1;
awal();
while (kondisi) {
printf("\n1. Push 1");
printf("\n2. Pop 1");
printf("\n3. Push 2");
printf("\n4. Pop 2");
printf("\n5. Tampil");
printf("\n6. Reset");
printf("\n7. Keluar");
printf("\nMasukkan pilihan (1-7) : ");
scanf("%i", &pil);
switch(pil) {
case 1 : push1(); break;
case 2 : pop1(); break;
case 3 : push2(); break;
case 4 : pop2(); break;
case 5 : tampil(); break;
case 6 : awal(); break;
case 7 : kondisi=0; break;
}
}
}
void awal()
{
top1= -1;
top2= 10;
}
void push1()
{ int X;
printf("\nMasukkan X = ");
scanf("%i", &X);
if (top2-top1 > 1) {
top1++;
S[top1] = X;
} else {
printf("\nStack Penuh");
}
}
void push2()
{ int X;
printf("\nMasukkan X = ");
scanf("%i", &X);
if (top2-top1 > 1) {
top2--;
S[top2] = X;
} else {
printf("\nStack Penuh");
}
}
void pop1()
{ int X;
if (top1 > -1) {
X = S[top1];
S[top1] = 0;
top1--;
printf("\nNilai %i sudah di-pop", X);
} else {
printf("\nStack Kosong");
}
}
void pop2()
{ int X;
if (top2 < 10) {
X = S[top2];
S[top2] = 0;
top2++;
printf("\nNilai %i sudah di-pop", X);
} else {
printf("\nStack Kosong");
}
}
void tampil()
{ int i;
for (i=0; i<10; i++) {
printf("%3i", S[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment