Created
June 24, 2020 06:41
-
-
Save ahmadmustafaanis/76299abddcd9e4b5df191ae364ab40db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct node { | |
int data; | |
node* next; | |
node* back; | |
}; | |
class LinkedList { | |
public: | |
LinkedList(); | |
node* front, * curr, * prev, * rear; | |
void printing(); | |
void pushFront(int); | |
void rearInsertion(int); | |
bool isEmpty(); | |
int reversePop(); | |
int popFront(); | |
}; | |
LinkedList::LinkedList() | |
{ | |
front = curr = prev = rear = nullptr; | |
} | |
int LinkedList::popFront() | |
{ | |
node* temp; | |
if (front == nullptr) | |
{ | |
throw "Linked List is Empty\n"; | |
} | |
else { | |
temp = front; | |
int data = front->data; | |
if (front->next != nullptr) | |
{ | |
front = front->next; | |
front->back = nullptr; | |
} | |
else { | |
front->next = nullptr; | |
front->back = nullptr; | |
front = nullptr; | |
} | |
delete temp; | |
return data; | |
} | |
} | |
int LinkedList::reversePop() | |
{ | |
node* temp; | |
if (rear == nullptr) | |
{ | |
throw "Empty Linked List\n"; | |
} | |
else { | |
int data = rear->data; | |
temp = rear; | |
if (rear->back == nullptr) | |
{ | |
rear->next = nullptr; | |
rear->back = nullptr; | |
rear = nullptr; | |
front = rear; | |
} | |
else | |
{ | |
rear = rear->back; | |
rear->next = nullptr; | |
} | |
delete temp; | |
return data; | |
} | |
} | |
bool LinkedList::isEmpty() | |
{ | |
return (front == nullptr or rear == nullptr); | |
} | |
void LinkedList::rearInsertion(int no) | |
{ | |
if (rear == nullptr) | |
{ | |
rear = new node; | |
rear->data = no; | |
rear->next = nullptr; | |
rear->back = nullptr; | |
front = prev = rear; | |
} | |
else { | |
node* temp = new node; | |
rear->next = temp; | |
temp->back = rear; | |
temp->next = nullptr; | |
temp->data = no; | |
rear = temp; | |
} | |
} | |
void LinkedList::pushFront(int no) | |
{ | |
if (front == nullptr) | |
{ | |
front = new node; | |
front->data = no; | |
front->back = nullptr; | |
front->next = nullptr; | |
curr = prev = rear = front; | |
} | |
else { | |
node* temp = new node; | |
temp->data = no; | |
temp->next = front; | |
temp->back = nullptr; | |
front = temp; | |
} | |
} | |
void LinkedList::printing() | |
{ | |
curr = front; | |
while (curr) | |
{ | |
cout << curr->data << endl; | |
curr = curr->next; | |
} | |
} | |
int main() | |
{ | |
LinkedList stack; | |
LinkedList queue; | |
int no; | |
int choice; | |
int subchoice; | |
do { | |
cout << "Type 1 for Stack \n2 For Queue\n3 to end program\n"; | |
cin >> choice; | |
switch (choice) | |
{ | |
case 1: | |
cout << "Type 1 for push\n2 for pop\n3 for printing stack\n"; | |
cin >> subchoice; | |
switch (subchoice) | |
{ | |
case 1: | |
cout << "What to push in stack\n"; | |
cin >> no; | |
stack.rearInsertion(no); | |
break; | |
case 2: | |
no = stack.reversePop(); | |
cout << no << " is poped from stack\n"; | |
break; | |
case 3: | |
stack.printing(); | |
break; | |
default: | |
break; | |
} | |
break; | |
case 2: | |
cout << "Type 1 for push\n2 for pop\n3 for printing queue\n"; | |
cin >> subchoice; | |
switch (subchoice) | |
{ | |
case 1: | |
cout << "What to push in queue\n"; | |
cin >> no; | |
queue.rearInsertion(no); | |
break; | |
case 2: | |
no = queue.popFront(); | |
cout << no << " is poped from queue\n"; | |
break; | |
case 3: | |
queue.printing(); | |
break; | |
default: | |
break; | |
} | |
break; | |
default: | |
break; | |
} | |
} while (choice != 3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment