Mastura Azzura Tumpukan Susunan Implementasi Pada pemrograman C ~ Dunia Elektro

Saturday, June 1, 2013




Mungkin masih bingung apa fungsi dari Stack Array, sebagai perumpaan, simak contoh berikut:


Apabila kita ingin mengambil TV yang telah tersusun dengan urutan seperti pada gambar, maka mau tidak mau kita harus mengelurkan Compo dan VCD terlebih dahulu. Apabila kita ingin mengambil TV yang telah tersusun dengan urutan seperti pada gambar, maka mau tidak mau kita harus mengelurkan Compo dan VCD terlebih dahulu. 
Terdapat 2 fungsi utama dalam Stack, yaitu :

1. Push

Fungsi ini adalah fungsi yang digunakan untuk mengisi data, dan data tersebut diisi pada tumpukan paling atas. 

2. Pop

Fungsi ini adalah untuk mengeluarkan data dari yang paling terakhir dimasukan.

Bagi yang masih penasaran, silahkan dicoba..





/*stack using array*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 20
int a[size];
int top=-1;
int isfull()
{
if(top==size-1)
return 1;
else
return 0;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}

int push(int x)
{
if(isfull())
{
printf("ERROR:overflow");
return 0;
}
top++;
a[top]=x;
return 0;
}
int pop()
{
if(isempty())
{
printf("ERROR:underflow\n");
return 0;
}
else
top--;
printf("VALUE on TOS=%d",a[top+1]);
return 0;
}
void display()
{ int i;
if(isempty())
printf("STACK is empty");
else
{
i=0;
for(i=0;i<=top;i++)
printf("%d\t",a[i]);
}
}
void main()
{
int choice,data;
clrscr();
do
    {
    clrscr();
    printf("WELCOME IN THE WORLD OF STACKS\n");
    printf("1.PUSH\n2.POP\n3.ISEMPTY\n4.ISFULL\n5.DISPLAY\n6.EXIT");
    printf("\nenter choice");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1:
        {
        printf("enter value that you want to push");
        scanf("%d",&data);
        push(data);
        
        printf("\npress any key to continue.....");
        getch();
        break;
        }//case 1 closed here
       case 2:
        {
        pop();
        printf("\npress any key to continue.....");
        getch();
        break;
        }//case 2 closed
       case 3:
        {
        if(isempty())
        {
        printf("stack is empty\n");
        printf("press any key to continue.....");
        getch();
        break;
        }
        else
        {
        printf("stack is not empty");
        printf("\npress any key to continue.....");
        getch();
        break;
        }
        }//case 3 closed

       case 4:
        {
        if(isfull())
        {
        printf("stack is full\n");
        printf("press any key to continue.....");
        getch();
        break;
        }
        else
        {
        printf("stack is not full\n");
        printf("press any key to continue.....");
        getch();
        break;
        }
        }//case 4 closed
       case 5:
        {
        display();
        printf("\npress any key to continue.....");
        getch();
        break;
        }
      case 6:
        {
        printf("program is QUITTING.....");
        printf("\npress any key to exit");
        getch();
        exit(0);
        }
       default:
        {
        printf("your choice is wrong\n");
        printf("press any key to continue.....");
        getch();
        break;
        }





    }//switch closed

    }while(choice!=6);

}

Post a Comment

Jika ingin berkomentar, silahkan menggunakan kata-kata yang baku, berkomentarlah sesuai dengan tema yang dibahas. Dilarang untuk promosi dalam bentuk apapun, memaki atau hanya sekedar spam.

Terima Kasih Jika Anda bersedia mematuhi aturan dari admin..
Selamat menikmati..