Friday, 15 November 2013

4. Write a C++ program to create a class called STACK using an array of integers. Implement the following operations by overloading the operators + and – i) s1=s1+element; where s1 is an object of the class STACK and element is an integer to be pushed on the top of the stack. ii) s1=s1-; where s1 is an object of the class STACK - operator pops the element. Handle the STACK empty and STACK full conditions. Also display the contents of the stack after each operation, by overloading the operator <<

#include<iostream.h> 
#include<stdlib.h> 
#include<conio.h> 

const int SIZE=5; //Stack size 

//class declaration 
class stack 
 private: int items[SIZE]; 
             int top; 
             int full(); 
             int empty(); 

 public:  stack() 
 top=-1; 
 } 
 stack operator--(int); 
 friend stack operator+(stack s1,int elem); 
 friend ostream &operator<<(ostream &os,stack &s1); 
}; 

// checking for Stack overflow 
int stack::full() 
 if(top==SIZE-1) 
 return 1; 
 else 
 return 0; 

//Checking for stack under flow. 

int stack::empty() 
 if(top==-1) 
 return 1; 
 else 
 return 0; 

//function for element deletion from the stack 
stack stack::operator--(int ) 

 if(empty()) 
 { 
 cout<<"Stack underflow\n"; 
 } 

 else 
 {
 cout<<"\nThe element deleted is :" <<items[top]; 
 stack t; 
 t.top=--top; 
 for(int i=0;i<=top;i++) 
 t.items[i]=items[i]; 
return *this; 

ostream &operator<<(ostream &os,stack &s1) 
 for(int i=s1.top;i>=0;i--) 
 {
     os<<s1.items[i]<<"\n"; 
 }
return os; 
}

//function for element insertion on to the stack 
stack operator+(stack s1,int elem) 
 if (s1.full()) 
 cout<<"\nStack overflow\n"; 
 else 
 s1.items[++(s1.top)]=elem; 
 return s1; 


void main()  
 stack s1; 
 int choice,elem; 
 clrscr(); 
 for(;;) 
 { 
 cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n" 
 <<"enter your choice:"; 
 cin>>choice; 
 switch(choice) 
 { 
 case 1: 
 cout<<"Enter the element to be inserted:"; 
 cin>>elem; 
 s1=s1+elem; 
 break; 
 case 2: 
 s1=s1--; 
 break; 
 case 3: 
 cout <<"The contents of the stack are :\n" 
 <<s1; 
 break; 

 case 4: exit(0); 
 default: 
 cout <<"Invalid choice\n"; 
 getch(); 
 exit(0); 
 } 
 } 



OUTPUT 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:2 
Stack underflow 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:1 
Enter the element to be inserted:20 

1:PUSH 2:POP 3:DISPLAY 4:EXIT Bangalore Institute of Technology C++ Lab Manual 

Department of Information Science and Engineering 23 
enter your choice:1 
Enter the element to be inserted:45 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:1 
Enter the element to be inserted:51 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:1 
Enter the element to be inserted:62 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:1 
Enter the element to be inserted:77 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:3 
The contents of the stack are : 
77 
62 
51 
45 
20 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:2 
The element deleted is :77 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:2 
The element deleted is :62 

1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:3 
The contents of the stack are : 
51 
45 
20 
1:PUSH 2:POP 3:DISPLAY 4:EXIT 
enter your choice:4 


No comments:

Post a Comment