From ad97178a8b1bdc8c6ad79725706822596ee72de9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20T=C3=BCrkal?= Date: Thu, 26 Oct 2017 23:42:58 +0300 Subject: [PATCH] update readme --- linked-list-1-introduction/README.md | 36 ++++++++++++++++++++- linked-list-3-deleting-a-node/README.md | 42 ++++++++++--------------- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/linked-list-1-introduction/README.md b/linked-list-1-introduction/README.md index 12278ed..8d4db4a 100644 --- a/linked-list-1-introduction/README.md +++ b/linked-list-1-introduction/README.md @@ -1,6 +1,40 @@ -

Linked List | SET 1 (INTRODUCTION) Source

+

LinkedList | SET 3 (DELETING A NODE) Source

[What It Is](#what-it-is) ## What It Is +Like arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers. + +![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-1-introduction/resources/linked-list.png) + +Why Linked List ? +-------------------------- + +Arrays can be used to store linear data of similar types, but arrays have following limitations ; + +* 1) The size of the arrays is fixed. So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to upper limit irrespective of the usage. + +* 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. + +**Example** + +> * ID[] = [1000, 1010, 1050, 2000, 2040] + +And if we want to insert a new ID 10005, the to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). + +Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in ID[], everythink after 1010 has to moved. + +Advantages Over Arrays +-------------------------- + +* 1) Dynamic size + +* 2) Ease of insertion/deletion + +Drawbacks +-------------------------- + +* 1) Random access is not allowed. We have to access elements sequientally starting from the first node. So we cannot do binary search with LinkedLists. + +* 2) Extra memory space for a pointer is required with each element of the list. \ No newline at end of file diff --git a/linked-list-3-deleting-a-node/README.md b/linked-list-3-deleting-a-node/README.md index 8d4db4a..053892d 100644 --- a/linked-list-3-deleting-a-node/README.md +++ b/linked-list-3-deleting-a-node/README.md @@ -4,37 +4,29 @@ ## What It Is -Like arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers. +We have discussed **[Linked List Introduction](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-1-introduction)** and **[Linked List Insertion](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-2-inserting-a-node)** in previous posts on singly linked list. +Let us formulate the problem statement to understand the deletion process. Given a `key`, delete the first occurrence of this key in linked list. -![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-1-introduction/resources/linked-list.png) +To delete a node from linked list, we need to do following steps. -Why Linked List ? --------------------------- +* 1) Find previous node of the node to be deleted. +* 2) Changed next of previous node. +* 3) Free memory for the node to be deleted. -Arrays can be used to store linear data of similar types, but arrays have following limitations ; +> * Input: Linked List = `[7 -> 1 -> 3 -> 2]` +> * Output: Created Linked List `[2 -> 3 -> 1 -> 7]` +> * Output: Linked List after Deletion of 1: `[2 -> 3 -> 7]` -* 1) The size of the arrays is fixed. So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to upper limit irrespective of the usage. +> * Input: Position = 1, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]` +> * Output: Linked List = `[8 -> 3 -> 1 -> 7]` -* 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. +> * Input: Position = 0, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]` +> * Output: Linked List = `[2 -> 3 -> 1 -> 7]` -**Example** -> * ID[] = [1000, 1010, 1050, 2000, 2040] -And if we want to insert a new ID 10005, the to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). +**Algorithm Complexity** -Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in ID[], everythink after 1010 has to moved. - -Advantages Over Arrays --------------------------- - -* 1) Dynamic size - -* 2) Ease of insertion/deletion - -Drawbacks --------------------------- - -* 1) Random access is not allowed. We have to access elements sequientally starting from the first node. So we cannot do binary search with LinkedLists. - -* 2) Extra memory space for a pointer is required with each element of the list. \ No newline at end of file +| Complexity | Notation | +| ----------------- |:---------:| +| `Time Complexity` | `O(1)` |