From c7ac089ff5a5efc59a333d5e1b51c11f71b72b96 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 16:48:25 +0530 Subject: [PATCH] Implemented suggestions C to C++ in Linked List C to C++ in Linked List --- Data-Structure-Arrays.md | 28 +++++++----- Data-Structure-Linked-Lists.md | 78 ++++++++++++++-------------------- 2 files changed, 50 insertions(+), 56 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 23ca5fa2e..c751daa2e 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -12,26 +12,31 @@ and an `array` in Python is that a `list` can have different types of values whe #### Declaration of `array` ```python + from array import array intarray = array('i') # Declares an array of integer type + ``` -#### 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 + +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.append('d') # Would give a TypeError as the array is of integer type. #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. @@ -41,11 +46,13 @@ print(intarray[3]) # Would give IndexError as there is no element at index 3 of # 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 @@ -53,6 +60,7 @@ intarray.insert(1, 3) # Insert a new item with value x in the array before posit 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/CWJB) @@ -74,14 +82,14 @@ int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all el ``` -#### Adding elements to `array`: +#### Inserting 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[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 intarray[4] = "dd"; // Would give a TypeError as the array is of integer type. diff --git a/Data-Structure-Linked-Lists.md b/Data-Structure-Linked-Lists.md index 60958557c..b09f281af 100644 --- a/Data-Structure-Linked-Lists.md +++ b/Data-Structure-Linked-Lists.md @@ -166,7 +166,7 @@ struct node *head = NULL; ``` -### Printing data in each node +#### Printing data in each node ```cpp @@ -187,7 +187,7 @@ void printList() ``` -### Insertion at the beginning +#### Insertion at the beginning ```cpp @@ -210,7 +210,7 @@ void insertFirst(int data) ``` -### Deletion at the beginning +#### Deletion at the beginning ```cpp @@ -231,7 +231,7 @@ void deleteFirst() ``` -### Size +#### Size ```cpp @@ -251,7 +251,7 @@ void size() ``` -### Searching +#### Searching ```cpp @@ -288,7 +288,7 @@ void find(int data){ ``` -### Deletion after a node +#### Deletion after a node ```cpp @@ -339,36 +339,10 @@ void del(int data){ ``` -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) -* Python Implementation of Singly Linked List + +### Python Implementation of Singly Linked List ```python @@ -395,6 +369,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 +383,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 +398,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 +418,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 +444,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)