|
| 1 | +#include<iostream> |
| 2 | +using namespace std; |
| 3 | +class Queue |
| 4 | +{ |
| 5 | +public: //all public needed to access outside in main |
| 6 | +string name; |
| 7 | +int roll_number; |
| 8 | +double total_marks; |
| 9 | +Queue *next; |
| 10 | +Queue() |
| 11 | +{ |
| 12 | +name="Nishkarsh"; |
| 13 | +roll_number=41; |
| 14 | +total_marks=256.60; |
| 15 | +} |
| 16 | +~Queue() |
| 17 | +{ |
| 18 | +cout<<"Object Destroyed"<<endl; |
| 19 | +} |
| 20 | +void show() |
| 21 | +{ |
| 22 | +cout<<endl; |
| 23 | +cout<<"Name of the person is "<<name<<endl<<"Roll number of the person is "<<roll_number<<endl<<"Total marks of the person are "<<total_marks<<endl; |
| 24 | +} |
| 25 | +}*rear,*front,*node,*ptr,*p; |
| 26 | +int main() |
| 27 | +{ |
| 28 | +int quit=0; |
| 29 | +cout<<"Hello! This is a program to demonstrate Queue implementation using Classes and Objects"<<endl; |
| 30 | + node=new Queue; |
| 31 | + cout<<"Enter the name of this person"<<endl; |
| 32 | + cin>>node->name; |
| 33 | + cout<<"Enter the roll number of this person"<<endl; |
| 34 | + cin>>node->roll_number; |
| 35 | + cout<<"Enter the total marks of this person"<<endl; |
| 36 | + cin>>node->total_marks; |
| 37 | + node->next=NULL; |
| 38 | +front=node; |
| 39 | +rear=node; |
| 40 | +node->show(); |
| 41 | +cout<<endl; |
| 42 | +cout<<"Let the party begins"<<endl; |
| 43 | +do |
| 44 | +{ |
| 45 | +cout<<"\t\t\t\tOption Menu"<<endl; |
| 46 | +cout<<"1) Insert"<<endl; |
| 47 | +cout<<"2) Delete"<<endl; |
| 48 | +cout<<"3) Show"<<endl; |
| 49 | +cout<<"4) Exit"<<endl; |
| 50 | +int ch; |
| 51 | +cout<<"Enter your choice"<<endl; |
| 52 | +cin>>ch; |
| 53 | +switch(ch) |
| 54 | +{ |
| 55 | +case 1: cout<<"Insertion at end"<<endl; |
| 56 | + p=new Queue; |
| 57 | + cout<<"Enter the name of this person"<<endl; |
| 58 | + cin>>p->name; |
| 59 | + cout<<"Enter the roll number of this person"<<endl; |
| 60 | + cin>>p->roll_number; |
| 61 | + cout<<"Enter the total marks of this person"<<endl; |
| 62 | + cin>>p->total_marks; |
| 63 | + rear->next=p; |
| 64 | +p->next=NULL; |
| 65 | +rear=p; |
| 66 | +break; |
| 67 | +case 2: cout<<"Deletion at beginning"<<endl; |
| 68 | +cout<<"Object to be deleted is"<<endl; |
| 69 | +front->show(); |
| 70 | +ptr=front; |
| 71 | +front=front->next; |
| 72 | +delete ptr; |
| 73 | +break; |
| 74 | +case 3: cout<<"Traversal"<<endl; |
| 75 | +for(ptr=front;ptr!=NULL;ptr=ptr->next) |
| 76 | +{ |
| 77 | +int i=1; |
| 78 | +cout<<"The "<<i++<<" Record is of"; |
| 79 | +ptr->show(); |
| 80 | +} |
| 81 | +break; |
| 82 | +case 4: quit=1; |
| 83 | +break; |
| 84 | +default: cout<<"Wrong Choice! Try again"<<endl; |
| 85 | +} |
| 86 | +}while(quit!=1); |
| 87 | +return 0; |
| 88 | +} |
0 commit comments