subscribe to RSS

Single Stack dan Double Stack

Posted by prawoto on Sabtu, 03 April 2010

SINGLE STACK

#include
#include
#include

#define MAX 50
#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);
void baca(void);

main(){
char pilih,elm[30];
int pjg,j;
printf("---Demo Operasi Single stack");
init();

do{
printf("Operasi single stack :\n");
printf("[1] Push\n");
printf("[2] Baca Balik\n");
printf("[3] Clear\n");
printf("[4] Selesai\n");
printf("Pilihan : ");
scanf("%s",&pilih);
switch(pilih){
case '1' : printf("Push : ");
pjg=strlen(elm);
scanf("%s",&elm);
pjg=strlen(elm);
for(j=0;j<=pjg-1;j++)
push(elm[j]);
break;
case '2' : baca();
break;
case '3' : clear();
break;
case '4' : break;
default  : printf("Salah pilih...\n");
break;
           
}
printf("\n");
}while(pilih!='4');
}
void init(void){
top=0;
}
void push(char info){
if (full()!=true){
top++;
stack[top]=info;
}
else printf("stack sudah penuh");
}
char pop(void){
char info;
if (empty()!=true){
info=stack[top];
top--;
return(info);
}
else
printf("Stack kosong\n");
}
void clear(void){
top=0;
}
int full(void){
if(top==MAX) return (true);
else return(false);
}
int empty(void){
if(top==0)return(true);
else return(false);
}

void baca(void){
int i;
printf("Isi stack : ");
if (top>0){
for(i=1;i<=top;i++)
printf("%c",stack[i]);
printf("\n");

printf("Setelah dibalik :");
for(i=top;i>=1;i--)
printf("%c",pop());
printf("\n");
}
else printf("kosong");
printf("\n");
}

HASIL SINGLE STACK

































DOUBLE STACK


#include
#include
#include

#define MAX 50
#define true 1
#define false 0

char stack[MAX];
int top1;
int top2;

void init (void);
int full(void);
int empty(int nostack);
char pop(int nostack);
void clear(int nostack);
void push(char info,int nostack);

main(){
char pilih,elm[25];
int pjg,j,no,topA,topB;
printf("---Demo Operasi double stack");
init();

            do{
            printf("Operasi double stack :\n");
            printf("[1] Push\n");
            printf("[2] Baca Balik\n");
            printf("[3] Clear\n");
            printf("[4] Selesai\n");
            printf("Pilihan : ");
            scanf("%s",&pilih);
            switch(pilih){
                        case '1' : printf("Masukkan no stack : ");
                        scanf("%d",&no);
            //fflush(stdin);
            switch(no){
            case 1 : printf("Masukkan kata : ");
                                                scanf("%s",&elm);
            pjg=strlen(elm);
                                                for(j=0;j<=pjg-1;j++)
                                                push(elm[j],1);
            printf("Kata yang masuk : ");
            for(j=1;j<=top1;j++)
            printf("%c",stack[j]);
            printf("\n");
                                                break;
         case 2     : printf("Masukkan kata : ");
            scanf("%s",&elm);
            pjg=strlen(elm);
                                                for(j=0;j<=pjg-1;j++)
                                                push(elm[j],2);
            printf("Kata yang masuk : ");
            for(j=MAX;j>=top2;j--)
            printf("%c",stack[j]);
            printf("\n");
                                       break;
        }
                        break;
                                    case '2' : printf("Isi stack pertama dibalik : ");
                        if (empty(1)!=true){
            topA=top1;
            for(j=top1;j>=1;j--)
            printf("%c",pop(1));
            top1=topA;
            }
            else printf("Kosong");
            printf("\n");

            printf("Isi stack kedua dibalik : ");
                                      if (empty(2)!=true){
                 topB=top2;
                 for(j=top2;j<=MAX;j++)
                 printf("%c",pop(2));
                 top2=topB;
                 }
                 else printf("Kosong");
                 printf("\n");
                 break;
                                    case '3' :printf("Masukkan no stack : ");
                                                scanf("%d",&no);
                        clear(no);
                                                break;
         case '4' : break;
  //                    case '4' : break;
                                    default  : printf("Salah pilih...\n");
                                    break;
                        }
            printf("\n");
            }while(pilih!='4');
}
void init(void){
top1=0;
top2=MAX+1;
}
void push(char info,int nostack){
if (full()!=true){
            switch(nostack){
                        case 1 : top1++;
                        stack[top1]=info;
      break;
      case 2 : top2--;
      stack[top2]=info;
      break;
      default : printf("Invalid Push \n");
      break;
      }
}
            else printf("stack sudah penuh");
}
char pop(int nostack){
char info;

            if (empty(nostack)!=true){
                switch (nostack)
            {
              case 1 : info=stack[top1];
                                      top1--;
           return (info);
           break;
               case 2           : info=stack[top2];
                                      top2++;
           return (info);
           break;
               default           : printf("Invalid pop");
           break;
             }
            }
            else
                        printf("Stack kosong\n");
}
void clear(int nostack){
            switch (nostack){
            case 1 : top1=0;
            break;
            case 2 :  if(top2==MAX+1)
   //                   return(true);
            //          else return(false);
            default  : printf("Nomor stack invalid..\n");
                        break;
   }
}
int full(void){
            if(top1+1>=top2) return (true);
            else return(false);
}
int empty(int nostack){
            switch (nostack) {

            case 1  :           if(top1==0)return(true);
                                    else return(false);
            case 2 :  if(top2==MAX+1)return(true);
         else return(false);
            default  : printf("Nomor stack invalid..\n");
            break;
            }
}







HASIL DOUBLE STACK