DS Lab 05 (2020-BSE-051)
DS Lab 05 (2020-BSE-051)
DS Lab 05 (2020-BSE-051)
SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
TASK 01
1. 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;
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.
Code task 01
#include<iostream>
using namespace std;
int const q_size = 10;
typedef int q_element;
class queue
{
private:
q_element arr[q_size];
q_element front, rear;
public:
queue() //CONSTRUCTOR
{
front = rear = -1;
}
bool is_Empty() //EMPTY FUNCTION
{
if (front == -1 && rear == -1)
return true;
else
return false;
}
bool is_Full() //FULL FUNCTION
{
if (front == rear)
return true;
else
return false;
}
void getFront() //RETRIEVE FRONT VALUE
{
cout << "\nThe value of front is: " << arr[front];
}
void getRare() //RETRIEVE REAR VALUE
{
cout << "\nThe value of rear is: " << arr[rear];
}
void EnQueue(q_element n) //ADD VALUE
{
if (rear == q_size - 1)
{
cout << "Overflow Condition!\n";
}
else if (is_Empty())
{
front++;
rear++;
arr[rear] = n;
}
else
{
rear++;
arr[rear] = n;
}
}
void DeQueue() //DELETE VALUE
{
if (is_Empty())
{
cout << "Underflow Condition!\n";
}
else if (front == rear)
{
front = rear = -1;
}
else
{
cout << "\nThe deleted value is: " << arr[front];
front++;
}
}
void display() //PRINT VALUE
{
cout << "\nDisplay the Queue: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
}
};
int main()
{
queue q1;
q1.EnQueue(40);
q1.EnQueue(80);
q1.EnQueue(200);
q1.EnQueue(201);
q1.display();
q1.getFront();
q1.getRare();
q1.DeQueue();
q1.DeQueue();
cout << endl;
q1.display();
cout << endl;
system("pause");
return 0;
Code task 02
#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 getFront() //RETRIEVE FRONT VALUE
{
cout << "\nThe value of front is: " << arr[front];
}
void getRear() //RETRIEVE REAR VALUE
{
cout << "\nThe value of rear is: " << arr[rear];
}
void EnQueue(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 display() //PRINT VALUE
{
cout << "\nDisplay the Queue: ";
for (int i = 0; i <= rear; i++)
cout << arr[i] << " ";
}
};
int main()
{
queue q;
q.EnQueue(60);
q.EnQueue(55);
q.EnQueue(20);
q.EnQueue(11);
q.display();
q.DeQueue();
q.EnQueue(8);
q.display();
cout << endl;
system("pause");
return 0;
}