Tuesday 9 September 2014

C Program to implement stack using array


A stack is a data structure which is a  logical representation of  a real physical stack or pile of objects, where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of the data set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. In a stack last added item is deleted first and so it is called a LIFO (Last In First Out) data structure.





Following is the implementation of a stack in C programming language by using array. Functions implemented in this program are:

1.  void push(int value) -  to add a new value to the stack
2.  int pop() - to take off or pop a value from the stack
3.  int top() - to access the value at the top of the stack
4.  int getsize()  - to get the number of values in the stack
5.  int isfull() - to check whether the stack is full or not
6.  int isempty() - to check whether the stack is empty or not
7.  void trace()  - to display the content of the stack

Source Code

#include<stdio.h>
#include<conio.h>

#define size 5

// ----------------------------- Stack Structure --------------------------------------
// Defines the structure (MyStack) to hold data
struct MyStack {
   int data[size];
   int top;
};

// Declares a new variable astack of type MyStack
struct MyStack astack;
// ------------------------------------------------------------------------------------

// ----------------------------- Stack Functions --------------------------------------
// Function to push new value to the stach
void push(int value) {
   astack.top++;
   astack.data[astack.top] = value;
}

// Funtion to pop a value from the stack
int pop() {
   int value;
   value = astack.data[astack.top];
   astack.top--;
   return (value);
}


// Funtion to get the top value from the stack without poping it
int top() {
   int value;
   value = astack.data[astack.top];
   return (value);
}

// Funtion to get the number of values in the stack
int getsize() {
   return (astack.top+1);
}


// Funtion to check whether the stack is full. Returns 1 if stack is full else returns 0
int isfull() {
   if (astack.top >= size - 1)
      return 1;
   else
      return 0;
}

// Funtion to check whether the stack is empty. Returns 1 if stack is empty else returns 0
int isempty() {
   if (astack.top == -1)
      return 1;
   else
      return 0;
}

// Funtion to display the content of the stack
void trace() {
   int i;
   if (isempty())
      printf("\nSorry, stack is empty!");
   else {
      for (i = astack.top; i >= 0; i--)
         printf("\n%d", astack.data[i]);
   }
}
// ---------------------------------------------------------------------------------------




// ---------------------------------- Main function to test the stack ---------------------
int main() {
   int value, ch;
   astack.top = -1;

   printf("\n\tImplementation of stack using array");
   do {
      printf("\nSelect an option");
      printf("\n1.Push \n2.Pop \n3.Top \n4.Size \n5.Trace \n6.Exit");
      printf("\nEnter your choice:");
      scanf("%d", &ch);
     
      switch (ch) {
      // Push
      case 1:
         printf("\nEnter the value for the item to be pushed:");
         scanf("%d", &value);
         if (isfull())
            printf("\nSorry, stack is full!");
         else
            push(value);
         break;
      // Pop
      case 2:
         if (isempty())
            printf("\n Sorry, stack is empty!");
         else {
            value = pop();
            printf("\nPopped value  is: %d", value);
         }
         break;
      // Top
      case 3:
         if (isempty())
            printf("\n Sorry, stack is empty!");
         else {
            value = top();
            printf("\nValue at the top of stack is: %d", value);
         }
         break;
      // Size
      case 4:              
         printf("\nSize of the stack is: %d", getsize());        
         break;
      // Trace
      case 5:
         trace();
         break;
      // Exit from the program
      case 6:
         exit(0);
      }
      printf("\nPress any key to continue...");   
      getch();
       
   } while (ch!=6);

return 0;
}
// ---------------------------------------------------------------------------------------

No comments:

Post a Comment