From ad4d7c72b264acb1250779be74c036c41640e206 Mon Sep 17 00:00:00 2001 From: Varun kumar Date: Sat, 24 Nov 2018 13:54:22 +0530 Subject: [PATCH] add delete function (#22404) Added a delete function in the implementation part. --- .../data-structures/linked-lists/index.md | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/guide/english/computer-science/data-structures/linked-lists/index.md b/guide/english/computer-science/data-structures/linked-lists/index.md index 0bfab23592..6df55c6a12 100644 --- a/guide/english/computer-science/data-structures/linked-lists/index.md +++ b/guide/english/computer-science/data-structures/linked-lists/index.md @@ -90,6 +90,7 @@ class List public: void display(); void insertBefore(int); + void deleteNode(int); List(); }; @@ -119,6 +120,47 @@ void List :: insertBefore(int data) count++; } +void List :: deleteNode(int loc) +{ + //delete first node + if(loc == 1 || count == 1) + { + N *node = new N; + node = head; + head = head->tail; + delete node; + } + //delete last node + else if(loc == count) + { + N *curr = new N; + N *prev = new N; + curr = head; + while(curr->tail != NULL) + { + prev = curr; + curr = curr->tail; + } + prev->tail = NULL; + end = prev; + delete curr; + } + //delete in between + else + { + N *curr=new N; + N *prev=new N; + curr=head; + for(int i=1;itail; + } + prev->tail=curr->tail; + } + count--; +} + void List :: display() { cout<<"Number of nodes in the list = "<