From cb63a175f01a795b7e9d82ba69d1d4bc11fc3c9e Mon Sep 17 00:00:00 2001 From: Manish-NMR <103754182+Manish-NMR@users.noreply.github.com> Date: Sun, 15 Oct 2023 17:01:26 +0530 Subject: [PATCH 1/2] Create Double linked List.cpp --- Linked List/Double linked List.cpp | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 Linked List/Double linked List.cpp diff --git a/Linked List/Double linked List.cpp b/Linked List/Double linked List.cpp new file mode 100644 index 000000000..24f054954 --- /dev/null +++ b/Linked List/Double linked List.cpp @@ -0,0 +1,157 @@ +/* +All Basics Features of Double Linked List +@author: nmanish_0128 + +- creating a Double Linked list Node +- printing a Double linked list +- inserting in a element +- deleting a element +- searching a element +*/ + +#include +using namespace std; + +// class for Node +class node { + public: + int data; + node* prev; + node* next; + node(){ + next=NULL; + prev=NULL; + } +}; + +// printing an Linked List +void print(node* head){ + node* ptr = head; + if(head==NULL){ + cout<<"Linked List is empty"<data<<" "; + ptr=ptr->next; + }while(ptr!=NULL); + } +} + +// inserting an element in a Linked List + +//insert at head +void insertHead(node* &head,int val){ + node* new_node = new node(); + new_node->data = val; + if(head==NULL){ + head=new_node; + new_node->next=NULL; + }else{ + head->prev= new_node; + new_node->next = head; + head=new_node; + } +} + +//insert at tail +void insertTail(node* head,int val){ + node* ptr = head; + node* new_node = new node(); + new_node->data=val; + while(ptr->next!=NULL){ + ptr=ptr->next; + } + new_node->prev=ptr; + ptr->next=new_node; +} + +// insert before a given val +void insertBefore(node* head,int key,int val){ + node* ptr = head; + while(ptr->next->data!=key){ + if(ptr->next->next==NULL){ + cout<<"key not found - insertBefore key("<next; + } + node* new_node= new node(); + new_node->data=val; + ptr->next->prev=new_node; + new_node->next=ptr->next; + new_node->prev=ptr; + ptr->next = new_node; +} + +//insert after a given val +void insertAfter(node* head,int key,int val){ + node* ptr = head; + while(ptr->data!=key){ + if(ptr->next==NULL){ + cout<<"key not found - insertAfter key("<next; + } + node* new_node= new node(); + new_node->data=val; + ptr->next->prev=new_node; + new_node->next=ptr->next; + new_node->prev=ptr; + ptr->next = new_node; +} + +//delete a given element +void Delete(node* &head,int key){ + node* ptr = head; + if(ptr->data==key){ + node* temp=ptr; + head=ptr->next; + head->prev=NULL; + free(temp); + return; + } + while(ptr->next->data!=key){ + if(ptr->next->next==NULL){ + cout<<"key not found - Delete key("<next; + } + node* temp=ptr->next; + temp->next->prev=ptr; + ptr->next = temp->next; + free(temp); +} + +//search a element in a Linked List +void Search(node* head,int key){ + node* ptr = head; + while(ptr->data!=key){ + if(ptr->next==NULL){ + cout<<"key not found - search key("<next; + } + cout<<"key found"< Date: Sun, 15 Oct 2023 17:01:47 +0530 Subject: [PATCH 2/2] Rename Double linked List.cpp to Double Linked List.cpp --- Linked List/{Double linked List.cpp => Double Linked List.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Linked List/{Double linked List.cpp => Double Linked List.cpp} (100%) diff --git a/Linked List/Double linked List.cpp b/Linked List/Double Linked List.cpp similarity index 100% rename from Linked List/Double linked List.cpp rename to Linked List/Double Linked List.cpp