Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
Implemented suggestions
Browse files Browse the repository at this point in the history
C to C++ in Linked List

C to C++ in Linked List
  • Loading branch information
jainaman224 committed Jun 1, 2016
1 parent ec4bc64 commit c7ac089
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 56 deletions.
28 changes: 18 additions & 10 deletions Data-Structure-Arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,18 +46,21 @@ 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
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/CWJB)
Expand All @@ -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.
Expand Down
78 changes: 32 additions & 46 deletions Data-Structure-Linked-Lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct node *head = NULL;

```
### Printing data in each node
#### Printing data in each node
```cpp
Expand All @@ -187,7 +187,7 @@ void printList()
```

### Insertion at the beginning
#### Insertion at the beginning

```cpp

Expand All @@ -210,7 +210,7 @@ void insertFirst(int data)

```
### Deletion at the beginning
#### Deletion at the beginning
```cpp
Expand All @@ -231,7 +231,7 @@ void deleteFirst()
```

### Size
#### Size

```cpp

Expand All @@ -251,7 +251,7 @@ void size()

```

### Searching
#### Searching

```cpp

Expand Down Expand Up @@ -288,7 +288,7 @@ void find(int data){

```
### Deletion after a node
#### Deletion after a node
```cpp
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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)


Expand Down

0 comments on commit c7ac089

Please sign in to comment.