Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make the following string linked list class to add and remove elements from the

ID: 3757015 • Letter: M

Question

Make the following string linked list class to add and remove elements from the front and back of the list. This program already adds to the front. Just need the rest of the parts included.

Please see the program below. Make sure when answering to divide the program into 4 parts just like mine.

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

#include <iostream>

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

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;

}

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

#include"StringLinkedList.h"

int main()

{

StringLinkedList s;

s.addFront("Jill");

s.addFront("Joe");

s.addFront("Rick");

while(s.empty() == false)

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

system("pause");

return 0;

}

Explanation / Answer

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

#include <iostream>

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote