Test cases are provided only to help you test your code. Each

computer science

Description

Driver.cpp
#include <iostream>
#include <string>
using namespace std;

// 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.

  */

/*
Test 1
*/
cout << "\n---------------------\n";
  int n = 5;
  char testStringA[] = {'m','a','d','a','m'};
  SLL sA;
  for(int i = 0; i<n; i++){
    sA.insert(nullptr,testStringA[i]);
  }
  cout << "Test A:" << endl;
  sA.displayList();

  cout << "expected >> Palindrome " << endl;

  if(sA.palindrome())
    cout << "result >> Palindrome" << endl;
  else
    cout << "result >> Not a palindrome" << endl;
   cout << "\n---------------------\n";

  /*
Test 2
*/
cout << "\n---------------------\n";
  char testStringB[] = {'m','a','d','m','a'};
  SLL sB;
  for(int i = 0; i<n; i++){
    sB.insert(nullptr,testStringB[i]);
  }
  cout << "Test B: \n";
  sB.displayList();
  cout << "expected >> Not a palindrome " << endl;
  if(sB.palindrome())
    cout << "result >> Palindrome" << endl;
  else
    cout << "result >> Not a palindrome" << endl;
cout << "\n---------------------\n";


  return 0;
}

SLL.cpp
#include <iostream> // predefined header file (defined for you) using namespace std; #include "SLL.hpp" // your own header file // <> vs "" -- compiler looks for the file in the C++ implementation // vs in the current directory SLL::SLL(){ // constructor definition head = nullptr; } SLL::~SLL(){ Node* crawler; while(head!=nullptr){ crawler = head->next; delete head; head = crawler; } } void SLL::displayList(){ Node* crawler = head; while( crawler != nullptr ){ cout << crawler->key << "->"; crawler = crawler->next; } cout << "END" << endl; } void SLL::insert(Node* afterMe, char newValue){ if(head == nullptr){ // first case: empty list // add new node and make head point to it head = new Node; head->key = newValue; head->next = nullptr; // what if we forget this line? } else if(afterMe == nullptr){ // This condition implies that the list is not empty, but // the caller wants to add node before the head node Node* newNode = new Node; newNode->key = newValue; newNode->next = head; head = newNode; // at this point, we can test our code for this use case } else{ Node* newNode = new Node; newNode->key = newValue; newNode->next = afterMe->next; afterMe->next = newNode; } } bool SLL::palindrome(){ /* * TODO your function definition goes here */ return true; } char SLL::atIndex(int i){ return '\0'; } SLL.hpp
// SLL.hpp - interface file (header file)
#ifndef SLL_H
#define SLL_H

struct Node{
  char key;
  Node *next;
};

class SLL{
private:
  Node* head;

public:
  SLL(); // constructor declaration

  ~SLL(); // destructor declaration

  void displayList();
  // Precondition: the head node is defined.

  // Post condition: display the key values of the entire list, starting with
  // first node and ending with last node.

  void insert(Node* afterMe, char newValue);
  // Precondition: afterMe is a valid pointer to a node in the linked list.
  // newValue is a valid string.

  // Postcondition: a new node is created and newValue is stored as its key.
  // The new node is added after node containing afterMe.

  bool palindrome();
  // Precondition: the head node is defined
  
  // Postcondition: if the list constitutes a palindrome, the function returns
  // a boolean true. Otherwise, it returns a false.

  char atIndex(int i);
  // Precondition: input parameter is a positive integer

  // Postcondition: return a character corresponding to the key value of the
  // node at the i-th numbered node in the list. The head node is the 0-index node,
  // the following node is the 1-index node, and so on.
};





#endif

Instruction Files
2.png
109.0 KB

Related Questions in computer science category