
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a doubly linked list of characters is palindrome or not in C++
Here we will see how to check a string is a palindrome or not using a Doubly linked list. Here we will push each character of a string inside one doubly linked list. There will be two pointers, left and right. Then start scanning from both sides. If a left character is the same as right character, then move the left pointer to the next node, and move the right pointer to the previous node. Otherwise, return false. This process will be continued until the left and right are pointing at the same node, or the right pointer is pointing to the previous element of the left pointer.
Example
#include <iostream> using namespace std; class Node { public: char data; Node *next; Node *prev; }; void getNode(Node** start, char new_data) { struct Node* newNode = new Node; newNode->data = new_data; newNode->next = (*start); newNode->prev = NULL; if ((*start) != NULL) (*start)->prev = newNode ; (*start) = newNode; } bool isPalindrome(Node *left) { if (left == NULL) return true; Node *right = left; while (right->next != NULL) right = right->next; while (left != right && right != left->prev) { if (left->data != right->data) return false; left = left->next; right = right->prev; } return true; } int main() { Node* head = NULL; string str = "madam"; for(int i = 0; i< str.length(); i++){ getNode(&head, str[i]); } if (isPalindrome(head)) cout << "This is Palindrome"; else cout << "This is Not a Palindrome"; }
Output
This is Palindrome
- Related Articles
- Program to check linked list items are forming palindrome or not in Python
- JavaScript Program to Check If a Singly Linked List is Palindrome
- Check if a linked list is Circular Linked List in C++
- C# program to check if a string is palindrome or not
- Recursive program to check if number is palindrome or not in C++
- Write a C# program to check if a number is Palindrome or not
- C Program to check if an Array is Palindrome or not
- Program to check string is palindrome with lowercase characters or not in Python
- Check if any anagram of a string is palindrome or not in Python
- Reverse a Doubly Linked List using C++
- Check if the value entered is palindrome or not using C language
- Python program to check if a string is palindrome or not
- Check if number is palindrome or not in Octal in Python
- Check if a Linked List is Pairwise Sorted in C++
- Program to check if an Array is Palindrome or not using STL in C++

Advertisements