Question & Answer: Please answer in c++ not any other language!…..

Please answer in c++ not any other language!

1 Modify the Linkedlist class Both stacks and queues are able to remove items from their lists. Therefore, this capability needs to be added to the LinkedList class we developed earlier Modify the LinkedList class from the previous assignment by adding two functions. Both of these functions will require us manipulate the relevant pointers correctly a)int deleteFromFront () This function will remove the first data item from the list (i.e, the one immediately after the head node) and return its value. Note that the node is actually removed. The list will thereafter have one fewer elements in it. Dont forget to deallocate (delete) the node after the value has been captured If the list is empty when this function is called, have it return a -1 as an error message b)int deleteFromEnd () This function will remove the last data item from the list (i.e, the one immediately before the tail node) and return its value. Note that the node is actually removed. The list will thereafter have one fewer elements in it. Dont forget to deallocate (delete) the node after the value has been captured. If the list is empty when this function is called, have it return a -1 as an error message 2) Create a Stack class Create a Stack class that inherits publicly from LinkedList and includes the

1 Modify the Linkedlist class Both stacks and queues are able to remove items from their lists. Therefore, this capability needs to be added to the LinkedList class we developed earlier Modify the LinkedList class from the previous assignment by adding two functions. Both of these functions will require us manipulate the relevant pointers correctly a)int deleteFromFront () This function will remove the first data item from the list (i.e, the one immediately after the head node) and return its value. Note that the node is actually removed. The list will thereafter have one fewer elements in it. Don’t forget to deallocate (delete) the node after the value has been captured If the list is empty when this function is called, have it return a -1 as an error message b)int deleteFromEnd () This function will remove the last data item from the list (i.e, the one immediately before the tail node) and return its value. Note that the node is actually removed. The list will thereafter have one fewer elements in it. Don’t forget to deallocate (delete) the node after the value has been captured. If the list is empty when this function is called, have it return a -1 as an error message 2) Create a Stack class Create a Stack class that inherits publicly from LinkedList and includes the

Expert Answer

 

#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

struct node
{
int data;
node *next;
};

class LinkedList{

private:
node *head;
node *end;

public:
LinkedList(){
head = NULL;
end = head;
}
void add(int q){
node *temp;
node *nd;
temp = head;
nd = new node;
nd->next = NULL;
nd->data = q;
if (temp == NULL){
head = nd;
end = nd;
}
else {
end->next = nd;
end = nd;
}
}
void printList(){
node *temp;
int count;
temp = head;
count = 0;
while (temp != NULL){
if (count % 10 == 0)
cout << endl;
cout << temp->data << ” ” ;
temp = temp->next;
count++;
}
cout << endl;
}

int deleteFromFront(){
int a;
node *temp;
if (head == NULL){
cout << “List is empty” << endl;
return -1;
}
else {
temp = head;
a = head->data;
head = head->next;
if (head == NULL)
end = NULL;
delete temp;
return a;
}
}

int deleteFromEnd(){
int a;
node *temp,*ptr;
if (end == NULL){
cout << “List is empty” << endl;
return -1;
}
else {
a = end->data;
temp = head;
if (head->next == NULL){
head = NULL;
end = NULL;
}
else {
while (temp->next != end)
temp = temp->next;
ptr = end;
end = temp;
temp->next = NULL;
delete ptr;
}
return a;
}
}

};

class Stack : public LinkedList {

public:
Stack() : LinkedList(){
}
void push(int q){
add(q);
}
int pop(){
return (deleteFromEnd());
}
int print(){
printList();
}
};

class Queue : public LinkedList{

public:
Queue() : LinkedList() {
}

void enqueue(int q){
add(q);
}
int dequeue(){
return (deleteFromFront());
}
int print(){
printList();
}

};

int main(){

int input;
Stack st;
Queue qt;

srand (time(NULL));

for (int i = 0; i<20; i++){
st.push(rand() % 100 + 1 );
}
cout << “Stack data is as follows:” << endl;
st.print();
cout << “Stack data after pop is as follows:” << endl;
for (int i = 0; i<20; i++){
if (i % 10 == 0)
cout << endl;
cout << st.pop() << ” “;
}
cout << endl;

for (int i = 0; i<20; i++){
qt.enqueue(rand() % 100 + 1 );
}
cout << “Queue data is as follows:” << endl;
qt.print();
cout << “Queue data after dequeue is as follows:” << endl;
for (int i = 0; i<20; i++){
if (i % 10 == 0)
cout << endl;
cout << qt.dequeue() << ” “;
}
cout << endl;

return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!