0% found this document useful (0 votes)
18 views6 pages

Lab Journal#03

1. This document contains a lab journal with two tasks: showing the contents of a queue after various enqueue and dequeue operations, and writing a program to check if a string read from a file is a palindrome using a stack and queue. 2. The program opens a text file, copies the string to a queue and stack, then pops from the stack and dequeues from the queue comparing the characters - if they all match it is a palindrome. 3. The output displays whether the string "level" from the file is a palindrome or not.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Lab Journal#03

1. This document contains a lab journal with two tasks: showing the contents of a queue after various enqueue and dequeue operations, and writing a program to check if a string read from a file is a palindrome using a stack and queue. 2. The program opens a text file, copies the string to a queue and stack, then pops from the stack and dequeues from the queue comparing the characters - if they all match it is a palindrome. 3. The output displays whether the string "level" from the file is a palindrome or not.

Uploaded by

Nazia butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab journal#03

Name:

Enrollment #:

Class/Section:

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

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.

4
front = 0 ; rear = 4 ;
6
front = 2 ; rear = 0 ;
Empty
front = 4 ; rear = 6 ; And two
elements are dequeued.

Task 2:
Write a program that reads a string from a text file and determines if the string is a
palindromeor not. Use a Stack and a Queue to check for the palindrome.
CODE:
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class stack
{
private:
int top;
char sarr[20];
public:
stack()
{
top = -1;
}
bool isempty()
{
if (top == -1)
{
return true;

}
else
return false;
}
bool isfull()
{
if (top == 20 - 1)
{
return true;
}
else
return false;
}
void push(char c)
{
if (isfull())
{
cout << "Stack overflow\n";
exit(1);
}
else
{
top++;
sarr[top] = c;
}
}
char pop()
{
char a;
if (isempty()){

cout << "Stack underflow\n";


return 0;
}
else{
a = sarr[top];
sarr[top] = 0;
top--;
}
return a;
}
void display()
{
cout << "All values in stack are :";
for (int i = 0; i < 20; i++)
{
cout << sarr[i];
}
}
}; class queue
{
private:
int front, rear;
char qarr[20];
int count;
public:
queue()
{ front = rear = -1;
for (int i = 0; i < 20; i++){
qarr[i] = 0;
}
}
bool isempty()
{
if (front == -1 && rear == -1)
{
return true;
}
else
return false;
}
bool(isfull())
{

if ((rear + 1) % 20 == front)
return true;
else return false;
}
void enqueue(char c)
{
if (isfull())

{
cout << "Queue is full\n";
}
else {
rear++;
qarr[rear] = c;
}
}
char deque()
{
char c;
if (isempty())
{
cout << "Queue is empty" << endl;
}
else
{
front++;
c = qarr[front];
qarr[front] = 0;
return c;
}
}
void display()
{
cout << endl;
cout << "All values in queue are :";
for (int i = 0; i < 20; i++){
cout << qarr[i];
}
}
}; int main()
{
stack s;
queue q;
string str;
str = "level";
int len, count = 0;
char a, b;
ofstream file;
file.open("example.txt");
file << str;
file.close();
ifstream myfile;
myfile.open("example.txt");
while (myfile.good())
{
getline(myfile, str);

}
myfile.close();
len = str.length();

for (int i = 0; i < len; i++)


{
q.enqueue(str[i]);
s.push(str[i]);
}
for (int i = 0; i < len; i++)
{
a = s.pop();
b = q.deque(); if (a == b)
{
count++;
}
}
if (count == len)
{
cout << "The string in text file is: "<<str<<endl << " This string is palindrome " << endl;
}
else {
cout << "The string in text file is: " << str << endl << "This string is not palindrome " << endl;;
}
_getch();
return 0;
}
OUTPUT:

You might also like