Skip to content

Instantly share code, notes, and snippets.

@anugrahbsoe
Created August 29, 2015 04:10
Show Gist options
  • Save anugrahbsoe/69de15c1895feb6c72bb to your computer and use it in GitHub Desktop.
Save anugrahbsoe/69de15c1895feb6c72bb to your computer and use it in GitHub Desktop.
belajar dstack
#include <stdio.h>
#define n 20
int S[n], top1, top2, x;
void awal();
void push1();
void pop1();
void push2();
void pop2();
void tampil();
void main()
{
int pilihan, kondisi = 1;
do {
printf("\nPILIHAN\n");
printf("\n1. Inisialisasi awal");
printf("\n2. Push1");
printf("\n3. Pop1");
printf("\n4. Push2");
printf("\n5. Pop2");
printf("\n6. Tampil Stack");
printf("\n7. Keluar");
printf("Input pilihan: ");
scanf("%i", &pilihan);
switch(pilihan) {
case 1 : awal(); break;
case 2 : push1(); break;
case 3 : pop1(); break;
case 4 : push2(); break;
case 5 : pop2(); break;
case 6 : tampil(); break;
case 7 : kondisi = 0; break;
}
} while (kondisi);
}
void awal()
{
top1 = -1;
top2 = n;
}
void push1()
{
if (top2-top1 > 1) {
printf("\nInput bilangan: ");
scanf("%i", &x);
top1 ++;
S[top1] = x;
} else {
printf("\nStack Penuh");
}
}
void pop1()
{
if (top1 > -1) {
x = S[top1];
S[top1] = 0;
top1 --;
printf("\nBilangan %i sudah di-pop", x);
} else {
printf("\nStack Kosong");
}
}
void push2()
{
if (top2-top1 > 1) {
printf("\nInput bilangan: ");
scanf("%i", &x);
top2 --;
S[top2] = x;
} else {
printf("\nStack Penuh");
}
}
void pop2()
{
if (top2 < n) {
x = S[top2];
S[top2] = 0;
top2 ++;
printf("\nBilangan %i sudah di-pop", x);
} else {
printf("\nStack Kosong");
}
}
void tampil()
{
int i;
printf("\nISI STACK");
for (i=0; i<n; i++) {
printf("%3i", S[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment