Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. typedef struct node {
  8.    
  9.     int value;
  10.     node *next;
  11.    
  12. }node;
  13.  
  14. typedef struct linkedList {
  15.    
  16.     node *head;
  17.    
  18. }linkedList;
  19.  
  20. bool isEmpty(linkedList*);
  21. int size(linkedList*);
  22. bool add(linkedList*, int);
  23. bool remove(linkedList*, int);
  24. void printAll(linkedList*);
  25.  
  26. bool isPalindrome(linkedList*);
  27.  
  28. int main () {
  29.    
  30.    
  31.     int n;
  32.     cin>>n;
  33.    
  34.     linkedList *list;
  35.    
  36.     int tmp;
  37.     for(int i=0; i<n; i++){
  38.        
  39.         cin>>tmp;
  40.         add(list, tmp);
  41.     }
  42.    
  43.     //printAll(list);
  44.    
  45.     if(isPalindrome(list))
  46.         cout<<"palindrome\n";
  47.     else
  48.         cout<<"not palindrome\n";
  49.    
  50.    
  51.     return 0;
  52. }
  53.  
  54. bool isEmpty(linkedList *list){
  55.    
  56.     return list->head == NULL;
  57. }
  58.  
  59. int size(linkedList *list){
  60.    
  61.     if(isEmpty(list))
  62.         return 0;
  63.    
  64.     else{
  65.        
  66.         int size = 1;
  67.         node *tmp = list->head;
  68.        
  69.         while(tmp->next != NULL){
  70.            
  71.             size++;
  72.             tmp = tmp->next;
  73.         }
  74.        
  75.         return size;
  76.     }
  77. }
  78.  
  79. bool add(linkedList *list, int value){
  80.    
  81.     if(isEmpty(list)){
  82.        
  83.         if(!(list->head = (node*)malloc(sizeof(node))))
  84.             return false;
  85.        
  86.         list->head->value = value;
  87.         list->head->next = NULL;
  88.        
  89.         return true;
  90.     }
  91.    
  92.     else{
  93.        
  94.         node *tmp = list->head;
  95.        
  96.         while(tmp->next != NULL)
  97.             tmp = tmp->next;
  98.        
  99.         if(!(tmp->next = (node*)malloc(sizeof(node))))
  100.             return false;
  101.        
  102.         tmp = tmp->next;
  103.         tmp->value = value;
  104.         tmp->next = NULL;
  105.        
  106.         return true;
  107.     }
  108. }
  109.  
  110. bool remove(linkedList *list, int value){
  111.    
  112.     node* tmp = list->head;
  113.    
  114.     if(tmp->value == value){
  115.         list->head = tmp->next;
  116.         return true;
  117.     }
  118.    
  119.     else{
  120.        
  121.         while(tmp->next != NULL){
  122.            
  123.             if(tmp->next->value == value){
  124.                 tmp->next = tmp->next->next;
  125.                 return true;
  126.             }
  127.            
  128.             tmp = tmp->next;
  129.         }
  130.     }
  131.    
  132.     return false;
  133. }
  134.  
  135. void printAll(linkedList *list){
  136.    
  137.     if(isEmpty(list))
  138.         return;
  139.    
  140.     else{
  141.        
  142.         node *tmp = list->head;
  143.        
  144.         while(tmp != NULL){
  145.            
  146.             cout<<tmp->value<<"\n";
  147.             tmp = tmp->next;
  148.         }
  149.     }
  150. }
  151.  
  152. bool isPalindrome(linkedList *list){
  153.    
  154.     if(isEmpty(list) || size(list) == 1)
  155.         return true;
  156.    
  157.     else{
  158.        
  159.         stack<int> palCheck;
  160.         node *tmp;
  161.         int listSize = size(list);
  162.        
  163.         tmp = list->head;
  164.         for(int i = 0; i < listSize / 2; i++){
  165.            
  166.             palCheck.push(tmp->value);
  167.             tmp = tmp->next;
  168.         }
  169.        
  170.         if(listSize % 2 == 1)
  171.             tmp = tmp->next;
  172.        
  173.         while(tmp->next != NULL){
  174.            
  175.             if(tmp->value == palCheck.top()){
  176.                 palCheck.pop();
  177.                 tmp = tmp->next;
  178.             }
  179.             else
  180.                 return false;
  181.         }
  182.         return true;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement