Saturday 17 September 2011

STACK IMPLEMENTATION


Implementation of Stack
Aim:
To write a C++ program to implement stack using array.
Algorithm:
Step 1: Create a class as stk.
Step 2: Declare the data members as x, top, stack[10].
Step 3: Declare the member function as
stk()
void push()
void pop()
void display()
Step 4: Default constructor is used to initialize the value to zero.
Step5: In the push function,
5.1 Check the stack for overflow.
5.2 Increment the top value.
5.3 Read the value to be pushed to the stack.
5.4 Push the value into the stack.
5.5 Display the pushed value.
Step 6: In the pop function,
6.1 Check the stack for underflow.
6.2 Pop the value from the stack.
6.3 Decrement the top value.
6.4 Display the value that is popped from the stack.
Step 7: display () function is used to display the stack content.
Step 8: In the main, create the object for the class stk.
Step 9: In the do-while loop, it display the choice as push, pop and display.
9.1 If the choice is 1, call push function.
9.2 If the choice is 2, call pop function.
9.3 If the choice is 3, call display function.

Program:
#include<iostream.h>
#include<conio.h>

class stk
{
 int x,top,stack[10];
 public:
       stk()
       {
top=0;
       }
       void push();
       void pop();
       void display();
};

void stk::push()
{
  if(top==10)
    cout<<"\nSTACK FULL";
  else
    top=top+1;
    cout<<"\nENTER THE NUMBER TO BE PUSHED:";
    cin>>x;
    stack[top]=x;
    cout<<"\nTHE VALUE IS PUSHED INTO THE STACK IS:"<<x;
}

void stk::pop()
{
   if(top==0)
  cout<<"\nSTACK EMPTY\n:";
  else
   {
  x=stack[top];
  top=top-1;
  cout<<"\nTHE VALUE RETREIVED FROM STACK IS:"<<x;
   }
}

void stk::display()
{
  cout<<"\nCONTENTS OF STACK:";
 for(int i=top;i>0;i--)
   {
  cout<<"\n"<<stack[i]<<"\t";
   }
}

void main()
{
 clrscr();
  stk s;
  int a;
 cout<<"\n\t\t\t*****\n";
 cout<<"\n\t\t\tSTACK\n";
 cout<<"\n\t\t\t*****\n";
 do
 {
cout<<"\n\n\t\t1.PUSH \n\t\t2.POP \n\t\t3.DISPLAY
        \n\t\t4.EXIT";

  cout<<"\n\t\tENTER THE CHOICE:";
  cin>>a;
  switch(a)
  {
  case 1:
s.push();
break;
  case 2:
s.pop();
break;
    case 3:
s.display();
break;
  default:
cout<<"\nINVALID CHOICE";
  }
  } while(a<4);
getch();
}

Output:
                         *****
                        STACK
                        *****
                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:1

ENTER THE NUMBER TO BE PUSHED:67

THE VALUE IS PUSHED INTO THE STACK IS:67

                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:1

ENTER THE NUMBER TO BE PUSHED:89

THE VALUE IS PUSHED INTO THE STACK IS:89

                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:3

CONTENTS OF STACK:
89
67
                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:2

THE VALUE RETREIVED FROM STACK IS:89

                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:3

CONTENTS OF STACK: 67
                1.PUSH
                2.POP
                3.DISPLAY
                4.EXIT
                ENTER THE CHOICE:4

INVALID CHOICE

0 comments:

Post a Comment