DS Lab 05 (2020-BSE-051)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Department of Software Engineering

Data Structures & Algorithms Lab


Lab # 5

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;

1. Q.enqueue(10); 10 Front=10, Rear=10


2. Q.enqueue(20); 10,20 Front=10, Rear=20
3. Q.enqueue(30); 10,20,30 Front=10, Rear=30
4. Q.dequeue(); 20,30 Front=20, Rear=30
5. Q.dequeue(); 30 Front=30, Rear=30
6. Q.enqueue(40); 30,40 Front=30, Rear=40
7. Q.dequeue() 40 Front=40, Rear=40
8. Q.dequeue() Stack is empty Front= -1, Rear= -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 elements


front = 2; rear = 0; 7 elements
front = 4; rear = 6; And 1 elements
two elements are
dequeued.
front = 7; rear = 0; 2 elements
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.
q.dequeue(); front → 40
q.dequeue(); 60
q.enqueue(15); rear → 80
q.enqueue(25);
q.enqueue(105);
rear → 25
front→ 80
15

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;
}

You might also like