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

Data Structures and Algorithms

Lab Journal - Lab 4


Name: Maaz Nafees

Enrollment #: 01-235171-074

Class/Section: BSIT (3B)

Objective

This lab is intended to introduce students to Queues and their applications. The students will implement the
Queue and employ it in solving the given problems.

Task 1 :

Give answers to the following.

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=-1 , rear=0
2. Q.enqueue(20); | 10 | 20 |
front=-1 , rear=1
3. Q.enqueue(30); | 10 | 20 | 30 |
front=-1 , rear=2
4. Q.dequeue(); | 20 | 30 |
front=0 , rear=2
5. Q.dequeue(); | 30 |
front=1 , rear=2
6. Q.enqueue(40); | 30 | 40 |

front=1 , rear=3
7. Q.dequeue() | 40 |
front=2 , rear=3

Data Structures and Algorithms Page 1


8. Q.dequeue() “EMPTY”
front=3 , rear=3

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

front = 2 ; rear = 0 ; 6

front = 4 ; rear = 6 ; And two 0


elements are dequeued.

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); 80
q.enqueue(25); rear

25
rear→
front 80 →
15

Data Structures and Algorithms Page 2


Task 2 :

Implement the following exercises.

Exercise 1

Write a program that reads a string from a text file and determines if the string is a palindrome
or not. Use a Stack and a Queue to check for the palindrome.
CODE :

#include<stack>
#include"queue.h"

void main()
{
Queue q;
stack<char> s;
char ss, qq;
string str;
cout << "Enter Word to check it's Palindrome or not:";
cin>>str;

for (unsigned i = 0; i <str.length() ;i++)


{
s.push(str[i]);
q.enqueue(str[i]);
}
bool paal = true;
while (!q.isempty())
{
q.dequeue(qq);
ss=s.top();
s.pop();
paal = qq == ss;
if (paal == false)
break;
}
if (paal)
cout << "Palindrome!" << endl;
else
cout << "Not Palindrome!" << endl;

system("pause");
}

Data Structures and Algorithms Page 3


Exercise 2

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

queue.h

#define queuesize 100


typedef int queuetype;
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class Queue
{
public:
Queue();
void enqueue(queuetype item);
void dequeue(queuetype& item);
bool isempty();
bool isfull();
void display();
int getfront();
int getrear();
private:
int front, rear, count;
queuetype queueArray[queuesize];

};

queue.cpp
#include"queue.h"
Queue::Queue()
{
front = rear = count=0;
}
bool Queue::isempty()
{
if (count==0)
{
cout << "Queue is Empty" << endl;
return true;
}
else
return false;
}
bool Queue::isfull()
{
if (count==queuesize)
{
cout << "Queue is full" << endl;

Data Structures and Algorithms Page 4


return true;
}
else
{
return false;

}
}
void Queue::enqueue(queuetype n)
{
if (!isfull())
{

queueArray[rear] = n;
rear = (rear + 1) % queuesize;
count++;
}
}
void Queue::dequeue(queuetype& n)
{
if (!isempty())
{
n = queueArray[front];
front = (front + 1) % queuesize;
count--;
}
}
int Queue::getfront()
{
return front + 1;
}
int Queue::getrear()
{
return rear;
}
void Queue::display()
{
for (int i = front; i < rear; i++)
{
cout << queueArray[i] << endl;

}
}

Implement the given exercises and get them checked by your instructor. If you are unable to complete the
tasks in the lab session, deposit this journal alongwith your programs (printed or handwritten) before the
start of the next lab session.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

+++++++++++++++++++++++++
Data Structures and Algorithms Page 5

You might also like