/*########################################*/
/* Membalikan kata dengan single stack */
/* by Ma’shum Rois */

# 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] MASUKAN KATA\n”);
printf(“[2] BACA BALIKAN\n”);
printf(“[3] SELESAI\n”);
printf(” Pilihan : “);
scanf(“%s”, &pilih);
switch(pilih)
{
case’1′:printf(“Masukan Kata : “);
scanf(“%s”, &kal);
p=strlen(kal);
for(k=0;k<=p;k++)
push(kal[k]);
printf(“Kata yang masuk:”);
for(k=1;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 overflow…\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);
}

Iklan