From 9be65f62c939c4592189e7573085d05cb963f257 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sun, 29 May 2016 17:33:01 +0530 Subject: [PATCH 1/6] Added cpp implemention of array --- Data-Structure-Arrays.md | 58 +++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index a3279570c..33da66838 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -24,26 +24,26 @@ intarray.append(0) # Adds an integer value of 0 to the array intarray.append(-1) # Adds an integer value of -1 to the array intarray.append(1) # Again adds an integer value of 1 to the array -intarray.append('d') # Would give a TypeError as the array is of integer type. +intarray.append('d') # Would give a TypeError as the array is of integer type. #Resolve the above error and then move ahead. ``` -#### Printing an `array`: +#### Printing an `array`: ```python print(intarray) # Returns array('i', [1, 4, -1]) print(intarray[0]) # Returns 1 which is the element at index 0 of the array -print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. +print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. #Resolve the above error and then move ahead. - + # To print all the elements of the array for i in intarray: print(i) ``` -#### Basic operations on `array`: +#### Basic operations on `array`: ```python len(intarray) # Returns the length of the array i.e. 3 @@ -52,9 +52,55 @@ intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 intarray.insert(1, 3) # Insert a new item with value x in the array before position i intarray.remove(1) # Remove the first occurrence of 1 from the array intarray.reverse() # Reverse the order of the items in the array -intarray.pop(1) # Removes the item with the index 1 from the array and returns it +intarray.pop(1) # Removes the item with the index 1 from the array and returns it ``` :rocket: [Run Code](https://repl.it/CWJB) [Official Docs](https://docs.python.org/3.5/library/array.html) + +## Arrays in C++ + +C++ provides a data structure, `array`, which stores a fixed-size sequential collection of elements of the same data-type. An `array` is used to store a collection of data, but it is better to think of an `array` as a collection of variables of the same type. + +#### Declaration of `array` + +```cpp +int intarray[10]; // Declares an array of integer type of size 10 with elements having random values. Index ranges from 0 to 9(i.e. size-1). +int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all elements having value 0 + +// Choose one the two declarations and then move ahead. +``` + +#### Adding elements to `array`: + +```cpp +intarray[0] = 1; // Adds an integer value of 1 at index 0 +intarray[1] = 0; // Adds an integer value of 0 at index 1 +intarray[2] = -1; // Adds an integer value of -1 at index 2 +intarray[3] = 1; // Again adds an integer value of 1 at index 3 + +intarray[4] = "dd"; // Would give a TypeError as the array is of integer type. + +// Resolve the above error and then move ahead. +``` + +#### Printing an `array`: + +```cpp +cout << intarray[0]; // Returns 1 which is element at index of the array +cout << intarray[11]; // Would give a random value as there is no element at index 11 of array. + +// To print all the elements of the array +for(int i = 0; i < n; i++) + cout << intarray[i]; +``` + +#### Basic operations on `array`: + +```cpp +cout << sizeof(intarray)/sizeof(intarray[0]); // Returns the length of the array i.e. 10. +cout << sizeof(intarray[0]); // Returns length in bytes of one array item i.e. 4 as it is an integer +``` + +:rocket: [Run Code](https://repl.it/CWZE) From 0671144e4fbcf5b83aeeec825410d8f1466805b4 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sun, 29 May 2016 18:15:47 +0530 Subject: [PATCH 2/6] Added Data-Structures-Linked-Lists.md Update --- Data-Structure-Arrays.md | 12 +- Data-Structure-Linked-Lists.md | 412 +++++++++++++++++++++++++++++++++ 2 files changed, 418 insertions(+), 6 deletions(-) create mode 100644 Data-Structure-Linked-Lists.md diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 33da66838..55fc1e02c 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -88,19 +88,19 @@ intarray[4] = "dd"; // Would give a TypeError as the array is of integer type. #### Printing an `array`: ```cpp -cout << intarray[0]; // Returns 1 which is element at index of the array -cout << intarray[11]; // Would give a random value as there is no element at index 11 of array. +std::cout << intarray[0] << '\n'; // Returns 1 which is element at index of the array +std::cout << intarray[11] << '\n'; // Would give a random value as there is no element at index 11 of array. // To print all the elements of the array for(int i = 0; i < n; i++) - cout << intarray[i]; + std::cout << intarray[i] << '\n'; ``` #### Basic operations on `array`: ```cpp -cout << sizeof(intarray)/sizeof(intarray[0]); // Returns the length of the array i.e. 10. -cout << sizeof(intarray[0]); // Returns length in bytes of one array item i.e. 4 as it is an integer +std::cout << sizeof(intarray)/sizeof(intarray[0]) << '\n'; // Returns the length of the array i.e. 10. +std::cout << sizeof(intarray[0]) << '\n'; // Returns length in bytes of one array item i.e. 4 as it is an integer ``` -:rocket: [Run Code](https://repl.it/CWZE) +:rocket: [Run Code](https://repl.it/CWZE/1) diff --git a/Data-Structure-Linked-Lists.md b/Data-Structure-Linked-Lists.md new file mode 100644 index 000000000..387a15460 --- /dev/null +++ b/Data-Structure-Linked-Lists.md @@ -0,0 +1,412 @@ +# Linked list + + Just like a garland is made with flowers, a linked list is made up of nodes. We call every flower on this particular garland to be a node. And each of the node points to the next node in this list as well as it has data (here it is type of flower). + +**Types:** + +1. Singly Linked List + + Singly linked lists contain nodes which have a `data` field as well as a `next` field, which points to the next node in the sequence. Operations that can be performed on singly linked lists are insertion, deletion and traversal. + + ``` + Singly Link List + -------------- + + head + | + | + +-----+--+ +-----+--+ +-----+------+ + | 1 |o-----> | 2 |o-----> | 3 | NULL | + +-----+--+ +-----+--+ +-----+------+ + + ``` + +2. Doubly Linked List + + Doubly linked lists contain node which have `data` field, `next` field and another link field `prev` pointing to the previous node in the sequence. + + ``` + Doubly Linked List + ---------------- + + head + | + | + +------+-----+--+ +--+-----+--+ +-----+------+ + | | |o------> | |o------> | | | + | NULL | 1 | | 2 | | 3 | NULL | + | | | <------o| | <------o| | | + +------+-----+--+ +--+-----+--+ +-----+------+ + + ``` + +3. Circular Linked List + + Circular linked lists is a singly linked list in which last node, `next` field points to first node in the sequence. + + ``` + Circular Linked List + ------------------ + + head + | + | + +-----+--+ +-----+--+ +-----+--+ + --> | 1 |o-----> | 2 |o-----> | 3 |o---- + | +-----+--+ +-----+--+ +-----+--+ | + | | + ------------------------------------------------ + ``` + +**Basic Operations** + +1. Insertion + + To add a new element to the list. + + ``` + Insertion at the beginning + ------------------------ + + * Create a new node with given data. + * Point new node's `next` to old `head`. + * Point `head` to this new node. + + Insertion in the middle/end + -------------------------- + Insertion after node X. + + * Create a new node with given data. + * Point new node's `next` to old X's `next`. + * Point X's `next` to this new node. + ``` + Time Complexity: O(1) + +2. Deletion + + To delete existing element from the list. + + ``` + Deletion at the beginning + ----------------------- + + * Get the node pointed by `head` as Temp. + * Point `head` to Temp's `next`. + * Free memory used by Temp node. + + Deletion in the middle/end + ------------------------- + Deletion after node X. + + * Get the node pointed by `X` as Temp. + * Point X's `next` to Temp's `next`. + * Free memory used by Temp node. + ``` + Time Complexity: O(1) + +3. Traversing + + To travel acroos the list. + + ``` + Traversal + -------- + + * Get the node pointed by `head` as Current. + * Check if Current is not null and display it. + * Point Current to Current's `next` and move to above step. + ``` + Time Complexity: O(n) // Here n is size of link-list + +**Implementation:** + +* C implementation of singly linked list + +```c + + // Header files + #include + #include + + struct node + { + int data; + struct node *next; + }; + + // Head pointer always points to first element of the linked list + struct node *head = NULL; + + // Display the list + void printList() + { + struct node *ptr = head; + + // Start from the beginning + while(ptr != NULL) + { + printf("%d ",ptr->data); + ptr = ptr->next; + } + + printf("\n"); + } + + // Insert link at the beginning + void insertFirst(int data) + { + // Create a new node + struct node *new_node = (struct node*)malloc(sizeof(struct node)); + + new_node->data = data; + + // Point it to old head + new_node->next = head; + + // Point head to new node + head = new_node; + + printf("Inserted successfully\n"); + } + + // Delete first item + void deleteFirst() + { + // Save reference to head + struct node *temp = head; + + // Point head to head's next + head = head->next; + + // Free memory used by temp + free(temp); + + printf("Deleted successfully\n"); + } + + // Find no. of nodes in link list + void size() + { + int length = 0; + struct node *current; + + for(current = head; current != NULL; current = current->next) + { + length++; + } + + printf("Size of Linked List is %d\n", length); + } + + // Find node with given data + void find(int data){ + + // Start from the head + struct node* current = head; + + // If list is empty + if(head == NULL) + { + printf("List is empty\n"); + return; + } + + // Traverse through list + while(current->data != data){ + + // If it is last node + if(current->next == NULL){ + printf("Not Found\n"); + return; + } + else{ + // Go to next node + current = current->next; + } + } + + // If data found + printf("Found\n"); + } + + // Delete a node with given data + void delete(int data){ + + // Start from the first node + struct node* current = head; + struct node* previous = NULL; + + // If list is empty + if(head == NULL){ + printf("List is empty\n"); + return ; + } + + // Navigate through list + while(current->data != data){ + + // If it is last node + if(current->next == NULL){ + printf("Element not found\n"); + return ; + } + else { + // Store reference to current node + previous = current; + // Move to next node + current = current->next; + } + + } + + // Found a match, update the node + if(current == head) { + // Change head to point to next node + head = head->next; + } + else { + // Skip the current node + previous->next = current->next; + } + + // Free space used by deleted node + free(current); + printf("Deleted succesfully\n"); + } + + int main() { + insertFirst(10); // prints Inserted successfully + insertFirst(20); // prints Inserted successfully + insertFirst(30); // prints Inserted successfully + insertFirst(1); // prints Inserted successfully + insertFirst(40); // prints Inserted successfully + insertFirst(56); // prints Inserted successfully + + // print list + printList(); // prints 56 40 1 30 20 10 + + deleteFirst(); // prints Deleted successfully + + printList(); // prints 40 1 30 20 10 + + find(4); // prints Not Found + find(1); // prints Found + + delete(4); // prints Element not found + delete(1); // prints Deleted succesfully + + printList(); // prints 40 30 20 10 + + return 0; + } +``` +:rocket: [Run Code](https://repl.it/CVwG) + +* Python Implementation of Singly Linked List + +```python + +class Node(object): + # Constructor + def __init__(self, data=None, next=None): + self.data = data + self.next = next + + # Function to get data + def get_data(self): + return self.data + + # Function to get next node + def get_next(self): + return self.next + + # Function to set next field + def set_next(self, new_next): + self.next = new_next + + +class LinkedList(object): + def __init__(self, head=None): + self.head = head + + # Function to insert data + def insert(self, data): + # new_node is a object of class Node + new_node = Node(data) + new_node.set_next(self.head) + self.head = new_node + print("Node with data " + str(data) + " is created succesfully") + + # Function to get size + def size(self): + current = self.head + count = 0 + while current: + count += 1 + current = current.get_next() + print("Size of link list is " + str(count)) + + # Function to search a data + def search(self, data): + current = self.head + found = False + while current and found is False: + if current.get_data() == data: + found = True + else: + current = current.get_next() + if current is None: + print("Node with data " + str(data) + " is not present") + else: + print("Node with data " + str(data) + " is found") + + + # Function to delete a node with data + def delete(self, data): + current = self.head + previous = None + found = False + while current and found is False: + if current.get_data() == data: + found = True + else: + previous = current + current = current.get_next() + if current is None: + print("Node with data " + str(data) + " is not in list") + elif previous is None: + self.head = current.get_next() + print("Node with data " + str(data) + " is deleted successfully") + else: + previous.set_next(current.get_next()) + print("Node with data " + str(data) + " is deleted successfully") + +SLL = LinkedList() # Creates an object of class LinkedList +SLL.size() # prints 'Size of link list is 0' +data_elements = [5, 10, 2, 6, 8, 20] +# prints the entire list +for element in data_elements: + SLL.insert(element) +SLL.size() # prints 'Size of link list is 6' +SLL.search(4) # prints 'Node with data 4 is not present' +SLL.search(5) # prints 'Node with data 5 is found' +SLL.delete(4) # prints 'Node with data 4 is not in list' +SLL.delete(5) # prints 'Node with data 5 is deleted successfully' +``` +:rocket: [Run Code](https://repl.it/CVq3/2) + + +**Advantages** + +1. Linked lists are a dynamic data structure, which can grow and shrink, allocating and deallocating memory while the program is running. +2. Insertion and deletion of node are easily implemented in a linked list at any position. + +**Disadvantages** + +1. They use more memory than arrays because of the memory used by their pointers (`next` and `prev`). +2. Random access is not possible in linked list. We have to access nodes sequentially. +3. It's more complex than array. If a language supports array bound check automatically, Arrays would serve you better. + +#### Note + +We have to use free() in c and cpp to free the space used by deleted node, whereas, in python java free space is collected automatically. From a85f6203a74b5b0b1566edb17b27322c2aa02af3 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Sun, 29 May 2016 20:00:47 +0530 Subject: [PATCH 3/6] added insertion-sort.md --- insertion-sort.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 insertion-sort.md diff --git a/insertion-sort.md b/insertion-sort.md new file mode 100644 index 000000000..e0b3e8822 --- /dev/null +++ b/insertion-sort.md @@ -0,0 +1,100 @@ +# Insertion Sort + +Insertion sort is a comparision based sorting. In this sorting technique, we maintain a sub-list which is always sorted and then we take one element from the list and insert it at its correct place. We does so till all elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right,we keep on inserting the card at it's right place till end. + +## Example + +![alt tag](https://cloud.githubusercontent.com/assets/13117482/15633518/04ca4468-25cc-11e6-96af-feb395b456e0.png) + +In the above example, Grey Shaded sublist is always sorted and in each step we are inserting leftmost element of white sublist at it's correct position. Hence, we have sorted the complete list in this way. + +## Algorithm + +Loop for i=0 to N-1: +* Pick element array[i] and insert it into sorted sublist array[0...i-1] + +## Complexity + +``` +Space complexity: O(1) // Auxillary/temporary space is used. + +Time complexity: O(n*n) // Two nested for loops are used. +``` + +## Cpp Implementation + +```cpp +// Function to sort an array using insertion sort +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i-1; + + /* Move elements of arr[0..i-1], that are greater than key, + to one position ahead of their current position */ + while (j >= 0 && arr[j] > key) + { + arr[j+1] = arr[j]; + j = j-1; + } + arr[j+1] = key; // place element key at it's correct place + } +} + +int main() +{ + // array to be sorted + int arr[5] = {12, 11, 13, 5, 6}; + + // call the insertion sort + insertionSort(arr, 5); + + // prints sorted array i.e. 5 6 11 12 13 + for(int i=0; i<5; i++) + std::cout << arr[i] << " "; + return 0; +} +``` + +:rocket: [Run Code](https://repl.it/CWZq) + +## Python Implementation + +```python +# Function to perform insertion sort +def insertionSort(arr): + # Traverse through array + for i in range(1, len(arr)): + key = arr[i] + # Move elements of arr[0..i-1], that are greater than key, + # to one position ahead of their current position + j = i-1 + while j>=0 and key < arr[j] : + arr[j+1] = arr[j] + j -= 1 + arr[j+1] = key # place element key at it's correct place + +# array to be sorted +arr = [12, 11, 13, 5, 6] +# call the insertion sort +insertionSort(arr) +# prints sorted array i.e. 5 6 11 12 13 +for i in range(len(arr)): + print(arr[i],end = ' ') +``` + +:rocket: [Run Code](https://repl.it/CWZi) + +## Advantages + +1. Efficient for small set of data and data set that are almost sorted. +2. Simply implemented. +3. Mostly better than bubble sort and selection sort & generally used with merge sort. + +## Disadvantages + +1. It's less efficient on large set of data. +2. Less effecient than merge sort, heap sort and quick sort. From ec4bc64039dc2b517487fa5d6af539abbd5afd2f Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Mon, 30 May 2016 15:48:05 +0530 Subject: [PATCH 4/6] updated linked-list article implemented all suggestions Implemented suggestions Implemented suggestions Implemented suggestions --- Data-Structure-Arrays.md | 8 + Data-Structure-Linked-Lists.md | 341 ++++++++++++++++++++------------- Data-Structures-Trie.md | 12 ++ insertion-sort.md | 25 ++- 4 files changed, 243 insertions(+), 143 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 55fc1e02c..23ca5fa2e 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -66,15 +66,18 @@ C++ provides a data structure, `array`, which stores a fixed-size sequential col #### Declaration of `array` ```cpp + int intarray[10]; // Declares an array of integer type of size 10 with elements having random values. Index ranges from 0 to 9(i.e. size-1). int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all elements having value 0 // Choose one the two declarations and then move ahead. + ``` #### Adding elements to `array`: ```cpp + intarray[0] = 1; // Adds an integer value of 1 at index 0 intarray[1] = 0; // Adds an integer value of 0 at index 1 intarray[2] = -1; // Adds an integer value of -1 at index 2 @@ -83,24 +86,29 @@ intarray[3] = 1; // Again adds an integer value of 1 at index 3 intarray[4] = "dd"; // Would give a TypeError as the array is of integer type. // Resolve the above error and then move ahead. + ``` #### Printing an `array`: ```cpp + std::cout << intarray[0] << '\n'; // Returns 1 which is element at index of the array std::cout << intarray[11] << '\n'; // Would give a random value as there is no element at index 11 of array. // To print all the elements of the array for(int i = 0; i < n; i++) std::cout << intarray[i] << '\n'; + ``` #### Basic operations on `array`: ```cpp + std::cout << sizeof(intarray)/sizeof(intarray[0]) << '\n'; // Returns the length of the array i.e. 10. std::cout << sizeof(intarray[0]) << '\n'; // Returns length in bytes of one array item i.e. 4 as it is an integer + ``` :rocket: [Run Code](https://repl.it/CWZE/1) diff --git a/Data-Structure-Linked-Lists.md b/Data-Structure-Linked-Lists.md index 387a15460..60958557c 100644 --- a/Data-Structure-Linked-Lists.md +++ b/Data-Structure-Linked-Lists.md @@ -2,13 +2,14 @@ Just like a garland is made with flowers, a linked list is made up of nodes. We call every flower on this particular garland to be a node. And each of the node points to the next node in this list as well as it has data (here it is type of flower). -**Types:** +## Types 1. Singly Linked List Singly linked lists contain nodes which have a `data` field as well as a `next` field, which points to the next node in the sequence. Operations that can be performed on singly linked lists are insertion, deletion and traversal. ``` + Singly Link List -------------- @@ -21,11 +22,18 @@ ``` + Application + + Internal implementation of CPython, the frames and evaluated variables are kept on a stack. + + For this we need to iterate only forward aur get the head, therefore singly linked-list is used. + 2. Doubly Linked List Doubly linked lists contain node which have `data` field, `next` field and another link field `prev` pointing to the previous node in the sequence. ``` + Doubly Linked List ---------------- @@ -40,11 +48,16 @@ ``` + Application + + The browser cache which allows you to hit the BACK and FORWARD button. Here we need to maintain a doubly linked list, with `URLs` as data field, to allow access in both direction. To go to previous URL we will use `prev` field and to go to next page we will use `next` field. + 3. Circular Linked List Circular linked lists is a singly linked list in which last node, `next` field points to first node in the sequence. ``` + Circular Linked List ------------------ @@ -56,15 +69,25 @@ | +-----+--+ +-----+--+ +-----+--+ | | | ------------------------------------------------ + ``` -**Basic Operations** + Application + + Timesharing problem solved by the operating system. + + In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small portion of CPU time, one user at a time. The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user. + + For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time, i.e list is empty. + +## Basic Operations 1. Insertion To add a new element to the list. ``` + Insertion at the beginning ------------------------ @@ -79,6 +102,7 @@ * Create a new node with given data. * Point new node's `next` to old X's `next`. * Point X's `next` to this new node. + ``` Time Complexity: O(1) @@ -87,6 +111,7 @@ To delete existing element from the list. ``` + Deletion at the beginning ----------------------- @@ -101,6 +126,7 @@ * Get the node pointed by `X` as Temp. * Point X's `next` to Temp's `next`. * Free memory used by Temp node. + ``` Time Complexity: O(1) @@ -109,198 +135,238 @@ To travel acroos the list. ``` + Traversal -------- * Get the node pointed by `head` as Current. * Check if Current is not null and display it. * Point Current to Current's `next` and move to above step. + ``` Time Complexity: O(n) // Here n is size of link-list -**Implementation:** +## Implementation -* C implementation of singly linked list +### C++ implementation of singly linked list -```c +```cpp - // Header files - #include - #include +// Header files +#include - struct node - { - int data; - struct node *next; - }; +struct node +{ + int data; + struct node *next; +}; - // Head pointer always points to first element of the linked list - struct node *head = NULL; +// Head pointer always points to first element of the linked list +struct node *head = NULL; - // Display the list - void printList() - { - struct node *ptr = head; +``` - // Start from the beginning - while(ptr != NULL) - { - printf("%d ",ptr->data); - ptr = ptr->next; - } +### Printing data in each node - printf("\n"); - } +```cpp - // Insert link at the beginning - void insertFirst(int data) +// Display the list +void printList() +{ + struct node *ptr = head; + + // Start from the beginning + while(ptr != NULL) { - // Create a new node - struct node *new_node = (struct node*)malloc(sizeof(struct node)); + std::cout << ptr->data << " "; + ptr = ptr->next; + } - new_node->data = data; + std::cout << "\n"; +} - // Point it to old head - new_node->next = head; +``` - // Point head to new node - head = new_node; +### Insertion at the beginning - printf("Inserted successfully\n"); - } +```cpp - // Delete first item - void deleteFirst() - { - // Save reference to head - struct node *temp = head; +// Insert link at the beginning +void insertFirst(int data) +{ + // Create a new node + struct node *new_node = new struct node; - // Point head to head's next - head = head->next; + new_node->data = data; - // Free memory used by temp - free(temp); + // Point it to old head + new_node->next = head; - printf("Deleted successfully\n"); - } + // Point head to new node + head = new_node; - // Find no. of nodes in link list - void size() - { - int length = 0; - struct node *current; + std::cout << "Inserted successfully\n"; +} - for(current = head; current != NULL; current = current->next) - { - length++; - } +``` + +### Deletion at the beginning + +```cpp + +// Delete first item +void deleteFirst() +{ + // Save reference to head + struct node *temp = head; + + // Point head to head's next + head = head->next; - printf("Size of Linked List is %d\n", length); + // Free memory used by temp + delete temp; + + std::cout << "Deleted successfully\n"; +} + +``` + +### Size + +```cpp + +// Find no. of nodes in link list +void size() +{ + int length = 0; + struct node *current; + + for(current = head; current != NULL; current = current->next) + { + length++; } - // Find node with given data - void find(int data){ + std::cout << "Size of Linked List is " << length << "\n"; +} + +``` - // Start from the head - struct node* current = head; +### Searching - // If list is empty - if(head == NULL) - { - printf("List is empty\n"); +```cpp + +// Find node with given data +void find(int data){ + + // Start from the head + struct node* current = head; + + // If list is empty + if(head == NULL) + { + std::cout << "List is empty\n"; + return; + } + + // Traverse through list + while(current->data != data){ + + // If it is last node + if(current->next == NULL){ + std::cout << "Not Found\n"; return; } - - // Traverse through list - while(current->data != data){ - - // If it is last node - if(current->next == NULL){ - printf("Not Found\n"); - return; - } - else{ - // Go to next node - current = current->next; - } + else{ + // Go to next node + current = current->next; } - - // If data found - printf("Found\n"); } - // Delete a node with given data - void delete(int data){ + // If data found + std::cout << "Found\n"; +} - // Start from the first node - struct node* current = head; - struct node* previous = NULL; +``` - // If list is empty - if(head == NULL){ - printf("List is empty\n"); - return ; - } +### Deletion after a node - // Navigate through list - while(current->data != data){ - - // If it is last node - if(current->next == NULL){ - printf("Element not found\n"); - return ; - } - else { - // Store reference to current node - previous = current; - // Move to next node - current = current->next; - } +```cpp - } +// Delete a node with given data +void del(int data){ + + // Start from the first node + struct node* current = head; + struct node* previous = NULL; - // Found a match, update the node - if(current == head) { - // Change head to point to next node - head = head->next; + // If list is empty + if(head == NULL){ + std::cout << "List is empty\n"; + return ; + } + + // Navigate through list + while(current->data != data){ + + // If it is last node + if(current->next == NULL){ + std::cout << "Element not found\n"; + return ; } else { - // Skip the current node - previous->next = current->next; + // Store reference to current node + previous = current; + // Move to next node + current = current->next; } - // Free space used by deleted node - free(current); - printf("Deleted succesfully\n"); } - int main() { - insertFirst(10); // prints Inserted successfully - insertFirst(20); // prints Inserted successfully - insertFirst(30); // prints Inserted successfully - insertFirst(1); // prints Inserted successfully - insertFirst(40); // prints Inserted successfully - insertFirst(56); // prints Inserted successfully + // Found a match, update the node + if(current == head) { + // Change head to point to next node + head = head->next; + } + else { + // Skip the current node + previous->next = current->next; + } - // print list - printList(); // prints 56 40 1 30 20 10 + // Free space used by deleted node + delete current; + std::cout << "Deleted succesfully\n"; +} - deleteFirst(); // prints Deleted successfully +``` - printList(); // prints 40 1 30 20 10 +int main() { + insertFirst(10); // prints Inserted successfully + insertFirst(20); // prints Inserted successfully + insertFirst(30); // prints Inserted successfully + insertFirst(1); // prints Inserted successfully + insertFirst(40); // prints Inserted successfully + insertFirst(56); // prints Inserted successfully - find(4); // prints Not Found - find(1); // prints Found + // print list + printList(); // prints 56 40 1 30 20 10 - delete(4); // prints Element not found - delete(1); // prints Deleted succesfully + deleteFirst(); // prints Deleted successfully - printList(); // prints 40 30 20 10 + printList(); // prints 40 1 30 20 10 + + find(4); // prints Not Found + find(1); // prints Found + + del(4); // prints Element not found + del(1); // prints Deleted succesfully + + printList(); // prints 40 30 20 10 + + return 0; +} - return 0; - } ``` -:rocket: [Run Code](https://repl.it/CVwG) +:rocket: [Run Code](https://repl.it/CXVt) * Python Implementation of Singly Linked List @@ -392,6 +458,7 @@ SLL.search(4) # prints 'Node with data 4 is not present' SLL.search(5) # prints 'Node with data 5 is found' SLL.delete(4) # prints 'Node with data 4 is not in list' SLL.delete(5) # prints 'Node with data 5 is deleted successfully' + ``` :rocket: [Run Code](https://repl.it/CVq3/2) @@ -409,4 +476,4 @@ SLL.delete(5) # prints 'Node with data 5 is deleted successfully' #### Note -We have to use free() in c and cpp to free the space used by deleted node, whereas, in python java free space is collected automatically. +We have to use free() in C and delete in C++ to free the space used by deleted node, whereas, in Python and Java free space is collected automatically by garbage collector. diff --git a/Data-Structures-Trie.md b/Data-Structures-Trie.md index e9c133349..2d65e561e 100644 --- a/Data-Structures-Trie.md +++ b/Data-Structures-Trie.md @@ -21,9 +21,12 @@ This is an image of a Trie, which stores the words {assoc, algo, all, also, tree ## How to implement a trie? +### Python Implementation + Let's implement a trie in python, for storing words with their meanings from english dictionary. ```python + ALPHABET_SIZE = 26 # For English class TrieNode: @@ -31,6 +34,7 @@ class TrieNode: self.edges = [None]*(ALPHABET_SIZE) # Each index respective to each character. self.meaning = None # Meaning of the word. self.ends_here = False # Tells us if the word ends here. + ``` As you can see, edges are 26 in length, each index referring to each character in the alphabet. 'A' corresponding to 0, 'B' to 1, 'C' to 2 ... 'Z' to 25th index. If the character you are looking for is pointing to `None`, that implies the word is not there in the trie. @@ -48,6 +52,7 @@ Additionally, one can also add something like #### Adding Word to the trie ```python + def add_word(self,word,meaning): if len(word)==0: self.ends_here = True # Because we have reached the end of the word @@ -68,6 +73,7 @@ Additionally, one can also add something like #### Retrieving data ```python + def search_word(self,word): if len(word)==0: if self.ends_here: @@ -86,6 +92,7 @@ Additionally, one can also add something like The `search_word` function will tell us if the word exists in the Trie or not. Since ours is a dictionary, we need to fetch the meaning as well, now lets declare a function to do that. ```python + def get_meaning(self,word): if len(word)==0 : if self.ends_here: @@ -98,6 +105,7 @@ The `search_word` function will tell us if the word exists in the Trie or not. S return "Word doesn't exist in the Trie" else: return self.edges[index].get_meaning(word[1:]) + ``` #### Deleting data @@ -105,6 +113,7 @@ The `search_word` function will tell us if the word exists in the Trie or not. S By deleting data, you just need to change the variable `ends_here` to `False`. Doing that doesn't alter the prefixes, but stills deletes the meaning and the existence of the word from the trie. ```python + def delete_word(self,word): if len(word)==0: if self.ends_here: @@ -119,10 +128,13 @@ By deleting data, you just need to change the variable `ends_here` to `False`. D return "Word doesn't exist in the Trie" else: return self.edges[index].delete_word(word[1:]) + ``` :rocket: [Run Code](https://repl.it/CWbr) +### C++ Implementation + ## Resources - For further reading, you can try this [topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries/) tutorial. diff --git a/insertion-sort.md b/insertion-sort.md index e0b3e8822..885fa1e01 100644 --- a/insertion-sort.md +++ b/insertion-sort.md @@ -1,29 +1,40 @@ # Insertion Sort -Insertion sort is a comparision based sorting. In this sorting technique, we maintain a sub-list which is always sorted and then we take one element from the list and insert it at its correct place. We does so till all elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right,we keep on inserting the card at it's right place till end. +Insertion sort is a [comparision based sorting](https://en.wikipedia.org/wiki/Comparison_sort). A sorting algorithm is comparison based, if it uses comparison operators (such as `less than` and `greated than`) to find the order between two numbers. + +In this sorting technique, we always maintain a sorted sublist in lower position of list and then we take one element from the rest of list and insert it at it's correct place. We does so till all elements are inserted into sublist. For example, while playing cards we sort cards in our hand. Starting from left and moving to right, we keep on inserting the card at it's right place till end. ## Example -![alt tag](https://cloud.githubusercontent.com/assets/13117482/15633518/04ca4468-25cc-11e6-96af-feb395b456e0.png) +![Insertion Sort](https://cloud.githubusercontent.com/assets/13117482/15633518/04ca4468-25cc-11e6-96af-feb395b456e0.png) + +In the above example, `grey shaded` sublist is always sorted. Please note that in the beginning, sublist contains ony one element, and *trivially* sorted. Then at each step we are inserting leftmost element of `white shaded` sublist at it's correct position. -In the above example, Grey Shaded sublist is always sorted and in each step we are inserting leftmost element of white sublist at it's correct position. Hence, we have sorted the complete list in this way. +Hence, we have sorted the complete list in this way. ## Algorithm +``` + Loop for i=0 to N-1: * Pick element array[i] and insert it into sorted sublist array[0...i-1] +``` + ## Complexity ``` + Space complexity: O(1) // Auxillary/temporary space is used. Time complexity: O(n*n) // Two nested for loops are used. + ``` -## Cpp Implementation +## C++ Implementation ```cpp + // Function to sort an array using insertion sort void insertionSort(int arr[], int n) { @@ -57,6 +68,7 @@ int main() std::cout << arr[i] << " "; return 0; } + ``` :rocket: [Run Code](https://repl.it/CWZq) @@ -64,6 +76,7 @@ int main() ## Python Implementation ```python + # Function to perform insertion sort def insertionSort(arr): # Traverse through array @@ -84,6 +97,7 @@ insertionSort(arr) # prints sorted array i.e. 5 6 11 12 13 for i in range(len(arr)): print(arr[i],end = ' ') + ``` :rocket: [Run Code](https://repl.it/CWZi) @@ -96,5 +110,4 @@ for i in range(len(arr)): ## Disadvantages -1. It's less efficient on large set of data. -2. Less effecient than merge sort, heap sort and quick sort. +1. It's less efficient on large set of data than merge sort, heap sort and quick sort. From 7d3b63e51957c3cc80ccc6f73a6aed012ea11cd4 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 16:48:25 +0530 Subject: [PATCH 5/6] Implemented suggestions C to C++ in Linked List C to C++ in Linked List Update Update Update --- Data-Structure-Arrays.md | 120 +++++++++++++++++---------------- Data-Structure-Linked-Lists.md | 101 +++++++++++++-------------- 2 files changed, 108 insertions(+), 113 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 23ca5fa2e..3969a832e 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -4,111 +4,117 @@ Internally, `array` is a kind of data structure that can store a fixed-size sequ `array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. -## Arrays in Python -Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` -and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. +## Arrays in C++ + +C++ provides a data structure, `array`, which stores a fixed-size sequential collection of elements of the same data-type. An `array` is used to store a collection of data, but it is better to think of an `array` as a collection of variables of the same type. #### Declaration of `array` -```python -from array import array -intarray = array('i') # Declares an array of integer type +```cpp + +int intarray[10]; // Declares an array of integer type of size 10 with elements having random values. Index ranges from 0 to 9(i.e. size-1). +int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all elements having value 0 + +// Choose one the two declarations and then move ahead. + ``` -#### Adding elements to `array`: +#### Inserting elements to `array`: -```python -intarray.append(1) # Adds an integer value of 1 to the array -intarray.append(0) # Adds an integer value of 0 to the array -intarray.append(-1) # Adds an integer value of -1 to the array -intarray.append(1) # Again adds an integer value of 1 to the array +```cpp -intarray.append('d') # Would give a TypeError as the array is of integer type. +intarray[0] = 1; // Inserts an integer value of 1 at index 0 +intarray[1] = 0; // Inserts an integer value of 0 at index 1 +intarray[2] = -1; // Inserts an integer value of -1 at index 2 +intarray[3] = 1; // Inserts an integer value of 1 at index 3 -#Resolve the above error and then move ahead. ``` #### Printing an `array`: -```python -print(intarray) # Returns array('i', [1, 4, -1]) -print(intarray[0]) # Returns 1 which is the element at index 0 of the array -print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. +```cpp -#Resolve the above error and then move ahead. +std::cout << intarray[0] << std::endl; // Returns 1 which is element at index of the array +std::cout << intarray[11] << std::endl; // A random number is expected, while in reality this is `dangerous`, and is primary cause of crashes as it's accessing a memory location which does not exist. + +// To print all the elements of the array +for(int i = 0; i < n; i++) + std::cout << intarray[i] << std::endl; -# To print all the elements of the array -for i in intarray: - print(i) ``` #### Basic operations on `array`: -```python -len(intarray) # Returns the length of the array i.e. 3 -intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer -intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 -intarray.insert(1, 3) # Insert a new item with value x in the array before position i -intarray.remove(1) # Remove the first occurrence of 1 from the array -intarray.reverse() # Reverse the order of the items in the array -intarray.pop(1) # Removes the item with the index 1 from the array and returns it +```cpp + +std::cout << sizeof(intarray)/sizeof(intarray[0]) << std::endl; // Returns the length of the array i.e. 10. +std::cout << sizeof(intarray[0]) << std::endl; // Returns length in bytes of one array item i.e. 4 as it is an integer + ``` -:rocket: [Run Code](https://repl.it/CWJB) +:rocket: [Run Code](https://repl.it/CWZE/3) -[Official Docs](https://docs.python.org/3.5/library/array.html) -## Arrays in C++ +## Arrays in Python -C++ provides a data structure, `array`, which stores a fixed-size sequential collection of elements of the same data-type. An `array` is used to store a collection of data, but it is better to think of an `array` as a collection of variables of the same type. +Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` +and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type. #### Declaration of `array` -```cpp - -int intarray[10]; // Declares an array of integer type of size 10 with elements having random values. Index ranges from 0 to 9(i.e. size-1). -int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all elements having value 0 +```python -// Choose one the two declarations and then move ahead. +from array import array +intarray = array('i') # Declares an array of integer type ``` -#### Adding elements to `array`: +#### Inserting elements to `array`: -```cpp +```python -intarray[0] = 1; // Adds an integer value of 1 at index 0 -intarray[1] = 0; // Adds an integer value of 0 at index 1 -intarray[2] = -1; // Adds an integer value of -1 at index 2 -intarray[3] = 1; // Again adds an integer value of 1 at index 3 +intarray.append(1) # Inserts an integer value of 1 to the array +intarray.append(0) # Inserts an integer value of 0 to the array +intarray.append(-1) # Inserts an integer value of -1 to the array +intarray.append(1) # Inserts an integer value of 1 to the array -intarray[4] = "dd"; // Would give a TypeError as the array is of integer type. +intarray.append('d') # Would give a TypeError as the array is of integer type. -// Resolve the above error and then move ahead. +#Resolve the above error and then move ahead. ``` #### Printing an `array`: -```cpp +```python -std::cout << intarray[0] << '\n'; // Returns 1 which is element at index of the array -std::cout << intarray[11] << '\n'; // Would give a random value as there is no element at index 11 of array. +print(intarray) # Returns array('i', [1, 4, -1]) +print(intarray[0]) # Returns 1 which is the element at index 0 of the array +print(intarray[3]) # Would give IndexError as there is no element at index 3 of array. -// To print all the elements of the array -for(int i = 0; i < n; i++) - std::cout << intarray[i] << '\n'; +#Resolve the above error and then move ahead. + +# To print all the elements of the array +for i in intarray: + print(i) ``` #### Basic operations on `array`: -```cpp +```python -std::cout << sizeof(intarray)/sizeof(intarray[0]) << '\n'; // Returns the length of the array i.e. 10. -std::cout << sizeof(intarray[0]) << '\n'; // Returns length in bytes of one array item i.e. 4 as it is an integer +len(intarray) # Returns the length of the array i.e. 3 +intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer +intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 +intarray.insert(1, 3) # Insert a new item with value x in the array before position i +intarray.remove(1) # Remove the first occurrence of 1 from the array +intarray.reverse() # Reverse the order of the items in the array +intarray.pop(1) # Removes the item with the index 1 from the array and returns it ``` -:rocket: [Run Code](https://repl.it/CWZE/1) +:rocket: [Run Code](https://repl.it/CWJB) + +[Official Docs](https://docs.python.org/3.5/library/array.html) diff --git a/Data-Structure-Linked-Lists.md b/Data-Structure-Linked-Lists.md index 60958557c..285c7916c 100644 --- a/Data-Structure-Linked-Lists.md +++ b/Data-Structure-Linked-Lists.md @@ -80,6 +80,7 @@ For this application, there should be no NULL pointers unless there is absolutely no one requesting CPU time, i.e list is empty. + ## Basic Operations 1. Insertion @@ -166,7 +167,7 @@ struct node *head = NULL; ``` -### Printing data in each node +#### Printing data in each node ```cpp @@ -182,12 +183,12 @@ void printList() ptr = ptr->next; } - std::cout << "\n"; + std::cout << std::endl; } ``` -### Insertion at the beginning +#### Insertion at the beginning ```cpp @@ -205,12 +206,12 @@ void insertFirst(int data) // Point head to new node head = new_node; - std::cout << "Inserted successfully\n"; + std::cout << "Inserted successfully" << std::endl; } ``` -### Deletion at the beginning +#### Deletion at the beginning ```cpp @@ -224,14 +225,15 @@ void deleteFirst() head = head->next; // Free memory used by temp + temp = NULL: delete temp; - std::cout << "Deleted successfully\n"; + std::cout << "Deleted successfully" << std::endl; } ``` -### Size +#### Size ```cpp @@ -246,12 +248,12 @@ void size() length++; } - std::cout << "Size of Linked List is " << length << "\n"; + std::cout << "Size of Linked List is " << length << std::endl; } ``` -### Searching +#### Searching ```cpp @@ -264,7 +266,7 @@ void find(int data){ // If list is empty if(head == NULL) { - std::cout << "List is empty\n"; + std::cout << "List is empty" << std::endl; return; } @@ -273,7 +275,7 @@ void find(int data){ // If it is last node if(current->next == NULL){ - std::cout << "Not Found\n"; + std::cout << "Not Found" << std::endl; return; } else{ @@ -283,12 +285,12 @@ void find(int data){ } // If data found - std::cout << "Found\n"; + std::cout << "Found" << std::endl; } ``` -### Deletion after a node +#### Deletion after a node ```cpp @@ -301,7 +303,7 @@ void del(int data){ // If list is empty if(head == NULL){ - std::cout << "List is empty\n"; + std::cout << "List is empty" << std::endl; return ; } @@ -310,7 +312,7 @@ void del(int data){ // If it is last node if(current->next == NULL){ - std::cout << "Element not found\n"; + std::cout << "Element not found" << std::endl; return ; } else { @@ -333,42 +335,17 @@ void del(int data){ } // Free space used by deleted node + current = NULL; delete current; - std::cout << "Deleted succesfully\n"; + std::cout << "Deleted succesfully" << std::endl; } ``` -int main() { - insertFirst(10); // prints Inserted successfully - insertFirst(20); // prints Inserted successfully - insertFirst(30); // prints Inserted successfully - insertFirst(1); // prints Inserted successfully - insertFirst(40); // prints Inserted successfully - insertFirst(56); // prints Inserted successfully - - // print list - printList(); // prints 56 40 1 30 20 10 - - deleteFirst(); // prints Deleted successfully - - printList(); // prints 40 1 30 20 10 - - find(4); // prints Not Found - find(1); // prints Found - - del(4); // prints Element not found - del(1); // prints Deleted succesfully - - printList(); // prints 40 30 20 10 - - return 0; -} +:rocket: [Run Code](https://repl.it/CXVt/1) -``` -:rocket: [Run Code](https://repl.it/CXVt) -* Python Implementation of Singly Linked List +### Python Implementation of Singly Linked List ```python @@ -395,6 +372,12 @@ class LinkedList(object): def __init__(self, head=None): self.head = head +``` + +#### Insertion + +```python + # Function to insert data def insert(self, data): # new_node is a object of class Node @@ -403,6 +386,12 @@ class LinkedList(object): self.head = new_node print("Node with data " + str(data) + " is created succesfully") +``` + +#### Size + +```python + # Function to get size def size(self): current = self.head @@ -412,6 +401,12 @@ class LinkedList(object): current = current.get_next() print("Size of link list is " + str(count)) +``` + +#### Searching + +```python + # Function to search a data def search(self, data): current = self.head @@ -426,6 +421,11 @@ class LinkedList(object): else: print("Node with data " + str(data) + " is found") +``` + +#### Deletion after a node + +```python # Function to delete a node with data def delete(self, data): @@ -447,19 +447,8 @@ class LinkedList(object): previous.set_next(current.get_next()) print("Node with data " + str(data) + " is deleted successfully") -SLL = LinkedList() # Creates an object of class LinkedList -SLL.size() # prints 'Size of link list is 0' -data_elements = [5, 10, 2, 6, 8, 20] -# prints the entire list -for element in data_elements: - SLL.insert(element) -SLL.size() # prints 'Size of link list is 6' -SLL.search(4) # prints 'Node with data 4 is not present' -SLL.search(5) # prints 'Node with data 5 is found' -SLL.delete(4) # prints 'Node with data 4 is not in list' -SLL.delete(5) # prints 'Node with data 5 is deleted successfully' - ``` + :rocket: [Run Code](https://repl.it/CVq3/2) From a320741e974afd589f2ea3149e40a74f6dbee0fa Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 17:36:49 +0530 Subject: [PATCH 6/6] Update --- Data-Structures-Trie.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/Data-Structures-Trie.md b/Data-Structures-Trie.md index 2d65e561e..1ba440676 100644 --- a/Data-Structures-Trie.md +++ b/Data-Structures-Trie.md @@ -133,8 +133,6 @@ By deleting data, you just need to change the variable `ends_here` to `False`. D :rocket: [Run Code](https://repl.it/CWbr) -### C++ Implementation - ## Resources - For further reading, you can try this [topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries/) tutorial.