ARRAY IMPLEMENTATION OF STACK

Hi guys, In this post I would like to discuss about the simple concept, stack.For easy understanding I have implemented the stack using array.In the next post I will discuss with you about the implementation of stack using the linked list (Stack ADT).

STACK MODEL

A Stack is a restricted form of list, where the insertion and deletion can be performed only in the top of the list.Each time when the insertion is performed, the element is inserted into the stack it is inserted into the top of the stack.This insertion operation is called as push operation.The pop operation deletes the most recently inserted element. Sometimes Stack is also called as LIFO list (Last In First Out).In Stack model only the top elements are accessible.

Code for Stack:

#include

#include

void create();

void push(void);

void pop(void);

void display(void);

int isfull(int);

int isempty(void);

int a[100],i,n,top=-1,x=0,item,ch; //global declaration of stack array.

void main()

{

int choice;

clrscr();

do

{

printf(“\n1.CREATE”);

printf(“\n2.PUSH”);

printf(“\n3.POP”);

printf(“\n4.DISPLAY”);

printf(“\n5.EXIT”);

scanf(“%d”,&ch);

switch(ch)

{

case 1:

create();

break;

case 2:

push();

break;

case 3:

pop();

break;

case 4:

display();

break;

case 5:

exit(0);

}

}while(ch
}

void create()

{

printf(“\nEnter the size: “);

scanf(“%d”,&n);

for(i=0;i=0;i–)

printf(“\n%d”,a[i]);

}

int isfull(int x)

{

if(x==n)

return 1;

else

return 0;

}

int isempty(void)

{

if(top==-1)

return 1;

else

return 0;

}