You are on page 1of 6

Manual # 10

Topic:

Queues

Subject:

Data Structures and Computing

Author(s): Engr. Haleema Asif

Queues
1.

Implementation of Queues through Arrays

#include <iostream.h> class queue { private: int array[100]; int front; int rear; public: queue(); void addq(int item); int deleteq(); bool full(); bool empty(); void display(); int n; }; void queue::addq(int item) { if(full()) {cout<<"\n Queue is full"<<endl; return; } rear++; array[rear]=item; if(front==-1) front=0; } void queue::display() { for(int i=front;i<=rear;i++) {cout<<array[i]<<endl;} } int main() { int b; queue a;

a.empty(); a.deleteq(); cout<<" Enter the size of Queue " ; cin>>n; for(int i=0;i<n;i++) { cout<<" Enter the Element to add in queue : "; cin>>b; a.addq(b); cout<<endl; } a.display(); cout<<"Element is poped : "<<a.deleteq()<<endl; cout<<"Element is poped : "<<a.deleteq()<<endl; cout<<"Element is poped : "<<a.deleteq()<<endl; a.display(); return 0; } Lab Assignment: Q: 1 Use above program to create a queue of 15 elements. Then: 1 Use addq() method to create the queue and fill the it with user defined elements. 2. Implement bool empty() function for queues and display the queue. 3. Implement bool full() method to check whether queue is full or not. 4. Use deleteq() method to remove first 3 elements from the queue. 5. Use display() function again to display the edited queue. 6.Implement a while loop in the main method along with the empty() method to deleteq() all the elements. Then display() the queue.

Implementation of Queues through Link-Lists


#include <iostream.h> #include <conio.h> class queue { private: struct node { int data; node *link; }; node *front,*rear; public: queue(); void addq(int item); int deleteq(); bool empty(); void display(); }; void queue::display() { { node *temp; temp=front; while(temp!=NULL) cout<<temp->data<<endl; temp=temp->link; } } void queue::addq(int item) { node *temp; temp= new node; if(temp==NULL) { cout<<"\n Queue is Empty "; } temp->data=item; temp->link=NULL; if(front == NULL) {

rear=front=temp; } rear->link=temp; rear=rear->link; } int n; int main() { int b; queue a; a.deleteq(); cout<<"\n Enter the size of Queue " ; cin>>n; for(int i=0;i<n;i++) { cout<<" \n Enter the Element to add in queue : "; cin>>b; a.addq(b); cout<<endl; } a.display(); cout<<"Element is poped : "<<a.deleteq()<<endl; cout<<"Element is poped : "<<a.deleteq()<<endl; cout<<"Element is poped : "<<a.deleteq()<<endl; a.display(); return 0; } Lab Assignment: Q: 2 Use above program to create a queue of 10 elements. Then: Create object for queue class in main method and use that to call the class methods. Use addq() method to create the queue and fill the it with user defined elements. Implement display() function for queues and display the queue. Implement deleteq() method to remove elements from the front of the queue Use display() function again to display the edited queue. Implement a while loop in the main method along with the empty() method to dequeue() all the elements. Then display() the queue.

Q 3: Consider a parking garage with the following properties: It has only one lane that holds upto 10 cars. Cars arrive at the south end and leave from the north end. If a customer arrives to pick up his car that is parked in the middle, all the cars to the north of that car are moved out, his car is then driven out and the other cars are restored in the same order that they were in originally. Whenever a car leaves from anywhere in the queue, all the cars to the south of it are moved forward so that all the empty spaces are in the south part of the garage. Implement a program that reads the input cars. And: A struct car that contains a license plate number. Show a message whenever a car arrives or departs the parking lane. When a car arrives, a message should specify whether there is room for the car in the parking space or not. If there is no room for a car, the car waits until there is room. When the room becomes available, another message should be printed.

You might also like