Jika ada salah dalam Contoh Program Stack ini mohon informasinya, berikan komentar ttg kesalahannya dimana... :)
/*------------------------------*/
/* Contoh Program Stack Tunggal*/
/* Pratama Nur Wijaya */
/*-----------------------------*/
#include "stdio.h"
//GLOBAL VARIABLE
#define MAX 50
#define true 1
#define false 0
char stack[MAX];
int top;
//inisialisasi fungsi
void init(void);
int full(void);
int empty(void);
char pop(void);
void clear(void);
void push(char info);
void baca(void);
//main function
main(){
//local variable
char pilih, elm;
printf("--- Contoh Program Single Stack --\n");
init(); //jalankan fungsi init
do{
printf("Operasi Single Stack :\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. CLEAR\n");
printf("4. BACA\n");
printf("5. Selesai\n");
printf(" Pilihan anda :");
scanf("%s", &pilih);
switch(pilih){
case '1' : printf("PUSH ");
scanf("%s",&elm);
push(elm);
break;
case '2' : elm=pop();
printf("POP %c\n", elm);
break;
case '3' : clear();
break;
case '4' : baca();
break;
case '5' : break;
default : printf("Salah Pilih. . . \n");
break;
}
printf("\n");
}while(pilih!='5');
}
//end main function
void init(){
top=0;
}
void push(char info){
if(full()!=true){
top++;
stack[top]=info;
}
else
printf("Stack overflow. . . . \n");
}
char pop(){
char info;
if(empty()!=true){
info=stack[top];
top--;
return(info);
}
else printf("stack underflow. . . \n");
}
void clear(){
top=0;
}
int full(){
if(top==MAX){
return(true);
}
else {
return(false);
}
}
int empty(){
if(top==0) return(true);
else return(false);
}
void baca(){
int i;
printf("Isi stack. . . \n");
if(top>0){
for(i=1; i<=top; i++)
printf("%c", stack[i]);
}
else
printf("Stack kosong. . .\n");
}
Artikel Saya lainnya :
- Stack tunggal Pemrograman C++
- Mengenal Bash Programming
- Cara menjalankan C di Ubuntu
- Keyboard tidak Terdeteksi di Ubuntu 10.04 VMware
Read More..