C – Stack of array

Pada dasarnya STACK atau tumpukan itu hampir sama dengan array, hanya saja dalam stack, prinsip ketersediaan ruang didefinisikan dalam konstanta. Dan pengambilan data tidak dapat langsung diambil di index yang dituju seperti pada array, tetapi diambil dari data teratas terlebih dahulu. Sistemnya adalah data yang terakhir masuk adalah yang pertama keluar. Seperti pada contoh program sederhana berikut.

#include <stdio.h>
#include <stdlib.h>#define MAX 6
void push();
void pop();
int isFull();
int isEmpty();
void display();
int stack[6], TOP= -1;
int main() {
int stack[6], TOP= -1, choice;
printf("\nENter the choice\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
scanf("%d",&choice);
switch(choice){
case 1: push(); break;
case 2: pop(); break;
case 3: display();default : printf("invalid choice");
}
main();
return 0;
}
int isFull(){
if(TOP==MAX-1) return 1;
else return 0;
}
int isEmpty(){
if(TOP==-1) return 1;
else return 0;
}
void push(){
if(isFull()==1) printf("Overflow");
else {
int item;
printf("Enter the item :");
scanf("%d", &item);
TOP=TOP+1;
stack[TOP]=item;
}
}
void pop(){
int temp;
if(isEmpty()==1) printf("under flow");
else{
temp=stack[TOP];
TOP=TOP-1;
}
}
void display(){
int i, temp=TOP;
if(isEmpty()==1)printf("\nNo data");
else{
printf("\nStack contens are\n");
printf("\n\tLocation data\n");
printf("\n-------------\nTOP->");
for(i=TOP;i>=0;i--){
printf("\t%d\t->%d",temp--,stack[i]);
printf("\n\t------------\n");
}
}
}
 

About Iin aryani

I don't wanna be a "Copycat" but, something they need to copy.

Posted on 26 May 2012, in Open Source ( C Program ) and tagged . Bookmark the permalink. 1 Comment.

  1. It’s actually a nice and helpful piece of info. I’m satisfied that you simply
    shared this helpful info with us. Please stay us informed like this.
    Thank you for sharing.

Komentar untuk penulis

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: