Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I need help with this code. You only have to do the TODO part) Q2driver.cpp: SLL.cpp: SLL.hpp: 2. 30 points You are given a singly

(I need help with this code. You only have to do the TODO part)

image text in transcribed

image text in transcribed

Q2driver.cpp:

image text in transcribed

image text in transcribed

SLL.cpp:

image text in transcribed

image text in transcribed

SLL.hpp:

image text in transcribed

2. 30 points You are given a singly linked list class SLL (partial) implementation where each node consists of a single key value of char type. You are required to implement two member functions char at Index(int i) and bool palindrome (). 1. The function char at Index(int i) returns the character at the index i of the linked list. Note that we, the computer scientists, count from 0. Hence, the head of the list is the 0 index node, the next element is the 1 index node, and so on. Your function should terminate gracefully by printing an error message and returning the null character '\0' ifi is not a valid index (e.g. i is either negative or greater than or equal to the length of the linked list). You can safely assume that the null character '\0' is not a member of the linked list. 2. The function bool palindrome () returns true if the sequence of characters of the linked list is a palindrome Recall that a palindrome is a sequence of characters which reads the same backward as forward, such as tacocat or racecar. For the purpose of this question, we assume that empty lists are palindromes, and so are lists with only one node. Hint: The atIndex function. Example A. Let 11 be an object of the SLL class. If 11 has the following config- uration: d m null head m Then, 11.palindrome () should return true, while 11.atIndex (0) and 11.at Index(4) should both return m. On the other hand, 11.atIndex(-1) and 11.at Index(8) should return the null character ('\0') and print an error message. Similarly, if 11 has the following configuration: a a null head then 11.palindrome () should return false. Within your function, you are allowed to construct a local linked list if it helps you to solve the problem. However, you must deallocate (free) any heap memory that is no longer required. You are not allowed to use an array, a vector, or any of the string library functions. Hinclude Hinclude using namespace std; 1 2 3 4 5 6 7 8 9 10 11 12 // any file that uses the SLL class must include this header file #include "SLL.hpp int main() { Test cases are provided only to help you test your code. Each submission will be individually inspected by the teaching staff. 13 */ 14 15 16 17 18 Test 1 19 cout > Palindrome" > Palindrome" > Not a palindrome" > Not a palindrome > Palindrome" > Not a palindrome" Il predefined header file (defined for you) 2 using namespace std; 3 4 #include "SLL.hpp" // your own header file 5 11 vs "" -- compiler looks for the file in the C++ implementation 6 II vs in the current directory 7 8 9 SLL SLL(){ // constructor definition 10 head - nullptr; 11 12 13 SLL: SLL(){ 14 Node* crawler; 15 16 while(headlenullptr){ 17 crawler - head->next; 18 delete head; 19 head - crawler; 20 21 } 22 23 24 void SLL::displaylist() { 25 Node* crawler - head; 26 while( crmuler le nullptr ) { 27 cout key "->"; 28 crawler - crawler->next; 29 } 30 31 cout key - newWalue; 42 head->next- nullptr; // what if we forget this line? 43 } 44 else if(aftertle - nullptr){ 45 // This condition implies that the list is not empty, but 46 // the caller wants to add node before the head node 47 Node nendlode - new Node; 48 neudlode->key - newalue; 49 nendlode->next - head; 50 head - nendlode; 51 11 at this point, we can test our code for this use case 52 53 } 54 elsef 55 Node nendlode - new Node; 56 nerdlode-slay - nandalue; 57 neslode->next-afterthe->next; 58 after Me->next - nendlode; 59 60 61 62 63 64 bool SLL::palindrome() { 65 66 * TODO your function definition goes here 67 68 return true; 69 } 70 71 char SLL::at Index(int 1){ 72 73 return '\0'; 74 } 75 WN 1 V SLL.hpp - interface file (header file) 2 Hfndef SL H 3 #define SLL_H 4 5 struct Mode{ 6 char key; 7 Node *next; 8 }; 9 10 class SLLE 11 private: 12 Node* head; 13 14 public: 15 SLL(); // constructor declaration 16 17 SLL(); 11 destructor declaration 18 19 void displayList(); 20 Il Precondition: the head node is defined. 21 22 // Post condition: display the key values of the entire list, starting with Il first node and ending with last node. 24 25 void Insert(Node* efterte, char nealue); 26 // Precondition: afterMe is a valid pointer to a node in the linked list. 27 // newValue is a valid string. 28 29 Il Postcondition: a new node is created and newValue is stored as its key. 30 Il The new node is added after node containing afterMe. 23 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 bool palindrome(); Il Precondition: the head node is defined // Postcondition: if the list constitutes a palindrome, the function returns Il a boolean true. Otherwise, it returns a false. char at Index(int 1); Il Precondition: input parameter is a positive integer Il Postcondition: return a character corresponding to the key value of the Il node at the 1-th numbered node in the list. The head node is the -index node, // the following node is the 1-index node, and so on. }

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

Students also viewed these Databases questions

Question

a. When did your ancestors come to the United States?

Answered: 1 week ago

Question

d. What language(s) did they speak?

Answered: 1 week ago

Question

e. What difficulties did they encounter?

Answered: 1 week ago