STUDENT AMIKOM

Thursday, April 14, 2011

Program STUCK Balik Kalimat

/*program stackterbalik.c
Ninda Khana A.*/

#include <stdio.h>
#define MAX 20
char stack[MAX]; // array stack
int top; // penunjuk posisi atas stack

void push(char e);
char pop(void);

int main(void) {
char str[80];
int i;

printf("string : ");
gets(str);

for(i = 0; i < strlen(str); i++)
    push(str[i]);
   
printf("reverse : ");
    for(i = top - 1; i>= 0; i--)
        printf("%c", pop());
        return 0;
        }
   


/*
* fungsi push
* memasukkan elemen stack
*
*/
void push(char e) {
if(top >= MAX) {
printf("Stack penuh\n");
} else {
stack[top] = e;
top++;
}
}
/*
* fungsi pop
* mengeluarkan isi stack
*
*/
char pop(void) {
if(top <= 0) {
printf("Stack kosong\n");
return 0;
} else {
top--;
return stack[top];
}
}
/*
* fungsi show
* menampilkan isi stack
*
*/
void show(void) {
int i;
if(top <= 0) {
printf("Stack kosong\n");
} else {
for(i = 0; i < top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
/*
* fungsi menu
* menampilkan menu pilihan
*
*/
int menu(void) {
int m;
printf("Demo Stack\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Lihat\n");
printf("4. Keluar\n");
printf(": ");
scanf("%d", &m);
return m;
}

0 comments:

Post a Comment