update readme & added image

master
Furkan Türkal 7 years ago
parent dc805fe272
commit 901da63bee

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

@ -4,29 +4,37 @@
## What It Is
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.
Like arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers.
To delete a node from linked list, we need to do following steps.
![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-1-introduction/resources/linked-list.png)
* 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.
Why Linked List ?
--------------------------
> * 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]`
Arrays can be used to store linear data of similar types, but arrays have following limitations ;
> * Input: Position = 1, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`
> * Output: Linked List = `[8 -> 3 -> 1 -> 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 = 0, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]`
> * Output: Linked List = `[2 -> 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.
![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-3-deleting-a-node/resources/deleting-a-node.png)
**Example**
**Algorithm Complexity**
> * ID[] = [1000, 1010, 1050, 2000, 2040]
| Complexity | Notation |
| ----------------- |:---------:|
| `Time Complexity` | `O(n)` |
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.
Loading…
Cancel
Save