You are on page 1of 10

Lab Journal 3

Instructions:
1- Late Submission not acceptable
2- Submit only on LMS. Submission through other means is not acceptable
3- Screenshots of program outputs are mandatory. -2 marks for each screenshot missing.
4- Don’t paste whole programs. Paste only functions and outputs that are asked by instructor at
specified locations. -2 marks for pasting whole programs.

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); Front= -1
Rear= 0
Queue: 10

2. Q.enqueue(20); Front : -1
Rear : 1
Content(s) : 10 20
3. Q.enqueue(30); Front : -1
Rear : 2
Content(s) : 10 20 30
4. Q.dequeue(); Front : 0
Rear : 2
Content(s) : 20 30
5. Q.dequeue(); Front : 1
Rear : 2
Content(s) : 30
6. Q.enqueue(40); Front : 1
Rear : 3
Content(s) : 30 40

7. Q.dequeue() Front : 2
Rear : 3
Content(s) : 40
8. Q.dequeue() Front : 3
Rear : 3
Content(s) : empty

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 ; 1 2 3 4 5

front = 2 ; rear = 0 ; 1 2 3 4 5 6

front = 4 ; rear = 6 ; And two 1 2


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(); 40front
q.dequeue();
q.enqueue(15); 60
q.enqueue(25);
80 rear

25
rear
80
DSA LAB ASSIGNMENT 15 HASNAIN AJMAL 01-135201-022
front

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.

Program:
#include<iostream>
#include<string>
#include <fstream>
using namespace std;

typedef char ar;

#define MAX 100

class stack
{
protected:
char array[MAX];
int top;
public:
stack()
{
array[0] = '\0';
top = -1;
}

bool Full()
{
if (top == MAX)
return true;
else
return false;
}
bool empty()
{
if (top == -1)

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


return true;
else
return false;
}
void push(char item)
{
if (Full() != true)
{
top++;
array[top] = item;
}
}
char pop()
{
if (top >= 0)
{
return (array[top--]);
}
else
{
cout << "The stack is empty\n";
return 0;
}
}
void Top()
{
if (top >= 1)
{
cout << array[top];
}
else
{
cout << "Stack k underflow:" << endl;
cout << array[MAX - 1];
}
}
void display()
{
for (int i = 0; i <= top; i++)
{
cout << array[i];
}
}
};

class queue : public stack


{
private:
char array[MAX];
int front, rear;
public:
queue()
{
array[0] = '\0';
front = -1;
rear = -1;
}
bool isfull()

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


{
if (rear == MAX - 1)
return true;
else
return false;
}
bool isempty()
{
if (front == rear)
return true;
else
return false;
}
void enqueue(int value)
{
if (isfull())
cout << "queue is overflow.";
else
array[++rear] = value;

}
int getfront()
{
if (isempty())
{
cout << "queue is empty";
return -1;
}
else
return array[front + 1];
}
char dequeue()
{
if (isempty())
{
cout << "queue is empty";
return 0;
}
else
{
return array[++front];
}
}
void fh()
{
stack obj1;
queue obj2;

bool False = true;

ifstream fin;
fin.open("ex.txt");

if (fin.is_open())
{
string read;
while (fin.good())

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


{
getline(fin, read);
}
cout << "Word From File is :: " << read << endl << endl;

for (int i = 0; i < read.length(); i++)


{
obj1.push(read[i]);
obj2.enqueue(read[i]);
}

while (!obj2.isempty())
{
False = obj2.dequeue() == obj1.pop();
if (False == false)
{
break;

if (False == true)
{
cout << "The Following word is palindrome.";
}

else if (False == false)


{

cout << "The Following word is not a palindrome.";

cout << endl;


}

fin.close();

}
else if(!fin.is_open())
{

cout << "Error ! File either does not exist or cannot opened." <<
endl;

};

int main()
{
queue o;

o.fh();

cout << endl << endl;

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


system(" pause");
return 0;

Output:

Exercise 2:

Implement a program to read a postfix expression from a text file, evaluate the expression
using a Stack and display the result. The text file should contain expressions in the form as
illustrated in the following. (For simplicitiy, assume single digit numbers in the expression.)
23+5*6+

Program:
#include<iostream>

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


#include<conio.h>
using namespace std;
typedef char defItem;
#define Max 10
class CircularQueue
{
private:
int front, rear, count;
char arr[Max];
public:
CircularQueue()
{
front = 0;
rear = 0;
count = 0;
}
bool Full()
{
if (count == Max)
return true;
else
return false;
}
bool Empty()
{
if (count == 0)
return true;
else
return false;
}
void Enqueue(defItem item)
{
if (Full())
{
cout << " Queue Overflow";
cout << "\nQueue is Full" << endl;
}
else if(!Full())
{
arr[rear] = item;
rear = (rear + 1) % Max;
count++;
}
}
defItem Dequeue()
{
defItem item;
if (Empty())
{
cout << " Queue is Empty";
return 0;
}
else
{
item = arr[front];
front = (front + 1) % Max;
count--;
return item;

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


}
}
void getFront()
{
int temp = count;
int temp2 = front;
while (temp != 0)
{
cout << arr[temp2] << " " ;
temp2 = (temp2 + 1) % Max;
temp--;
}
}

};

int main()
{
CircularQueue q;
cout << "Queue Before Entering Anything." << endl;
q.getFront();
q.Enqueue('A');
q.Enqueue('B');
q.Enqueue('C');
cout << "Queue After Entering 3 Characters." << endl;
q.getFront();
q.Dequeue();
cout << "\nQueue After Dequeuing 1 Characters." << endl;
q.getFront();
q.Enqueue('D');
q.Enqueue('O');
q.Dequeue();
q.Dequeue();
q.Enqueue('N');
q.Enqueue('E');
cout << "\nQueue At The End." << endl;
q.getFront();

cout << endl << endl;


system("PAUSE");
return 0;
}

Output:

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022


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

DSA LAB ASSIGNMENT HASNAIN AJMAL 01-135201-022

You might also like