Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a queue class by inheriting the string linked list class Queue Header File Below #ifndef QUEUE_H #define QUEUE_H #includeSLinkedList.cpp template class Queue : public

Implement a queue class by inheriting the string linked list class

Queue Header File Below

#ifndef QUEUE_H #define QUEUE_H #include"SLinkedList.cpp" template  class Queue : public SLinkedList { public: Queue() : SLinkedList(){} void enqueue(const E& value ); E dequeue(); E front(); }; #endif 

String Linked linked Class Below

-------------------StringNode.h-------------------

#include

using namespace std;

class StringNode

{

private:

string elem;

StringNode* next;

friend class StringLinkedList;

};

------------------StringLinkedList.h-------------------

#include "StringNode.h"

class StringLinkedList { // a linked list of strings

public:

StringLinkedList(); // empty list constructor

~StringLinkedList(); // destructor

bool empty() const;

const string& front() const; // get front element

void addFront(const string& e); // add to front of list

string removeFront(); // remove front item list

void addBack(const string& e); // add to back of list

string removeBack(); // remove back item list

private:

StringNode* head; // pointer to the head of list

};

-------------------StringLinkedList.cpp-------------------

#include "StringLinkedList.h"

StringLinkedList::StringLinkedList() // constructor

: head(NULL) { }

StringLinkedList::~StringLinkedList() // destructor

{ while (!empty()) removeFront(); }

bool StringLinkedList::empty() const

{ return head == NULL; }

const string& StringLinkedList::front() const // get front element

{ return head->elem; }

void StringLinkedList::addFront(const string& e) { // add to front of list

StringNode* v = new StringNode; // create new node

v->elem = e; // store data

v->next = head; // head now follows v

head = v; // v is now the head

}

string StringLinkedList::removeFront() { // remove front item

StringNode* old = head; // save current head

string ret = head->elem;

head = old->next; // skip over old head

delete old; // delete the old head

return ret;

system("pause");

return 0;

}

void StringLinkedList::addBack(const string& e) { // add to back of list

StringNode* v = new StringNode; // create new node

v->elem = e; // store data

v->next = NULL; // V is now the last node

if(head == NULL)

{

head = v;

return;

}

StringNode* crawl = head;//this moves through the whole list

while(crawl->next!=NULL)//moving thhrough the list to reach the end

{

crawl = crawl->next;

}

crawl->next = v;//assigning the new node to the next of current last node

}

string StringLinkedList::removeBack() { // remove last item

StringNode* crawl = head; // this points to the node to be deleted

StringNode* lazycrawl = head;// this points to the previous node of the node to be deleted

while(crawl->next!=NULL)//moving thorugh the list

{

lazycrawl = crawl;

crawl = crawl->next;

}

string ret = crawl->elem;//element to be returned

if(crawl == head)

{

head = NULL;//if list had only one element

}

else

{

lazycrawl->next = NULL;//else disconnect the last node

}

delete crawl;//free the memory allocation

return ret;

system("pause");

return 0;

}

-------------------main.cpp-------------------

#include"StringLinkedList.h"

int main()

{

StringLinkedList s;

s.addFront("Jill");

s.addFront("Joe");

s.addFront("Rick");

s.addBack("Tom");

s.addBack("Mike");

while(s.empty() == false)

cout << s.removeFront() << endl;

s.addBack("Tom");

s.addBack("Mike");

s.addFront("Jill");

s.addFront("Joe");

s.addFront("Rick");

while(s.empty() == false)

cout << s.removeBack() << endl;

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago