I need help with finishing the core code for \" Singly List \" This is the heade
ID: 3782203 • Letter: I
Question
I need help with finishing the core code for " Singly List "
This is the header file I have :
template <typename E>
class SNode { // singly linked list node
private:
E elem; // linked list element value
SNode<E>* next; // next item in the list
friend class SLinkedList<E>; // provide SLinkedList access
};
template <typename E>
class SLinkedList { // a singly linked list
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() const; // is list empty?
const E& front() const; // return front element
void addFront(const E& e); // add to front of list
void removeFront(); // remove front item list
private:
SNode<E>* head; // head of the list
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL) { }
template <typename E>
bool SLinkedList<E>::empty() const // is list empty?
{
return head == NULL;
}
template <typename E>
const E& SLinkedList<E>::front() const // return front element
{
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList() // destructor
{
while (!empty()) removeFront();
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) { // add to front of list
SNode<E>* v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
}
template <typename E>
void SLinkedList<E>::removeFront() { // remove front item
SNode<E>* old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
}
I need to create a .cpp file based on this header file.
What needs to be created in the main function based on steps 17 & 18 ?
#include <iostream>
#include <string>
#include "SLinkedList.h"
using namespace std;
int main()
{
}
17) Add code to the main function in "slist.cpp" to instantiate a "string" type "SL List". Then inked create entries and add the following strings in order.+ Name BOS ATL MSP LAX 18) Add code to the main function in "slist.cpp" to create an "integer" type "SLinkedList". Then create entries and add the following integers in order Nam 13 12 11 10Explanation / Answer
//I have written function print to display list elements
//add below function declaration to class SLinkedList
void print();
//below is the function declartion of print
template <typename E>
void SLinkedList<E>::print()
{
SNode<E>* tmp = head;
while (tmp)
{
cout << tmp->elem << " ";
tmp = tmp->next;
}
cout << endl;
}
//main function to test linked list
#include <iostream>
#include <string>
#include "SLinkedList.h"
using namespace std;
int main()
{
//create list of string type
SLinkedList<string> strList;
//create list of int type
SLinkedList<int> intList;
//add strings given to list
strList.addFront("BOS");
strList.addFront("ATL");
strList.addFront("MSP");
strList.addFront("LAX");
//add integers to intList
intList.addFront(13);
intList.addFront(12);
intList.addFront(11);
intList.addFront(10);
intList.addFront(9);
//print string list
cout << "String list has elements: ";
strList.print();
//print intList
cout << "Integer list has elements: ";
intList.print();
}
--------------------------------------------------------------------
//output
String list has elements: LAX MSP ATL BOS
Integer list has elements: 9 10 11 12 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.