Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- typedef struct node {
- int value;
- node *next;
- }node;
- typedef struct linkedList {
- node *head;
- }linkedList;
- bool isEmpty(linkedList*);
- int size(linkedList*);
- bool add(linkedList*, int);
- bool remove(linkedList*, int);
- void printAll(linkedList*);
- bool isPalindrome(linkedList*);
- int main () {
- int n;
- cin>>n;
- linkedList *list;
- int tmp;
- for(int i=0; i<n; i++){
- cin>>tmp;
- add(list, tmp);
- }
- //printAll(list);
- if(isPalindrome(list))
- cout<<"palindrome\n";
- else
- cout<<"not palindrome\n";
- return 0;
- }
- bool isEmpty(linkedList *list){
- return list->head == NULL;
- }
- int size(linkedList *list){
- if(isEmpty(list))
- return 0;
- else{
- int size = 1;
- node *tmp = list->head;
- while(tmp->next != NULL){
- size++;
- tmp = tmp->next;
- }
- return size;
- }
- }
- bool add(linkedList *list, int value){
- if(isEmpty(list)){
- if(!(list->head = (node*)malloc(sizeof(node))))
- return false;
- list->head->value = value;
- list->head->next = NULL;
- return true;
- }
- else{
- node *tmp = list->head;
- while(tmp->next != NULL)
- tmp = tmp->next;
- if(!(tmp->next = (node*)malloc(sizeof(node))))
- return false;
- tmp = tmp->next;
- tmp->value = value;
- tmp->next = NULL;
- return true;
- }
- }
- bool remove(linkedList *list, int value){
- node* tmp = list->head;
- if(tmp->value == value){
- list->head = tmp->next;
- return true;
- }
- else{
- while(tmp->next != NULL){
- if(tmp->next->value == value){
- tmp->next = tmp->next->next;
- return true;
- }
- tmp = tmp->next;
- }
- }
- return false;
- }
- void printAll(linkedList *list){
- if(isEmpty(list))
- return;
- else{
- node *tmp = list->head;
- while(tmp != NULL){
- cout<<tmp->value<<"\n";
- tmp = tmp->next;
- }
- }
- }
- bool isPalindrome(linkedList *list){
- if(isEmpty(list) || size(list) == 1)
- return true;
- else{
- stack<int> palCheck;
- node *tmp;
- int listSize = size(list);
- tmp = list->head;
- for(int i = 0; i < listSize / 2; i++){
- palCheck.push(tmp->value);
- tmp = tmp->next;
- }
- if(listSize % 2 == 1)
- tmp = tmp->next;
- while(tmp->next != NULL){
- if(tmp->value == palCheck.top()){
- palCheck.pop();
- tmp = tmp->next;
- }
- else
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement