Topic – 2
Queue
Previous years Examination Questions
4 Marks Questions
Question 1:
Write the definition of a member function DELETE( ) for a class QUEUE in C++, to remove a product from a dynamically allocated Queue of products considering the following code is already written as a part of the program. All India 2016
struct PRODUCT { int PID; char PNAME[20]; PRODUCT *Next; }; class QUEUE { PRODUCT *R,*F; public: QUEUE(){R=NULL;F=NULL;} void INSERT(); void DELETE(); ∼QUEUE(); };
Аnswer:
void QUEUE :: DELETE() { if(F!=NULL) { PRODUCT *Temp=F; cout<<F->data<<"deleted\n"; F=F->Next; delete Temp; if(F==NULL) R=NULL; } else cout<<"\n Queue is Empty": }
Question 2:
Write the definition of a member function INSERT( ) for a class QUEUE in C++, to insert an ITEM in a dynamically allocated Queue of items considering the following code is already written as a part of the program. Delhi 2016
struct ITEM { int INO; char INAME[20]; ITEM *Link; }; class QUEUE { ITEM *R, *F; public: QUEUE() {R=NULL; F=NULL;} void INSERT(); void DELETE(); ∼QUEUE(); };
Аnswer:
void QUEUE :: INSERT!) { ITEM *temp=new ITEM; cout<<"\n Enter Item no & Item name:"; cin>>temp->INO; gets(temp->IName); temp->Next = Null; if(R==Null) { R = temp; F = temp; } else { R->Next = temp; R = temp; } }
Question 3:
Write the definition of a member function INSERT! ) in C++, to add a new passenger detail in a dynamic queue of PASSENGERS considering the following code is already existing in the program. All India 2015
struct PASSENGERS { char PID[20]; NAME[80]; PASSENGERS *Next; }; class QUEUE { PASSENGERS *Rear, *Front; public: QUEUE() {Rear=NULL; Front=NULL} void INSERT(); void DELETE(); ∼QUEUE(); };
Аnswer:
void QUEUE ::INSERT() { PASSENGERS * P = new PASSENGERS; cout<<"Enter PID"; cin>>P->PID; cout<<"Enter Name”; gets(p->Name); P->Next = NULL; if(Rear == NULL) { Front = Rear = P; } else { Rear->Next = P; Rear = P; } }
Question 4:
Write a function QDELETE( ) in C++ to perform delete operation in a Linked Queue, which contains Passenger number and Passenger name. Consider the following definition of node in the code. All India 2013
struct node { long int Pno; char Pname[20]; node *Link; };
Аnswer:
void QDELETE() { if(Front == NULL) { cout<<"Queue is empty"; exit(0); } else { node *temp = front; cout<<"Passenger Information"; cout<<Front->Pno; cout<<Front->Pname; Front=Front->Link; delete temp; } }
Question 5:
Write a function QINSERT( ) in C++ to perform insert operation on a linked queue, which contains client number and client name. Consider the following definition of NODE in the code of QINSERT ( ). Delhi 2013
struct NODE { long int Cno; //Client number char Cname[20]; //Client name NODE *Next; };
Аnswer:
void QINSERT() { NODE *P = new Node; cout<<"Enter the Client Number and Name:"; cin>>P- Cno; gets(P->Cname) ; P->Next=NULL; if((front ~ NULL)&&(rear == NULL)) { front=rear=P; } else { rear->Next = P; rear = P; } }
Question 6:
Given the necessary declaration of linked implemented Queue containing players information (as defined in the following definition of Node). Also, write a user defined function in C++ to delete one Player’s information from the Queue.
Delhi 2013C
struct Node { int PlayerNo; char PIayerName[20]; Node *Link; };
Аnswer:
void Delete_NODE() { NODE *P; if(front == Null) cout<<”Queue is empty"; else if(front == rear) { P = front; cout<<"Deleted Node information is"; cout<<P->PlayerNo; puts(p->PlayerName); front = rear = NULL; delete P; } else { P = front; cout<<"Deleted Node information is"; cout<<P->PlayerNo; puts(P->PlayerName); front = front->Link; delete P; } }
Question 7:
Write a function in C++ to perform insert operation in a dynamic queue containing DVD’s information (represented with the help of an array of structure DVD). Delhi 2012
struct DVD { long No; char title[20]; DVD *Link; };
Аnswer:
void Insert() { DVD *p = new DVD; cout<<"Enter the DVD number and Title"; cin<<p->No; gets(p->title); p->Link = NULL; if((front — NULL) && (rear == NULL)) { front - rear = p; } else { rear->Link = p; rear = p; } }
Question 8:
Write a function in C++ to perform insert operation in a static circular queue containing book’s information (represented with the help of an array of structure BOOK). All India 2012
struct BOOK { long AccNo; //Book account number char Title[20]; //Book Title };
Аnswer:
void circularQueInsert(BOOK B[], int Front, int Rear, int Max) { if((Rear=Max-l && Front ==0)||(Front == Rear +1)) { cout<<"Circular Queue is full"; exit(O); } else if(Front == -1 && Rear == -1) { Front = Rear = 0; } else if(Rear = Max -1 && Front >0) { Rear = 0; } else Rear = Rear+1; cout<<"Enter book account number and title"; cin>>B[Rear].AccNo; gets(B[Rear].Title); }
Question 9:
Write a function in C++ to perform insert operation on a dynamically allocated queue containing passenger details as given in the following definition of NODE; Delhi 2011
struct NODE { long Pno; //Passenger Number char Pname[20]; //Passenger Name NODE *Link; };
Аnswer:
void Enter() { NODE *nptr=new NODE; nptr->Link = NULL; cout<<"Enter name and number for new passenger"; gets(nptr->Pname); cin>>nptr->Pno; if(rear == NULL) { front = rear = nptr; } else { rear->Link = nptr; rear = nptr; } }
Question 10:
Write a function in C++ to perform delete operation on a dynamically allocated queue containing passenger details as given in the following definition of NODE: All India 2011
struct NODE { long Mno; //Member Number char Mname[20]; //Member Name NODE *Link; };
Аnswer:
void remove(NODE *front) { NODE *nptr; if(front == NULL) { cout<<"Queue is empty"; } else { nptr = front; front = front->Link; if(front == NULL) rear = NULL; delete nptr; }
Question 11:
Write a complete program in C++ to implement a dynamically allocated queue containing names of cities. All India 2010
Аnswer:
#include<iostream.h> #include<string.h> struct Node { char city[30]; Node *link; }; class queue { Node *front, *rear; public: queue() {front=rear=NULL;} void add_Q(); //add queue void del_Q(); //delete queue void show_Q(); //show queue }; void queue :: add_Q() { Node *temp = new Node; char ct[30]; cout<<"Enter city"; gets(ct); strcpy(temp->city,ct); temp->link = NULL; if(rear == NULL) front = rear = temp; else { rear->link = temp; rear = temp; } } void queue :: del_Q() { Node *temp; char ct[20]; if(front == NULL) { cout<<”queue is empty"; } else { temp = front; front = front->link; strcpy(ct, temp->city); cout<<"Deleted values are"<<ct; delete temp; } if(front == NULL) rear = front; } void queue :: show_Q() { Node *temp = front; cout<<"The queue elements are"; while(temp!=NULL) { cout<<"\n"<<temp->city; temp = temp->l ink; } } void main() { int choice; queue QUEUE; char opt='Y'; do { cout<<"\nMain Menu"; cout<<"\nl.Insertion in queue"; cout<<"\n2.Deletion from queue"; cout<<"\n3.Traversal of queue"; cout<<"\n4.Exit from queue"; cout<<"\nEnter your choice from above(1,2,3,4)"; cin<<choice; switch(choice) { case 1: do { QUEUE.add_Q(); cout<<"\ndo you want to add more elements<Y/N>?"; cin>>opt; } while(toupper(opt)=='Y'); break; case 2: do { QUEUE.del_Q(); cout<<"\ndo you want to delete more elements<Y/N>?"; cin>>opt; } whi1e(toupper(opt)== 'Y'); break; case 3: QUEUE.show_Q(); break; case 4; exit(0); } } while(choice!=4); }
Question 12:
Write a function QUEINS( ) in C++ to insert an element in a dynamically allocated queue containing nodes of the following given structure: Delhi 2009
struct Node { int Pid; //Product Id char Pname[20]; //Product Name Node *Next; };
Аnswer:
void QUEINS(Node *rear, int val, char valid) { Node *temp=new Node; temp->Pid = val; strcpy(temp->Pname,val1); temp->Next = NULL; if(rear == NULL) rear = front = temp; else { rear->Next = temp; rear = temp; } }
Question 13:
Write a function QUEDEL( ) in C++ to display and delete an element in a dynamically allocated queue containing nodes of the following given structure: All India 2009
struct Node { int Itemno; char Itemname[30]; Node *Link; };
Аnswer:
void QUEDEL(Node *front) { Node *temp; if(front == NULL) cout<<"Queue is Empty"; else { temp = front; cout<<"deleted item="; cout<<front->Itemno; cout<<front->Itemname; front = front->Link; if(front == NULL) rear = NULL; delete temp; } }
0 comments:
Post a Comment
Thanks for leaving a comment. As soon as it's approved, it will appear.