Monday, April 5, 2010
MEMBALIK KATA DENGAN SINGLE STACK
Print Screen
#include
#include
#define MAX 75
#define true 1
#define false 0
char stack[MAX];
int top;
void init(void);
int full(void);
int empty(void);
char pop(void);
void clear(void);
void push(char info);
main()
{
char pilih;
char kal[75];
int k,p;
printf("--membalik kata dengan stack--\n");
init();
do
{
printf("MENU PILIHAN :\n");
printf("[1] MASUKKAN KATA :\n");
printf("[2] BACA BALIK :\n");
printf("[3] SELESAI\n");
printf(" PILIHAN : ");
scanf("%s",&pilih);
switch(pilih)
{
case '1': printf("masukkan kata : ");
scanf("%s",&kal);
p=strlen(kal);
for(k=0;k<=p;k++)
push(kal[k]);
printf("kata yang masuk : ");
for(k=1;k<=top;k++)
printf("%c ",stack[k]);
printf("\n");
break;
case '2': if(empty()!=true)
{
printf("kata dibalik:");
for(k=top;k>=1;k--)
printf("%c ",pop());
printf("\n");
}
else printf("stack kosong\n");
break;
case '3': break;
default : printf("salah pilih...\n");
}
printf("\n");
}while(pilih!='3');
}
void init(void)
{top=0;
}
void push(char info )
{
if(full()!=true)
{
top++;
stack[top]=info;
}
else
printf("stack overlow...\n");
}
char pop(void)
{
char info;
if(empty()!=true)
{
info=stack[top];
top--;
return(info);
}
else
printf("stack underflow...\n");
}
int full(void)
{
if(top==MAX)
return(true);
else
return(false);
}
int empty(void)
{
if(top==0)
return(true);
else
return(false);
}
0 komentar:
Post a Comment