0% found this document useful (0 votes)
39 views10 pages

Fatima Jinnah Women University: Task 1

The document contains code for implementing queue and circular queue data structures in C++. It defines classes Queue and CircularQueue with methods like enqueue(), dequeue(), isEmpty(), isFull() and display() to add and remove elements from the queues. The Queue class uses a static array to store elements while CircularQueue implements a circular buffer using modulo operation to allow insertion from front as rear reaches end of array. Examples are provided to demonstrate adding and removing elements from both queue implementations.

Uploaded by

Muneeba Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views10 pages

Fatima Jinnah Women University: Task 1

The document contains code for implementing queue and circular queue data structures in C++. It defines classes Queue and CircularQueue with methods like enqueue(), dequeue(), isEmpty(), isFull() and display() to add and remove elements from the queues. The Queue class uses a static array to store elements while CircularQueue implements a circular buffer using modulo operation to allow insertion from front as rear reaches end of array. Examples are provided to demonstrate adding and removing elements from both queue implementations.

Uploaded by

Muneeba Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

FATIMA JINNAH WOMEN UNIVERSITY

Department : Software Engineering

Name : Muneeba Mehmood

Section : B

Reg no : 2020-BSE-54

Subject : DATA STRUCTURE

Semester : III

Lab 5
Task 1 :

Give answers to the following.

Show the contents of a (linear) queue and position of front and rear markers (at
each step) once the

following sequence of statements is executed.


Queue Q;
1. Q.enqueue(10); 10
2. Q.enqueue(20); 10 ,20
3. Q.enqueue(30); 10,20,30
4. Q.dequeue(); 20,30
5. Q.dequeue(); 30
6. Q.enqueue(40); 30,40
7. Q.dequeue() 40
8. Q.dequeue() empty

Data Structures and Algorithms Page 1

2. Consider a circular QUEUE with N=8 memory cells. Find the number of
elements in QUEUE for the following positions of front and rear.

front = 0 ; rear = 4 ; 5

front = 2 ; rear = 0 ; 7

front = 4 ; rear = 6 ;

And two elements are dequeued; 1


3. Suppose q is an instance of a circular queue and the queue size is 4. Show the
contents of the queue and positions of the front and rear markers once the
following sequence of statements is executed. The initial contents of the
queue are listed in the following.

front
q.dequeue();
40
q.dequeue();
60
q.enqueue(15);
rear
80

q.enqueue(25); 15
q.enqueue(105); 25

Code Task # 01

Create a class Queue that implements the functionality of a queue providing


all the required operations (Enqueue(), Dequeue(), is_Empty(),
is_Full(),display() , getFront(), getRare()).

// queue.cpp : Defines the entry point for the console application.


//

#include "stdafx.h"
#include<iostream>
using namespace std;
int const q_size = 4;
typedef int q_element;
class queue
{
private:
q_element arr[q_size];
q_element front, rear;

public:
queue() //CONSTRUCTOR
{
front = rear = -1;
}

bool empty() //EMPTY FUNCTION


{
if(front == -1 && rear == -1)
return true;
else
return false;
}

bool full() //FULL FUNCTION


{
if(front==rear)
return true;
else
return false;
}

void frontVal() //RETRIEVE FRONT VALUE


{
cout<<"\nThe value of front is: "<<arr[front];
}

void rearVal() //RETRIEVE REAR VALUE


{
cout<<"\nThe value of rear is: "<<arr[rear];
}

void addQueue(q_element n) //ADD VALUE


{

if(rear == q_size - 1)
{
cout<<"Overflow Condition!\n";
}
else if(empty())
{
front++;
rear++;
arr[rear] = n;
}
else
{
rear++;
arr[rear]= n;
}
}

void deQueue() //DELETE VALUE


{
if(empty())
{
cout<<"Underflow Condition!\n";
}

else if(front == rear)


{
front = rear =-1;
}

else
{
cout<<"\nThe deleted value is: "<<arr[front];
front++;
}
for(int i = 0; i<q_size - 1; i++)
{
arr[i] = arr[i+1];
}
rear--;
}
void print() //PRINT VALUE
{
cout<<"\nDisplay the Queue: ";
for(int i=0; i<=rear; i++)
cout<<arr[i]<<" ";
}

};

int _tmain(int argc, _TCHAR* argv[])


{
queue q;

q.addQueue(1);
q.addQueue(6);
q.addQueue(12);
q.addQueue(24);
q.frontVal();
q.rearVal();
q.print();
q.deQueue();
q.deQueue();
q.deQueue();
q.addQueue(16);
q.addQueue(21);
q.print();
cout<<endl;

system("pause");
return 0;
}

Code Task # 02
Create a class Circular Queue that implements the functionality of a queue
providing all the required operations (Enqueue(), Dequeue(), Empty(),
Full() and getFront()).

// circularm.cpp : Defines the entry point for the console application.

// circular.cpp : Defines the entry point for the console application.


//

//

#include "stdafx.h"
#include<iostream>
using namespace std;
int const q_size = 4;
typedef int q_element;
class queue
{
private:
q_element arr[q_size];
q_element front, rear;

public:
queue() //CONSTRUCTOR
{
front = rear = -1;
}

bool empty() //EMPTY FUNCTION


{
if(front == -1 && rear == -1)
return true;
else
return false;
}
bool full() //FULL FUNCTION
{
if((rear+1)%q_size== front)
return true;
else
return false;
}

void frontVal() //RETRIEVE FRONT VALUE


{
cout<<"\nThe value of front is: "<<arr[front];
}

void rearVal() //RETRIEVE REAR VALUE


{
cout<<"\nThe value of rear is: "<<arr[rear];
}

void addQueue(q_element n) //ADD VALUE


{

if(full())
{
cout<<"Overflow Condition!\n";
}
else
{
rear = (rear + 1)% q_size;
arr[rear]= n;
}
}

void deQueue() //DELETE VALUE


{

front = (front + 1)% q_size;


rear--;
}
void print() //PRINT VALUE
{
cout<<"\nDisplay the Queue: ";
for(int i=0; i<=rear; i++)
cout<<arr[i]<<" ";
}

};

int _tmain(int argc, _TCHAR* argv[])


{
queue q;

q.addQueue(20);
q.addQueue(10);
q.addQueue(9);
q.addQueue(6);
q.print();
cout<<endl;
cout<<endl;

cout<<"we have deleted 6 from queue"<<endl;


q.deQueue();
q.print();
cout<<endl;

q.addQueue(8);
cout<<endl;
cout<<"we have added 8 in the place of 6 "<<endl;
q.print();
cout<<endl;

cout<<endl;
cout<<endl;

system("pause");
return 0;}

You might also like