// Code from: // Data Structures and Algorithms in C++, Goodrich, Tamassia, and
ID: 3730520 • Letter: #
Question
// Code from:
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.
//
#pragma once
#include <stdexcept>
using namespace std;
template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
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?
E& front(); // return front element
void addFront(const E& e); // add to front of list
void removeFront(); // remove front item list
int size() const; // list size
private:
SNode<E>* head; // head of the list
int n; // number of items
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(0) { }
template <typename E>
bool SLinkedList<E>::empty() const // is list empty?
{
return head == NULL; // can also use return (n == 0);
}
template <typename E>
E& SLinkedList<E>::front() // return front element
{
if (empty()) throw length_error("empty list");
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
n++;
}
template <typename E>
void SLinkedList<E>::removeFront() { // remove front item
if (empty()) throw length_error("empty list");
SNode<E>* old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
n--;
}
template <typename E>
int SLinkedList<E>::size() const { // list size
return n;
}
template<typename E>
bool SLinkedList<E>::identicalEnds()
{
// your code goes here
}
IV. (15 points) implementing data structure operations Write C++ code for a new public member function to be added to the singly linked list data structure used in class. Write C++ code for a new public member function to be added to the singly linked list data structure. The function should return true if the front and back element are the same. You may assume that the element type E can be compared with the == operator. The function returns false if the list is empty. The function declaration should be: bool identicalEnds (); You may use loops or recursion as needed. If necessary, you may add private helper function(s). La definition of the SLinkedList class and its related SNode class are shown below along with the space to write your function.Explanation / Answer
template<typename E>
bool SLinkedList<E>::identicalEnds()
{
// your code goes here
if(head == NULL) //return false, because the list is empty
return false;
SNode<E> *back = head; // Add a temporary back pointer which points to head. we will now traverse to end
/* Traverse to the end of the list. The last element will have next=NULL. So continue until back->next!=NULL */
while(back->next)
back = back->next; //Move back to next one in the list
/* Now as we have the last node of list in back, we can compare head and back. Then return as required. */
if(head == back) /* If head/front is same as back then return true, else return false. */
return true;
else
return false;
}
Note: You have not provided any main function to test the result. So I am providing only the code for the required function. Please provide me the main function, so that I can execute and provide you the output. Thanks. Ping me back for any doubts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.