Question

In: Computer Science

C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked...

C++ Question

Write a code for :-

public List Palindrome();

//Check whether a given singly linked list is palindrome or not.

Input:

a -> b-> NULL

a -> b -> a-> NULL

s -> a -> g -> a -> r-> NULL r -> a -> d -> a -> r-> NULL

Output:

not palindrome palindrome

not palindrome palindrome

Note:- Code should work on MS-Visual Studio 2017 and provide outputs with code

Solutions

Expert Solution

C++ Code:

#include<bits/stdc++.h> 
using namespace std;
// Class Node accepts a char value
class Node {
    public:
    char data;
    Node(char value){
        data = value;
    }
    Node *pointer; 
}; 
// This function checks if the given linked list is a palindrome or not 
bool checkPalindrome(Node* head){
    // temp pointer 'temp' is declared
    Node* temp = head;
    // A stack is declared
    stack <int> stack_value;
    // The elements in the list are pushed to the stack
    while (temp != NULL){
        stack_value.push(temp -> data);
        temp = temp -> pointer;
    }
    // Checks the list and the values are popped from the stack
    while(head != NULL){
        // stack_value.top() gives the topmost element in the stack     
        int top_element = stack_value.top();
        // This checks if the data is not same element as the element which was popped
        if(head -> data != top_element){
            return false; 
        }
        // Element is popped
        stack_value.pop();
        head = head -> pointer; 
    }
    return true; 
} 
  
// Main Function starts here   
int main(){
    // Test Case 1: 
    // Linked list addition
    Node t1n1 =  Node('a'); 
    Node t1n2 = Node('b');
    cout << t1n1.data << " -> " << t1n2.data << endl;
    // Pointers of the linked list are initialized
    t1n2.pointer = NULL; 
    t1n1.pointer = &t1n2;
    Node* temp1 = &t1n1;
    // checkPalindrome function is called and returns a boolean value    
    int value1 = checkPalindrome(&t1n1);
    if(value1 == 1){
        cout << "Palindrome \n" << endl; 
    }
    else{
        cout << "Not Palindrome \n" << endl; 
    }
    
    // Test Case 2: 
    // Linked list addition
    Node t2n1 =  Node('a'); 
    Node t2n2 = Node('b');
    Node t2n3 = Node('a');
    cout << t2n1.data << " -> " << t2n2.data << " -> " << t2n3.data << endl;
    // Pointers of the linked list are initialized
    t2n3.pointer = NULL; 
    t2n1.pointer = &t2n2;
    t2n2.pointer = &t2n3;
    Node* temp2 = &t2n1;
    // checkPalindrome function is called and returns a boolean value    
    int value2 = checkPalindrome(&t2n1);
    if(value2 == 1){
        cout << "Palindrome \n" << endl; 
    }
    else{
        cout << "Not Palindrome \n" << endl; 
    }

    // Test Case 3: 
    // Linked list addition
    Node t3n1 =  Node('s'); 
    Node t3n2 = Node('a');
    Node t3n3 = Node('g');
    Node t3n4 = Node('a');
    Node t3n5 = Node('r');
    cout << t3n1.data << " -> " << t3n2.data << " -> " << t3n3.data << " -> " << t3n4.data << " -> " << t3n5.data << endl;
    // Pointers of the linked list are initialized
    t3n5.pointer = NULL; 
    t3n1.pointer = &t3n2;
    t3n2.pointer = &t3n3;
    t3n3.pointer = &t3n4;
    t3n4.pointer = &t3n5;
    Node* temp3 = &t3n1;
    // checkPalindrome function is called and returns a boolean value    
    int value3 = checkPalindrome(&t3n1);
    if(value3 == 1){
        cout << "Palindrome \n" << endl; 
    }
    else{
        cout << "Not Palindrome \n" << endl; 
    }

    // Test Case 4: 
    // Linked list addition
    Node t4n1 =  Node('r'); 
    Node t4n2 = Node('a');
    Node t4n3 = Node('d');
    Node t4n4 = Node('a');
    Node t4n5 = Node('r');
    cout << t4n1.data << " -> " << t4n2.data << " -> " << t4n3.data << " -> " << t4n4.data << " -> " << t4n5.data << endl;
    // Pointers of the linked list are initialized
    t4n5.pointer = NULL; 
    t4n1.pointer = &t4n2;
    t4n2.pointer = &t4n3;
    t4n3.pointer = &t4n4;
    t4n4.pointer = &t4n5;
    Node* temp4 = &t4n1;
    // checkPalindrome function is called and returns a boolean value    
    int value4 = checkPalindrome(&t4n1);
    if(value4 == 1){
        cout << "Palindrome \n" << endl; 
    }
    else{
        cout << "Not Palindrome \n" << endl; 
    }
} 

OUTPUT:


Related Solutions

You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
C Programming Question: Q) Write a C - program to check whether a given graph is...
C Programming Question: Q) Write a C - program to check whether a given graph is Bipartite using Breadth-first search (adjacency list) Do take your time but please do post full code and also please do it in C.
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT