Saturday, 22 April 2017

HOME Tutor C++ and JAVA in Govindpuram, Ghaziabad

C++ and JAVA Program for stack
Example 8.1 Program to implement stack on Array
#include <iostream.h>

int MAXSIZE = 8;      
int stack[8];    
int top = -1;           

int isempty()
{

   if(top == -1)
      return 1;
   else
      return 0;
}
  
int isfull()
{
   if(top == MAXSIZE)
      return 1;
   else
      return 0;
}

int peek()
{
   return stack[top];
}

int pop()
{
int data;
      data = stack[top];
      top = top - 1;  
      return data;
}

int push(int data)
{
      top = top + 1;  
      stack[top] = data;
}

void pip()
{
   int i = top;
   while(i >= 0)
   {
      cout << stack[i]<<”\t”;
      i--;
   }
}

int main()
{
      int item, ch;
      do
      {
            cout<<"\n 1- PUSH
 \n 2- POP
 \n 3- Display all items
 \n 4- Display top item
 \n 5- Exit
 \n Enter Ur choice:";
            cin >> ch;

            switch(ch)
            {
                  case 1:     cout << "\n Enter item to push:";
                              cin >> item;
                              if(!isfull())
      push(item);
      else
cout <<”\n Stack is full.\n";
                              break;
                  case 2:    
if(!isempty())
{
item = pop();
cout <<item << “is deleted \n”;
                              }
else
      cout <<"\n Stack is empty.\n";
break;     
case 3:     if(!isempty())
      pip();
else
      cout <<"\n Stack is empty.\n";

                              break;
case 4:     if(!isempty())
{
item = peek();
cout <<item << “is on top of Stack \n”;
                              }
else
      cout <<"\n Stack is empty.\n";

default:    cout << “\n Exit from program”;
                  }
            }while(ch<5);

   return 0;
}
Output

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 10
Enter item to push:1

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 1
Enter item to push: 20

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 1
Enter item to push: 30

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 3
30    20    10

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 4
30 is on top of Stack  

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 2
30 is deleted

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 3
20    10

 1- PUSH
 2- POP
 3- Display all items
 4- Display top item
 5- Exit
 Enter Ur choice: 5
Exit from program.




The implementation of stack using linked list is also possible. The linked list implementation of stack provides the support to add unlimited data and no memory wastage is there because linked list creates the new node when new data is arrived to push and on the request of pop the node is deleted from memory. Therefore, don’t need the function isempty() and isfull() function like array implementation of stack. Let see an example to understand the concept of stack implementation on the linked list:  



























- CBSE
- CBSE Curriculum, CBSE study material, CBSE XI sample papers, CBSE XI previous year question papers
- CBSE textbooks , CBSE books download, CBSE XII sample papers, CBSE XII previous year question papers,
- CBSE IX sample papers , CBSE X sample papers , CBSE X previous year papers, CBSE syllabus, CBSE classes
- CBSE Computer Sc Question papers, CBSE model papers, CBSE coaching in Govindpuram, Ghaziabad.
- C++ Classes, JAVA Classes,  C#.NET classes
- C++ in Govindpuram, C++ Coaching in Govindpuram, C++ Tuition in Govindpuram, C++ Classes in Govindpuram
- JAVA in Govindpuram, JAVA Coaching in Govindpuram, JAVA Tuition in Govindpuram, JAVA Classes in Govindpuram
- C#.NET in Govindpuram, C#.NET Coaching in Govindpuram, C#.NET Tuition in Govindpuram, C#.NET Classes in Govindpuram
- English Speaking in Govindpuram, English Speaking Coaching in Govindpuram, English Speaking Tuition in Govindpuram, English Speaking Classes in Govindpuram
- Spoken English in Govindpuram, Spoken English Coaching in Govindpuram, Spoken English Tuition in Govindpuram, Spoken English Classes in Govindpuram
- C++ in Ghaziabad, C++ Coaching in Ghaziabad, C++  Tuition in Ghaziabad, C++ Classes in Ghaziabad
- JAVA in Ghaziabad, JAVA Coaching in Ghaziabad,  JAVA Tuition in Ghaziabad, JAVA Classes in Ghaziabad
- C#.NET in Ghaziabad, C#.NET Coaching in  Ghaziabad, C#.NET Tuition in Ghaziabad, C#.NET Classes in Ghaziabad
- English Speaking in Ghaziabad, English Speaking Coaching in Ghaziabad, English Speaking Tuition in Ghaziabad, English Speaking Classes in Ghaziabad
- Spoken English in Ghaziabad, Spoken English Coaching in Ghaziabad, Spoken English Tuition in Ghaziabad, Spoken English Classes in Ghaziabad
- C++ training in Govindpuram, C++ institute in Govindpuram, C++ jobs in Govindpuram, C++ center in Govindpuram
- C++ training in Ghaziabad, C++ institute in Ghaziabad, C++ jobs in Ghaziabad, C++ center in Ghaziabad
- JAVA training in Govindpuram, JAVA institute in Govindpuram, JAVA jobs in Govindpuram, JAVA center in Govindpuram
- JAVA training in Ghaziabad, JAVA institute in Ghaziabad, JAVA jobs in Ghaziabad, JAVA center in Ghaziabad
- C#.NET training in Govindpuram, C#.NET institute in Govindpuram, C#.NET jobs in Govindpuram, C#.NET center in Govindpuram
- C#.NET training in Ghaziabad, C#.NET institute in Ghaziabad, C#.NET jobs in Ghaziabad, C#.NET center in Ghaziabad
- english speaking course in ghaziabad, english speaking course in ghaziabad,english speaking course in ghaziabad, utter pradesh,english speaking course in ghaziabad,english speaking course in ghaziabad,

No comments:

Post a Comment