Lab Journal#03
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()){
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();