m2skills

isPalindrome LL c++

Jan 28th, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class node{
  8.  
  9. protected:
  10.     int element;
  11.     node* link;
  12.  
  13. public:
  14.     //constructor that accepts only element
  15.     node(int element) {
  16.         this->element = element;
  17.         this->link = NULL;
  18.     }
  19.  
  20.     //constructor that accepts both link and element
  21.     node(int element, node* link){
  22.         this->element = element;
  23.         this->link = link;
  24.     }
  25.  
  26.     //method to update the element
  27.     void updateData(int element){
  28.         this->element = element;
  29.     }
  30.  
  31.     //method to update or setup link
  32.     void updateLink(node* link){
  33.         this->link = link;
  34.     }
  35.  
  36.     //method to get the element from the node
  37.     int getElement(){
  38.         return this->element;
  39.     }
  40.  
  41.     //method to get the next node
  42.     node* getNextNode(){
  43.         return this->link;
  44.     }
  45. };
  46.  
  47. // checking if linked list is a palindrome
  48. bool isPalindrome(node* right){
  49.     if(right == NULL){
  50.         return true;
  51.     }
  52.     static node* left = right;
  53.  
  54.     if(right->getNextNode() != NULL){
  55.         right = right->getNextNode();
  56.         isPalindrome(right);
  57.     }
  58.  
  59.     if(left->getElement() == right->getElement()){
  60.         left = left->getNextNode();
  61.         return true;
  62.     }
  63. }
  64.  
  65. class Linkedlist {
  66. public:
  67.     node *head;
  68.  
  69.     //constructor for the Linked List class
  70.     Linkedlist() {
  71.         head = NULL;
  72.     }
  73.  
  74.     //returns head node
  75.     node *getHead() {
  76.         return this->head;
  77.     }
  78.  
  79.     // method to add a node at the end
  80.     void insert(int element) {
  81.         node *tempNode = new node(element);
  82.         node *p = head;
  83.         if (head == NULL) {
  84.             head = tempNode;
  85.             return;
  86.         }
  87.         else {
  88.             while (p->getNextNode() != NULL) {
  89.                 p = p->getNextNode();
  90.             }
  91.             p->updateLink(tempNode);
  92.             return;
  93.         }
  94.     }
  95.  
  96.     //method to display all the elements of the Linked List
  97.     void display() {
  98.         cout << "\n";
  99.         node *tempNode = head;
  100.         while (tempNode != NULL) {
  101.             if (tempNode->getNextNode() != NULL)
  102.                 cout << tempNode->getElement() << " --> ";
  103.             else
  104.                 cout << tempNode->getElement();
  105.  
  106.             tempNode = tempNode->getNextNode();
  107.         }
  108.         return;
  109.     }
  110.  
  111. };
  112.  
  113. int main() {
  114.     cout<<"Program to check if a Linked List is Palindrome or not";
  115.     Linkedlist l1;
  116.     l1.insert(1);
  117.     l1.insert(3);
  118.     l1.insert(5);
  119.     l1.insert(7);
  120.     l1.insert(7);
  121.     l1.insert(8);
  122.     l1.insert(5);
  123.     l1.insert(3);
  124.     l1.insert(1);
  125.  
  126.     cout<<"\nLinked list is : ";
  127.     l1.display();
  128.     if(isPalindrome(l1.getHead()))
  129.         cout<<"\nThe given Linked List is Palindrome";
  130.     else
  131.         cout<<"\nThe given Linked List is not a Palindrome";
  132.  
  133.     return 0;
  134. }
Add Comment
Please, Sign In to add comment