click on this link......

LinkGrand.com

Thursday 29 August 2013

Stack using array

/* Program to implement stack using array */

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

void push();
void pop();
void display();

int stack[MAX], top=-1, item;

main()
{
          int ch;
          do
          {
                   printf("\n\n 1. push \n 2. Pop \n 3. Display \n 4. Exit \n");
                   printf("\n Enter your choice:");
                   scanf("%d",&ch);
                   switch(ch)
                   {
                           case 1:
                                    push();
                                    break;
                           case 2:
                                    pop();
                                    break;

                           case 3:
                                    display();
                                    break;
                            case 4:
                                    exit(0);
                            default: 
                                 printf("\n Invalid choice. Please try again\n");
                 }
         }while(ch!=4);
         getch();
}

void push()
{
           if(top==MAX-1)
                       printf("\n Stack is full");
          else
          {
                   printf("\n Enter item:");
                   scanf("%d",&item);
                   top++;
                   stack[top]=item;
                   printf("\n Iem inserted = %d",item);
            }
}

void pop()
{
          if(top==-1)
          {
                  printf("\n Satck is empty");
           }
           else
           {
                  item=satck[top];
                  top--;
                  printf("\n Item deleted = %d",item);
           }
}

void display()
{
             int i;
            if(top==-1)
                printf("\n Stack is empty");
           else
           {
                   for(i=top;i>=0;i--)
                            printf("\n%d",satck[i]);
           }
}
  

No comments:

Post a Comment