Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added recursive reverse in Circular Doubly Linked Lists #202

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*

This is a Circular Doubly Linked List program which recursively reverses the Linked Lists.
Since, it is a Ciruclar Doubly Linked List, both the forward and backward traversal is also possible.

*/

#include <iostream>

using namespace std;

/*

Node definition:
1. Pointer to previous node.
2. Integer Data
3. Pointer to next node.

*/

class dcll_node {
public:
dcll_node* prev;
int data;
dcll_node* next;
};

void createDCLL(dcll_node* &head) {

dcll_node* temp = head;

int choice;

do {

int data;

cout << "Enter Data : ";
cin >> data;

dcll_node* newNode = new dcll_node();
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;

if(head == NULL) {
head = newNode;
temp = head;
head->next = head;
head->prev = head;
} else {
temp->next = newNode;
newNode->prev = temp;
temp = newNode;
newNode->next = head;
head->prev = newNode;
}

cout << "Do you want to continue? (1/0) : ";
cin >> choice;

} while(choice == 1);

}

dcll_node* recursive_reverse(dcll_node *head) {

if(head == NULL || head->next == head)
return head;

head->prev->next = head->next;
head->next->prev = head->prev;
dcll_node* tHead = recursive_reverse(head->next);
tHead->prev->next = head;
head->prev = tHead->prev;
head->next = tHead;
tHead->prev = head;
return tHead;
}

void display(dcll_node* head) {

if(head == NULL)
return;

cout << "The elements are : ";
dcll_node* temp = head;

do {

cout << temp->data << " ";
temp = temp->next;

} while(temp != head);
cout << endl;
}

int main() {

dcll_node* head = NULL;

createDCLL(head);

cout << "Before reversing : " << endl;
display(head);

head = recursive_reverse(head);

cout << "After reversing : " << endl;
display(head);

return 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*

This is a Circular Doubly Linked List program which deletes a Node at the end of the Linked Lists.
This is a Circular Doubly Linked List program which reverses the Linked Lists.
Since, it is a Ciruclar Doubly Linked List, both the forward and backward traversal is also possible.

*/
Expand Down