From 8b5a07706c716a9291204fbab99d898a487e116d Mon Sep 17 00:00:00 2001 From: sayali-kadam <62012906+sayali-kadam@users.noreply.github.com> Date: Fri, 2 Oct 2020 16:00:18 +0530 Subject: [PATCH] Doubly-linked-list.cpp --- Doubly-linked-list.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Doubly-linked-list.cpp diff --git a/Doubly-linked-list.cpp b/Doubly-linked-list.cpp new file mode 100644 index 0000000..0d36580 --- /dev/null +++ b/Doubly-linked-list.cpp @@ -0,0 +1,98 @@ +// Inserting sorted elements in doubly linked list +#include + +using namespace std; + +// Node of a doubly linked list +struct Node { + int data; + struct Node* prev, *next; +}; + +// function to create and return a new node +// of a doubly linked list +struct Node* getNode(int data) +{ + // allocate node + struct Node* newNode = + (struct Node*)malloc(sizeof(struct Node)); + + // put in the data + newNode->data = data; + newNode->prev = newNode->next = NULL; + return newNode; +} + +// function to insert a new node in sorted way in +// a sorted doubly linked list +void sortedInsert(struct Node** head_ref, struct Node* newNode) +{ + struct Node* current; + + // if list is empty + if (*head_ref == NULL) + *head_ref = newNode; + + // if the node is to be inserted at the beginning + // of the doubly linked list + else if ((*head_ref)->data >= newNode->data) { + newNode->next = *head_ref; + newNode->next->prev = newNode; + *head_ref = newNode; + } + + else { + current = *head_ref; + + // locate the node after which the new node + // is to be inserted + while (current->next != NULL && + current->next->data < newNode->data) + current = current->next; + + /* Make the appropriate links */ + newNode->next = current->next; + + // if the new node is not inserted + // at the end of the list + if (current->next != NULL) + newNode->next->prev = newNode; + + current->next = newNode; + newNode->prev = current; + } +} + +// function to print the doubly linked list +void printList(struct Node* head) +{ + while (head != NULL) { + cout << head->data << " "; + head = head->next; + } +} + +// Driver program to test above +int main() +{ + /* start with the empty doubly linked list */ + struct Node* head = NULL; + + // insert the following nodes in sorted way + struct Node* new_node = getNode(8); + sortedInsert(&head, new_node); + new_node = getNode(5); + sortedInsert(&head, new_node); + new_node = getNode(3); + sortedInsert(&head, new_node); + new_node = getNode(10); + sortedInsert(&head, new_node); + new_node = getNode(12); + sortedInsert(&head, new_node); + new_node = getNode(9); + sortedInsert(&head, new_node); + + cout << "Created Doubly Linked Listn"; + printList(head); + return 0; +}