Question

In: Computer Science

Write a recursive function to check if a string whose each character is stored in a...

Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github.

//
// Doubly-linked list with 2 dummy nodes
//
#pragma once
#include <stdexcept>
template<typename T>
struct Node {
T data;
Node<T>* next;
Node<T>* prev;
Node() = delete; // Intentionally no default constructor
Node( const T & element ) : data( element ), next( nullptr ), prev( nullptr ) {}
};
template<typename T>
class DoublyLinkedList {
private:
Node<T>* head;
Node<T>* tail;
public:
// Constructors
DoublyLinkedList();
DoublyLinkedList(const DoublyLinkedList&);
DoublyLinkedList& operator=(const DoublyLinkedList&); // assignment operator
~DoublyLinkedList(); // destructor
// Getters / Setters
bool empty();
int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !!
void append( const T& );
void prepend( const T& );
void insertAfter( Node<T>*, const T& );
void remove( Node<T>* );
void pop_front(); // remove element at front of list
void pop_back(); // remove element at back of list
T& front(); // return list's front element
T& back(); // return list's back element
void clear();
};
template<typename T>
DoublyLinkedList<T>::DoublyLinkedList() {
head = new Node<T>( T() ); // create dummy nodes
tail = new Node<T>( T() );
head->next = tail; // have them point to each other
tail->prev = head;
}
template<typename T>
bool DoublyLinkedList<T>::empty() {
return head->next == tail;
}
template<typename T>
void DoublyLinkedList<T>::append( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
newNode->prev = tail->prev;
newNode->next = tail;
tail->prev->next = newNode;
tail->prev = newNode;
}
template<typename T>
void DoublyLinkedList<T>::prepend( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
Node<T> * firstNode = head->next;
newNode->next = head->next;
newNode->prev = head;
head->next = newNode;
firstNode->prev = newNode;
}
template<typename T>
void DoublyLinkedList<T>::insertAfter(Node<T>* curNode, const T& newData) {
if (curNode == tail) {
// Can't insert after dummy tail
return;
}
// Construct new node
Node<T>* newNode = new Node<T>( newData );
Node<T>* sucNode = curNode->next;
newNode->next = sucNode;
newNode->prev = curNode;
curNode->next = newNode;
sucNode->prev = newNode;
}
template<typename T>
void DoublyLinkedList<T>::remove(Node<T>* curNode) {
if( empty() ) throw std::length_error( "empty list" );
if (curNode == head || curNode == tail) {
// Dummy nodes cannot be removed
return;
}
Node<T>* sucNode = curNode->next;
Node<T>* predNode = curNode->prev;
// Successor node is never null
sucNode->prev = predNode;
// Predecessor node is never null
predNode->next = sucNode;
}
template <typename T>
void DoublyLinkedList<T>::pop_front() {
remove(head->next);
}
template <typename T>
void DoublyLinkedList<T>::pop_back() {
remove(tail->prev);
}
template <typename T>
T& DoublyLinkedList<T>::front() {
if( empty() ) throw std::length_error( "empty list" );
return head->next->data;
}
template <typename T>
T& DoublyLinkedList<T>::back() {
if( empty() ) throw std::length_error( "empty list" );
return tail->prev->data;
}
template<typename T>
void DoublyLinkedList<T>::clear() {
while( !empty() )
pop_front();
}
template<typename T>
DoublyLinkedList<T>::~DoublyLinkedList() {
clear();
delete head; // remove the dummy nodes
delete tail;
}
template <typename T>
DoublyLinkedList<T>::DoublyLinkedList( const DoublyLinkedList<T> & original ) : DoublyLinkedList() {
// Walk the original list adding copies of the elements to this list maintaining order
for( Node<T> * position = original.head->next; position != original.tail; position = position->next ) {
append( position->data );
}
}
template <typename T>
DoublyLinkedList<T> & DoublyLinkedList<T>::operator=( const DoublyLinkedList<T> & rhs ) {
if( this != &rhs ) // avoid self assignment
{
// Release the contents of this list first
clear(); // An optimization might be possible by reusing already allocated nodes
// Walk the right hand side list adding copies of the elements to this list maintaining order
for( Node<T> * position = rhs.head->next; position != rhs.tail; position = position->next ) {
append( position->data );
}
}
return *this;

}

Think like, you are adding function/s to this class code. Also write a helper function.

A string is a palindrome if its reverse is the same as the original string. For e.g. “civic” is a palindrome. The function should return bool true or false.



b) Why do we have to write two functions for the above recursive implementation ? How many functions do we have to write for the singly linked list we created in Simple Singly Linked List Example.cpp code posted on Titanium ?


Solutions

Expert Solution

Function to check if a doubly linkedList is Palindrome or not

bool Palindrome(struct Node *left)

{

    if (left == NULL)

       return true;

  

    // Find rightmost node

    struct Node *right = left;

    while (right->next != NULL)

        right = right->next;

  

    while (left != right)

    {

        if (left->data != right->data)

            return false;

  

        left = left->next; // next is pointing to the next node

        right = right->prev; //prev is pointing to the previous node

    }

  

    return true;

}

2. A Double linked list usually has two links one for the next node, one for the previous node as compared to a singly linked list which has only one link to the next node, hence two functions have been written for double linked list


Related Solutions

Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT