Stack Implementation using  Array


The program is about to insert/ push an element in the stack by using array.
Array is a collection of item  of similar data type. Ordinary variables are capable fir holding only one value at a time, but in real programming environment array is solution fir holding more then one values in a single variable name. An  array is a collection of data items that holds fixed number of value of same type. It is also called order set of an array stored in consecutive memory location in the RAM. The elements of an array are of the same data types and each item can be accessed using the same name with their index. Their are two types of single dimensional array and multi dimensional array.
Stack is a linear data structure which uses the same principle that means, the elements in the stack are added or remove only from the one end which is called top. A stack is called a LIFO list, that means last in first out. In LIFO that element of that one inserted last is the first one to be taken out. 


Stack is implemented in computer memory by tow variables:-


·        TOP:- It is used to store the address of the top most elements of stack. Top is the position from where the elements are deleted or added.
·        MAX:-This variable is used to store maximum variable that the stack can store.
If(TOP==NULL) then it indicates the stack is empty.
If(TOP==MAX-1)then the stack is full so in stack.
 

Input : 6->5->4->3->2->1




Output: 1->2->3->4->5->6

Implementation of stack can be done into two  ways:-
·        Array representation of stack
·        Linked list representation of stack 

This programme is implemented by array. 

Array representation of stack :- 
 In the computer memory stack can be represented as the linear array. Every stack has a variable which is called top and which is used  to store address of the top most element of the stack. Top is the position where the elements will be added to or deleted form.The another variable. Memory is also use will stack which stored the maximum number of elements that a stack can hold. 
IF top ==NULL that means stack is empty and IF top=max-1 that means stack is full. 

Types of basic operation in stack:-
·        PUSH:-The push operation is used to insert an element to the stack, always new elements is added at tgr top most position of the stack before inserting any value in the stack we must first check if top=max-1 that means the stack is full and no more insertion is possible and print a message for overflow.

Algorithm:-
Step 1:- if top=max-1
Print overflow
Go to step 4
[end of if]
Step 2:- set top=top +1
Step 3:- set st[top] =val
Step 4:- exit.

·        Pop:-The pop operation is used to delete the top most element from the stack but before deleting the value, we must check if top==NULL then print a message of underflowthat means no more element can be deleted because stack becomes empty.

Algorithm:-
Step 1:- if top=NULL 
Print underflow
Goto step 4
[end of if]
Step 2:- set val=st[top]
Step 3:- set top=top-1
Step 4:- exit

         PEEK:- It is an operation that returns the value of top most elements of the stack without deleting it from the stack. 

Algorithm:-
Step 1:- if top==NULL
Print stack is empty
Goto step [end of if]
Step 2:- return st[top]
Step 3:- exit. 


           Program starts with the header file #include<stdio. h>, #include<conio.h>.  Then we define the maximum size of array that is 5. Then we initialize two variables one is array variablest[max] of int type of max size and  one is top ofint type. Then write function prototype in which the program is based on. 

      There are 4 functions:-
·        Push:- To push the elements in stack of void type with two arguments st and val.
·        Pop:- To delete the last element in stack of int type with one argument st.
·        Pep:-To know the last element if stack of int type with one argument st.
·        Display :- To display the stack of void type with one argument st.

Then themain() starts which is called the driver of all the functions. In the main() we first declare two variables option, valof int type, then we use clrscr() which is use to clear the console. Our program is menu driven program. Firstly we print the messages for user convince. Then we start our program by writing switch()  keyword and passing option in that. We define case 1 for pushing the value  in stack by using push(), case 2to pop up the value by using pop(), case 3to pep the value by using pep(), case 4 to display the values by using display() and at last case 5 to exit. And then our loop ends if the condition is goes false.
Then we write the function definitions :-
·        Firstly we write the push() definition. To push the elementsin the stack.
·        Then we write the pop() definition. To pop the elements from the stack.
·        Then we write the pep() definition. To pep the value from the stack.
·        Then we write the display() definition. To display all the elements present in the stack.



 

Program:-

PUSH AN ELEMENT IN STACK USING ARRAY


#include<conio.h>
#include<stdio.h>
#define MAX 5

int st[MAX];
int top=-1;

void push(int st[],int val);
int  pop(int st[]);
int pep(int st[]);
void display( int st[] );

void main()
{
  int option ,val;
  clrscr();
  do{
  printf("\n main menu\n");
  printf("\n1:PUSH\n");
 printf("\n2:POP\n");
 printf("\n3:PEEP\n");
  printf("\n4:DISPLAY");
  printf("\n5:EXIT");
  printf("\nenter your option:   ");
  scanf("%d",&option);


  switch(option)
  {
  case 1: printf("\n enter value to insert in stack\n");
      scanf("%d",&val);
      push(st,val);
      break;

  case 2: val=pop(st);

         if(val!=-1)
        // printf("\n underflow");
         //else
         printf("\n %d deleted from the stack ",val);


       break;
  case  3: val=pep(st);
       if(val!=-1)
       printf("\n%d is  stored at top ",val);
       break;

 case 4: display(st);
     break;
case 5:exit(0);
default :
printf("not a correct option") ;
}
}
while(option!=5);
 getch();

 }

void push(int st[],int val)
{
if(top==MAX-1)
{
printf("\nover flow");
}
else
 {
 top++;
 st[top]=val;
 }
}

  int pop(int st[])
  {
  int val;
   if(top==NULL)
   {
   printf("\nunder flow");
   }
   else
   {
   val=st[top];
   top--;
   }
   return val;
   }

   int pep(int st[])
   {
   if(top==-1)
   {
   printf("\nstack is empty");
   return-1;
   }
   else
  {
  return(st[top]) ;
  }
  }

    void display(int st[])
    {
    int i;
    if(top==NULL)
    {
    printf("\nstack is empty");
    }
    else
    {
    for(i=top;i>=0;i--)

    {
    printf("\n%d",st[i]);
    }

    }
    }


Post a Comment

Previous Post Next Post