Google Search

Sunday, January 29, 2012

C PROGRAM TO IMPLEMENT STACK USING ARRAY


ALGORITHM TO IMPLEMENT STACK USING ARRAY

INSERTION
PUSH(item)
1.  If (item = max of stack)
     Print “overflow”
     Return
2.  top = top + 1 
3.  stack[top] = item
4.  Return

DELETION
POP(item)
1.  If (top = - 1)
     Print “underflow”
     Return
2.  Item = stack[top]
3.  top = top – 1
4.  Return

DISPLAY
1.  If  top = - 1
     Print “underflow”
2.  repeat step 3 for i = top to i >= 0
3. Print stack[i]
4.  Return







#include<stdio.h>
#include<conio.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top=-1;
void main()
{
 int choice;
 char ch;
 do
  {
    clrscr();
    printf("\n1. PUSH ");
    printf("\n2. POP ");
    printf("\n3. TRAVERSE ");


    printf("\nEnter your choice");
    scanf("%d",&choice);
    switch(choice)
     {
       case 1:  push();
  break;
       case 2:  printf("\nThe deleted element is %d",pop());
  break;
       case 3:  traverse();
  break;
       default: printf("\nYou Entered Wrong Choice");
       }
    printf("\nDo You Wish To Continue (Y/N)");
    fflush(stdin);
    scanf("%c",&ch);
    }
 while(ch=='Y' || ch=='y');
}

void push()
{
 int item;
 if(Top == MAXSIZE - 1)
  {
    printf("\nThe Stack Is Full");
    getch();
    exit(0);
    }
 else
  {
    printf("Enter the element to be inserted");
    scanf("%d",&item);
    Top= Top+1;
    stack[Top] = item;
    }
}

int pop()
{
 int item;
 if(Top == -1)
  {
    printf("The stack is Empty");
    getch();
    exit(0);
    }
 else
   {
    item = stack[Top];
    Top = Top-1;
    }
return(item);
}

void traverse()
{
 int i;


 if(Top == -1)
  {
    printf("The Stack is Empty");
    getch();
    exit(0);
    }
 else
  {
    for(i=Top;i>=0;i--)
     {
      printf("Traverse the element");
       printf("\n%d",stack[i]);
       }
    }

0 comments:

Post a Comment