Data Structures PLEASE USING THIS C++ PROGRAM BELOW, I NEED HELP ON IMPLEMENTING
ID: 3936902 • Letter: D
Question
Data Structures
PLEASE USING THIS C++ PROGRAM BELOW, I NEED HELP ON IMPLEMENTING BOTH QUEUE AND STACK TO PRINT "I able" REVERSE/BACKWARDS(PALINDROME)
#include <iostream>
#include <stdlib.h>
using namespace std;
struct st
{
int year;
st *next;
};
class queue
{
public:
int sz,i = 0;
char chr[100];
queue()
{
//constructor for initializing front and rear to NULL
front = NULL;
rear = NULL;
cout << "enter size of queue: ";
cin >> sz;
}
// ~queue()
// {
//
// }
// in the enterqueue, create a new node, assign value and add it to queue
// if F = R = NULL the ( ) else ( )
void enterqueue(int yr, char el)
{
//inserting into queue if queue not overflow
if(i >= sz)
{
cout << "queue over flow";
return;
}
if (front == NULL )
{
front = new st;
front->next = NULL;
front->year = yr;
rear = front;
chr[i] = el;
i++;
}
else
{
st *temp = new st;
temp->year = yr;
rear->next = temp;
rear = temp;
chr[i] = el;
i++;
}
}
char deletequeue()
{
//delete front data from queue if data in the queue
char tr;
int j = 0;
st *temp;
temp = front;
if( front == NULL)
{
cout << "queue under flow";
return 'a';
}
if(front == rear)
{
front = rear = NULL;
return 'a';
}
front = front->next;
delete temp;
tr = chr[0];
i--;
for(j = 0; j < i; j++)
{
chr[j] = chr[j+1];
}
chr[j] = '';
return tr;
}
bool isempty()
{
//it shows whether queue is empty
if( front == NULL)
{
return true;
}
else
return false;
}
bool isfull()
{
//it shows whether queue is full
if(i == sz)
return true;
else
return false;
}
void display()
{
//it displays queue data with characters of queue if invalid choice is entered
st *tmp;
int j = 0;
cout << " queue is: character year";
for(tmp = front; tmp != rear; tmp = tmp->next, j++)
{
cout << " " << chr[j] << " " << tmp->year;
}
cout << " "<<chr[j] << " " << tmp->year;
}
private:
st *front, *rear;
};
int main()
{
//main method to call queue functions by choice
queue s1;
int ch,yr;
char el;
bool s;
while(1)
{
cout << " 1.ENTERQUEUE 2.DELETEQUEUE 3.ISEMPTY 4.ISFULL 5.EXIT enter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
if(!s1.isfull())
{
cout << " enter a year : ";
cin >> yr;
cout << " enter a character : ";
cin >> el;
s1.enterqueue(yr, el);
}
else
cout << "insertion not possible";
break;
case 2:
s = s1.isempty();
if(!s)
{
el = s1.deletequeue();
cout << " The character of queue " << el <<" is deleted" << endl;
}
else
cout << " queue is empty.deletion not possible"<< endl;
break;
case 3:
s = s1.isempty();
if(s)
cout << " queue is empty" << endl;
else
cout << " queue is not empty" << endl;
break;
case 4:
s=s1.isfull();
if(s)
cout << " queue is full" << endl;
else
cout << " queue is not full" << endl;
break;
case 5:
exit(0);
default : cout << "invalid choice" << endl;
if(!s1.isempty())
s1.display();
}
}
return (0);
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
struct st
{
int year;
st *next;
};
class queue
{
public:
//int sz,i=0;//modified //code modified here// everything is fine with the code except this... queue is implemented correctly
//you can use this code for ur program;
int sz,i;
char chr[100];
queue()
{
//constructor for initializing front and rear to NULL
front = NULL;
rear = NULL;
cout << "enter size of queue: ";
cin >> sz;
}
// ~queue()
// {
//
// }
// in the enterqueue, create a new node, assign value and add it to queue
// if F = R = NULL the ( ) else ( )
void enterqueue(int yr, char el)
{
//inserting into queue if queue not overflow
if(i >= sz)
{
cout << "queue over flow";
return;
}
if (front == NULL )
{
front = new st;
front->next = NULL;
front->year = yr;
rear = front;
chr[i] = el;
i++;
}
else
{
st *temp = new st;
temp->year = yr;
rear->next = temp;
rear = temp;
chr[i] = el;
i++;
}
}
char deletequeue()
{
//delete front data from queue if data in the queue
char tr;
int j = 0;
st *temp;
temp = front;
if( front == NULL)
{
cout << "queue under flow";
return 'a';
}
if(front == rear)
{
front = rear = NULL;
return 'a';
}
front = front->next;
delete temp;
tr = chr[0];
i--;
for(j = 0; j < i; j++)
{
chr[j] = chr[j+1];
}
chr[j] = '';
return tr;
}
bool isempty()
{
//it shows whether queue is empty
if( front == NULL)
{
return true;
}
else
return false;
}
bool isfull()
{
//it shows whether queue is full
if(i == sz)
return true;
else
return false;
}
void display()
{
//it displays queue data with characters of queue if invalid choice is entered
st *tmp;
int j = 0;
cout << " queue is: character year";
for(tmp = front; tmp != rear; tmp = tmp->next, j++)
{
cout << " " << chr[j] << " " << tmp->year;
}
cout << " "<<chr[j] << " " << tmp->year;
}
private:
st *front, *rear;
};
int main()
{
//main method to call queue functions by choice
queue s1;
int ch,yr;
char el;
bool s;
while(1)
{
cout << " 1.ENTERQUEUE 2.DELETEQUEUE 3.ISEMPTY 4.ISFULL 5.Display queue 6.EXIT enter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
if(!s1.isfull())
{
cout << " enter a year : ";
cin >> yr;
cout << " enter a character : ";
cin >> el;
s1.enterqueue(yr, el);
}
else
cout << "insertion not possible";
break;
case 2:
s = s1.isempty();
if(!s)
{
el = s1.deletequeue();
cout << " The character of queue " << el <<" is deleted" << endl;
}
else
cout << " queue is empty.deletion not possible"<< endl;
break;
case 3:
s = s1.isempty();
if(s)
cout << " queue is empty" << endl;
else
cout << " queue is not empty" << endl;
break;
case 4:
s=s1.isfull();
if(s)
cout << " queue is full" << endl;
else
cout << " queue is not full" << endl;
break;
case 5:
if(!s1.isempty())
s1.display();
break;
case 6: exit(0);
default : cout << "invalid choice" << endl;
}
}
return (0);
}
output:-
enter size of queue: 3
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 1
enter a year : 1994
enter a character : a
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 1
enter a year : 1995
enter a character : b
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 1
enter a year : 1996
enter a character : c
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 1
insertion not possible
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 3
queue is not empty
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 4
queue is full
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 5
queue is:
character year
a 1994
b 1995
c 1996
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 2
The character of queue a is deleted
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice: 5
queue is:
character year
b 1995
c 1996
1.ENTERQUEUE
2.DELETEQUEUE
3.ISEMPTY
4.ISFULL
5.Display queue
6.EXIT
enter your choice:6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.