From 4cfd9054de4dcb3f1fbfb300d98ff66954b7ac1f Mon Sep 17 00:00:00 2001 From: Anjuman Hasan <82674743+AnjumanHasan@users.noreply.github.com> Date: Sun, 2 Oct 2022 11:13:14 +0530 Subject: [PATCH] Organized readme.md of data structures and added time complexity (#31) * Update readme.md * Create Array.md * Update Array.md * Update Array.md * Update readme.md * Update readme.md * Update readme.md * Update readme.md * Update readme.md * added blobaccess * Cleaning Readme.md * Update readme.md * Create LinkedList.md * Created New readme.md files for array and ll Created a blob access for other readme.md files which prevents the data structures readme to get too long. Will be adding the same for stack and other data structures as well * added changes * changed file structure * added array and linked list * Update readme.md --- Data Structures/Array.md | 38 +++++++++++++++++++++++++++++++ Data Structures/LinkedList.md | 43 +++++++++++++++++++++++++++++++++++ Data Structures/readme.md | 25 ++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 Data Structures/Array.md create mode 100644 Data Structures/LinkedList.md diff --git a/Data Structures/Array.md b/Data Structures/Array.md new file mode 100644 index 00000000..3af9740a --- /dev/null +++ b/Data Structures/Array.md @@ -0,0 +1,38 @@ +## Array +An array is a data structure that stores a collection of data values in contiguous memory locations. + +### Example +``` +let array = [4,3,8,1,0,14,6]; +``` + +| Memory Location | Value | +|-----------------|-------| +| 1000 | 4 | +| 1001 | 3 | +| 1002 | 8 | +| 1003 | 1 | +| 1004 | 0 | +| 1005 | 14 | +| 1006 | 6 | + +Accessing an element in an array is done by indexing into it. +``` +array[0] // 4 (index 0 is the first element) (1000) +array[1] // 3 (index 1 is the second element) (1001) +array[2] // 8 (index 2 is the third element) (1002) +array[3] // 1 (index 3 is the fourth element) (1003) +array[4] // 0 (index 4 is the fifth element) (1004) +array[5] // 14 (index 5 is the sixth element) (1005) +array[6] // 6 (index 6 is the seventh element) (1006) +``` + +#### Time complexities of some basic array operations + +| Operation | Time Complexity | +|-----------------|-------| +| Accessing an element | O(1) | +| Searching an element | O(N) | +| Inserting an element | O(N) | +| Deleting an element | O(N) | + diff --git a/Data Structures/LinkedList.md b/Data Structures/LinkedList.md new file mode 100644 index 00000000..eee8181c --- /dev/null +++ b/Data Structures/LinkedList.md @@ -0,0 +1,43 @@ + +## LinkedList +A linked list is a data structure that consists of a group of nodes where each node contains a data value and a reference (or link) to the next node in the list. + +### Example +``` +let list = [4,3,1,0,14,6]; +``` + +| Memory Location | Value | +|-----------------|-------| +| 1000 | 4 | +| 1001 | 1008 | +| 1002 | 1 | +| 1003 | 1004 | +| 1004 | 0 | +| 1005 | 1006 | +| 1006 | 14 | +| 1007 | 1010 | +| 1008 | 3 | +| 1009 | 1002 | +| 1010 | 6 | +| 1011 | 1000 | + +Accessing an element in a linked list is done by traversing the list and finding the element at the correct index. +``` +index 0 value is 4 with memory location 1000 and pointer to next node 1008 +index 1 value is 3 with memory location 1008 and pointer to next node 1002 +index 2 value is 1 with memory location 1002 and pointer to next node 1004 +index 3 value is 0 with memory location 1004 and pointer to next node 1006 +index 4 value is 14 with memory location 1006 and pointer to next node 1010 +index 5 value is 6 with memory location 1010 and pointer to first node 1000 +``` + +#### Time complexities of some basic Linked List operations + +| Operation | Time Complexity | +|-----------------|-------| +| Accesing an element | O(N) | +| Searching an element | O(N) | +| Inserting an element | O(1) | +| Deleting an element | O(1) | + diff --git a/Data Structures/readme.md b/Data Structures/readme.md index 91ff366c..ec20a7af 100644 --- a/Data Structures/readme.md +++ b/Data Structures/readme.md @@ -40,6 +40,18 @@ array[5] // 14 (index 5 is the sixth element) (1005) array[6] // 6 (index 6 is the seventh element) (1006) ``` +#### Time complexities of some basic array operations + +| Operation | Time Complexity | +|-----------------|-------| +| Accessing an element | O(1) | +| Searching an element | O(N) | +| Inserting an element | O(N) | +| Deleting an element | O(N) | + +[More on Array](Array.md) + + ## LinkedList A linked list is a data structure that consists of a group of nodes where each node contains a data value and a reference (or link) to the next node in the list. @@ -73,6 +85,19 @@ index 4 value is 14 with memory location 1006 and pointer to next node 1010 index 5 value is 6 with memory location 1010 and pointer to first node 1000 ``` +#### Time complexities of some basic Linked List operations + +| Operation | Time Complexity | +|-----------------|-------| +| Accesing an element | O(N) | +| Searching an element | O(N) | +| Inserting an element | O(1) | +| Deleting an element | O(1) | + + +[More on Linked List](LinkedList.md) + + ## Stack A stack is a data structure that stores a collection of data values in a LIFO (last in, first out) order.