Professional Documents
Culture Documents
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 :
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.
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 :
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;
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)
}
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;
ifstream fin;
fin.open("ex.txt");
if (fin.is_open())
{
string read;
while (fin.good())
while (!obj2.isempty())
{
False = obj2.dequeue() == obj1.pop();
if (False == false)
{
break;
if (False == true)
{
cout << "The Following word is palindrome.";
}
fin.close();
}
else if(!fin.is_open())
{
cout << "Error ! File either does not exist or cannot opened." <<
endl;
};
int main()
{
queue o;
o.fh();
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>
};
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();
Output:
2. Exercise 2