Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <iostream>
- #include <string>
- using std::cin;
- using std::cout;
- void print(struct node *p, struct node *pb);
- void enqueue(struct node **p, struct node **pb, char dataToAdd);
- void deque(struct node **p, struct node **pb);
- void peek(struct node *p);
- void enqueue2(struct node **p, struct node **pb, char dataToAdd);
- void deque2(struct node **p, struct node **pb);
- void peek2(struct node *pb);
- bool isPalindrome(struct node *p, struct node *q);
- struct node
- {
- char data;
- struct node *link;
- };
- int main()
- {
- struct node *front = (struct node *)malloc(sizeof(struct node));
- front = NULL;
- struct node *rear = (struct node *)malloc(sizeof(struct node));
- rear = front;
- while (true)
- {
- cout << "\nEnter your choice: \n1. Enque\n2. Deque\n3. Peek\n4. View all\n5. Check if palindrome\n6. Exit\n";
- int ch;
- cin >> ch;
- if (ch == 6)
- {
- break;
- }
- int db;
- switch (ch)
- {
- case 1:
- cout << "Enter element: ";
- char data;
- cin >> data;
- cout << "1. Rear\n2. Front\n";
- cin >> db;
- if (db == 1)
- {
- enqueue(&front, &rear, data);
- }
- else if (db == 2)
- {
- enqueue2(&front, &rear, data);
- }
- break;
- case 2:
- cout << "1. Rear\n2. Front\n";
- cin >> db;
- if (db == 1)
- {
- deque2(&front, &rear);
- }
- else if (db == 2)
- {
- deque(&front, &rear);
- }
- break;
- case 3:
- peek(rear);
- break;
- case 4:
- cout << "\n";
- print(front, rear);
- break;
- case 5:
- if (isPalindrome(front, rear))
- {
- cout << "\nThe end is the beginning and the beginning is the end!";
- }
- else
- {
- cout << "\nNot a Palindrome!";
- }
- break;
- default:
- cout << "Invalid choice\n";
- }
- }
- }
- void print(struct node *p, struct node *pb)
- {
- struct node *t = p;
- cout << "\n";
- if (t == NULL)
- {
- cout << "Empty Queue\n";
- }
- else
- {
- while (true)
- {
- cout << t->data << " -> ";
- t = t->link;
- if (t == NULL)
- {
- break;
- }
- }
- cout << " NULL";
- }
- }
- void enqueue(struct node **p, struct node **pb, char dataToAdd)
- {
- if (*pb == NULL)
- {
- struct node *temp = (struct node *)malloc(sizeof(struct node));
- temp->data = dataToAdd;
- temp->link = NULL;
- (*pb) = temp;
- (*p) = temp;
- }
- else
- {
- struct node *temp = (struct node *)malloc(sizeof(struct node));
- struct node *t = (*pb);
- temp->data = dataToAdd;
- temp->link = NULL;
- t->link = temp;
- t = t->link;
- (*pb) = t;
- }
- }
- void deque(struct node **p, struct node **pb)
- {
- struct node *t = (*p);
- if (t == NULL)
- {
- cout << "Empty queue";
- }
- else if (t == (*pb))
- {
- (*pb) = NULL;
- (*p) = NULL;
- }
- else
- {
- (*p) = (*p)->link;
- free(t);
- }
- }
- void enqueue2(struct node **p, struct node **pb, char dataToAdd)
- {
- if (*p == NULL)
- {
- struct node *temp = (struct node *)malloc(sizeof(struct node));
- temp->data = dataToAdd;
- temp->link = NULL;
- (*pb) = temp;
- (*p) = temp;
- }
- else
- {
- struct node *temp = (struct node *)malloc(sizeof(struct node));
- struct node *t = (*p);
- temp->data = dataToAdd;
- temp->link = t;
- // t->link = temp;
- // t = t->link;
- t = temp;
- (*p) = t;
- }
- }
- void deque2(struct node **p, struct node **pb)
- {
- struct node *t = (*pb);
- struct node *t1 = (*p);
- if (t == NULL)
- {
- cout << "Empty queue";
- }
- else if (t == (*p))
- {
- (*pb) = NULL;
- (*p) = NULL;
- }
- else
- {
- while (1)
- {
- if (t1->link == (*pb))
- {
- break;
- }
- t1 = t1->link;
- }
- (*pb) = t1;
- (*pb)->link = NULL;
- t1 = t1->link;
- free(t1);
- }
- }
- void peek(struct node *p)
- {
- if (p == NULL)
- {
- cout << "Empty queue";
- return;
- }
- cout << p->data;
- }
- void peek2(struct node *pb)
- {
- if (pb == NULL)
- {
- cout << "Empty queue";
- return;
- }
- cout << pb->data;
- }
- bool isPalindrome(struct node *p, struct node *q)
- {
- struct node *f = (struct node *)malloc(sizeof(struct node));
- f = NULL;
- struct node *r = (struct node *)malloc(sizeof(struct node));
- r = f;
- struct node *t = p;
- while (t != NULL)
- {
- enqueue(&f, &r, t->data);
- t = t->link;
- }
- // print(f, r);
- struct node *t1 = f, *t2 = r;
- struct node *t3 = f;
- while (true)
- {
- if (t1 == t2 || t2 == t3)
- break;
- if (t1->data != t2->data)
- {
- // cout << "False";
- return false;
- }
- t1 = t1->link;
- deque2(&t1, &t2);
- }
- // cout << "True";
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment