< Back

Lecture 29 - Test Review


As usual, create a directory to hold today's activities:

  $ mkdir ~/cs170/labs/lab29 
  $ cd ~/cs170/labs/lab29

  1. Write a function called bool isPalindrome(char * a_string), which takes a single string as a parameter. Your function should return True if the parameter string is a palindrome.

    A string is a palindrome if its first and last letters are the same, and the string without the first and last letters is a palindrome. Notice that a string of length \(\leq 1\) is always a palindrome.

    YOU MAY NEED TO WRITE HELPER FUNCTIONS FOR THIS.

    palindrome.cc
    #include <iostream>
    using namespace std;
    
    void printBool(bool val) {
      if (val) {
        cout << "true" << endl;
      } else {
        cout << "false" << endl;
      }
    }
    /* Your code here */
    int main() {
      printBool(isPalindrome("bob"));      //true
      printBool(isPalindrome("racecar"));  //true
      printBool(isPalindrome("racecars")); //false
    }
    
    
  2. Download the files LinkedList.h and LinkedList.cc into your directory. Create a new method of the Linked List class called void doubleList(). This method takes no parameters. It should create a duplicate of every node in the linked list. Partial credit will be awarded for an \(O(n^2)\) algorithm, full credit for an \(O(n)\) algorithm.

      LinkedList ls;
      /* some code which puts elements into the list */
    
      cout << ls << endl; //[1, 2, 3, 4]
      ls.doubleList();
      cout << ls << endl; //[1, 2, 3, 4, 1, 2, 3, 4]