Sunday, May 13, 2012

Program C stack menggunakan linked list


Stack dianalogikan seperti tumpukan. sehingga data yang terakhir kali dimasukkan adalah data yang pertama kali keluar. Jika dibandingkan dengan linked list, stack hanya bisa melakukan operasi push dan pop. Push mirip dengan tambah awal pada linked list dan Pop mirip dengan hapus awal pada linked list.
Hal ini membuat stack lebih simpel dari linked list tapi menurut saya kurang flexibel.
Contoh program :

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct node{
       int bil;
       struct node *next;
       };

struct stack{
       int count;
       struct node *top;
       };
      
int isFull(stack *stack){
     if(stack->count==5) return 1;
     else return 0;
     }
    
int isEmpty(stack *stack){
    if(stack->count==0) return 1;
    else return 0;
}  

void Push(stack *stack){
     node *pNew;
     if(stack->count>=5) {printf("stack full"); getch();}
     else{pNew=(node*)malloc(sizeof(node*));
          printf("\nmasukkan nilai yang ingin dipush ke stack: "); scanf("%d",&pNew->bil);
          pNew->next=stack->top;
          stack->top=pNew;
          stack->count++;
          printf("\nproses push sukses"); getch();
          }   
}

void cetak(stack *stack){
     node *pWalker;
     pWalker=stack->top;
     if(stack->count==0){printf("\nstack kosong");getch();}
     else{system("cls");
          printf("\n   TOP   \n");
     while(pWalker!=NULL){
                           printf("---------\n");
                           printf("|   %d   |\n",pWalker->bil);
                           printf("---------\n");
                           pWalker=pWalker->next;
                           }
                           getch();
         }
}


void Pop(stack *stack){
     int dataOut;
     node *dltPtr;
     dltPtr=stack->top;
     if(stack->count==0){}
     else{
     dataOut=stack->top->bil;
     stack->top=stack->top->next;
     stack->count--;
     free(dltPtr);
     printf("\nproses pop berhasil\n");getch();
     }
}

void Top(stack *stack){
    int dataout;
    if(stack->count==0){printf("\ntop = NULL\n");getch();}
    else{ dataout=stack->top->bil;
          printf("\ntop = %d\n",dataout);getch();
}
}

void destroy(stack *stack){
    node *destroyer;
    while(stack->top!=NULL){
                            destroyer=stack->top;
                            stack->top=stack->top->next;
                            free(destroyer);
                            }
    stack->count=0;
}



      
int main(){
stack stack;
stack.count=0;
stack.top=NULL;

int isfull,isempty;
char pilih;
do{system("cls");
printf("\t\t\t\tSTACK\n\n");  
printf("\n[1].isFull");
printf("\n[2].isEmpty");
printf("\n[3].makeEmpty");   
printf("\n[4].push");
printf("\n[5].pop");
printf("\n[6].top");
printf("\n[7].tampilkan stack");
printf("\n[8].exit\n");
printf("\npilihan: "); scanf("%c",&pilih); fflush(stdin);
if(pilih=='1'){isfull=isFull(&stack);
               if(isfull==1){printf("\n stack penuh\n");getch();}
               else {printf("\ntidak penuh\n"); getch();}
               }
if(pilih=='2'){isempty=isEmpty(&stack);
               if(isempty==1){printf("\n stack kosong\n");getch();}
               else {printf("\ntidak kosong\n");getch();}
               }
if(pilih=='3'){destroy(&stack);}
if(pilih=='4'){Push(&stack);}
if(pilih=='5'){Pop(&stack);}
if(pilih=='6'){Top(&stack);}
if(pilih=='7'){cetak(&stack);}
}while(pilih!='8');

}



2 comments: