Skip to content

Commit 28168f5

Browse files
committed
Linked list code and README file added
1 parent c29672d commit 28168f5

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

Data Structure/Linked List/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Linked List
2+
3+
A **linked list** is a linear data structure where each element is a separate object.
4+
Each element (known as *node*) of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. The entry point into a linked list is called the head of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference.
5+
6+
![Linked List](linked-list.png)
7+
8+
A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects can use a linked list.
9+
10+
11+
**Basic Operations:**
12+
13+
Following are the basic operations supported by a list.
14+
15+
* **Insertion** − Adds an element at the beginning of the list.
16+
17+
* **Deletion** − Deletes an element at the beginning of the list.
18+
19+
* **Display** − Displays the complete list.
20+
21+
* **Search** − Searches an element using the given key.
22+
23+
* **Delete** − Deletes an element using the given key.
24+
25+
26+
#### Complexity Analysis
27+
- Search - O(n)
28+
- Insertion - O(1)
29+
- Deletion - O(1) (Deletion from beginning)
30+
- Deletion in middle - Searching O(n) and deleting O(1)
31+
- Space - O(n)
32+
33+
#### Array vs Linked List
34+
http://www.geeksforgeeks.org/linked-list-vs-array/
35+
36+
### More on this topic
37+
- https://en.wikipedia.org/wiki/Linked_list
38+
- https://www.hackerearth.com/practice/data-structures/linked-list/singly-linked-list/tutorial/
39+
- https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm
5.06 KB
Loading
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Node:
2+
def __init__(self, item, next):
3+
self.item = item
4+
self.next = next
5+
6+
class LinkedList:
7+
def __init__(self):
8+
self.head = None
9+
10+
# Add an item to the head of the linked list
11+
def add(self, item):
12+
self.head = Node(item, self.head)
13+
14+
# Delete the first item of the linked list
15+
def remove(self):
16+
if self.is_empty():
17+
return None
18+
else:
19+
item = self.head.item
20+
self.head = self.head.next
21+
return item
22+
23+
# Find and delete an given item
24+
def delete(self, item):
25+
if not self.is_empty() and self.head.item == item:
26+
self.remove()
27+
return True
28+
else:
29+
current = self.head
30+
prev = None
31+
while current != None:
32+
if current.item == item:
33+
prev.next = current.next
34+
return True
35+
prev = current
36+
current = current.next
37+
return False
38+
39+
# Check if the linked list is empty
40+
def is_empty(self):
41+
return self.head == None
42+
43+
# Search for an item in the list
44+
def find(self, item):
45+
node = self.head
46+
while node != None:
47+
if node.item == item:
48+
return True
49+
node = node.next
50+
51+
return False
52+
53+
# Print all the items in the list
54+
def show_all_items(self):
55+
print("Items in the list:")
56+
node = self.head
57+
while node != None:
58+
print(node.item)
59+
node = node.next

Data Structure/Linked List/test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from linked_list import LinkedList
2+
3+
new_list = LinkedList()
4+
5+
new_list.add(10)
6+
new_list.show_all_items()
7+
new_list.add(15)
8+
new_list.show_all_items()
9+
new_list.add(5)
10+
new_list.show_all_items()
11+
new_list.remove()
12+
new_list.show_all_items()
13+
new_list.add(2)
14+
new_list.show_all_items()
15+
print(new_list.find(50)) # Testing find with a invalid item
16+
print(new_list.find(10)) # Testing find with a valid item
17+
print(new_list.delete(15)) # Testing delete with a valid item
18+
print(new_list.delete(100)) # Testing delete with a invalid item

0 commit comments

Comments
 (0)