Skip to content

Commit 21b8a8a

Browse files
committed
Added linked list implementation
1 parent 03ce397 commit 21b8a8a

1 file changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 5th August July 2017
3+
4+
# Linked List and Node can be accomodated in separate classes for convenience
5+
6+
class Node(object):
7+
# Each node has its data and a pointer that points to next node in the Linked List
8+
def __init__(self, data, next = None):
9+
self.data = data;
10+
self.next = next;
11+
12+
# function to set data
13+
def setData(self, data):
14+
self.data = data;
15+
16+
# function to get data of a particular node
17+
def getData(self):
18+
return self.data
19+
20+
# function to set next node
21+
def setNext(self, next):
22+
self.next = next
23+
24+
# function to get the next node
25+
def getNext(self):
26+
return self.next
27+
28+
# easily retrieve the source code of the Node class
29+
def get_code(self):
30+
import inspect
31+
return inspect.getsource(Node)
32+
33+
class SinglyLinkedList(object):
34+
# Defining the head of the linked list
35+
def __init__(self):
36+
self.head = None
37+
38+
# printing the data in the linked list
39+
def get_data(self):
40+
temp = self.head
41+
List = []
42+
while(temp):
43+
# print(temp.data, end=' ')
44+
List.append(temp.data)
45+
temp = temp.next
46+
47+
return List
48+
49+
# inserting the node at the beginning
50+
def insert_at_start(self, data):
51+
if self.head == None:
52+
newNode = Node(data)
53+
self.head = newNode
54+
else:
55+
newNode = Node(data)
56+
newNode.next = self.head
57+
self.head = newNode
58+
59+
# inserting the node in between the linked list (after a specific node)
60+
def insert_between(self, next_node_data, data):
61+
# if (previousNode.next is None):
62+
# print('Previous node should have next node!')
63+
# else:
64+
newNode = Node(data)
65+
currentNode = self.search(self.head, next_node_data)
66+
newNode.next = currentNode.next
67+
currentNode.next = newNode
68+
69+
# inserting at the end of linked list
70+
def insert_at_end(self, data):
71+
newNode = Node(data)
72+
temp = self.head
73+
while(temp.next != None): # get last node
74+
temp = temp.next
75+
temp.next = newNode
76+
77+
# deleting an item based on data(or key)
78+
def delete(self, data):
79+
temp = self.head
80+
# if data/key is found in head node itself
81+
if (temp.next is not None):
82+
if(temp.data == data):
83+
self.head = temp.next
84+
temp = None
85+
return
86+
else:
87+
# else search all the nodes
88+
while(temp.next != None):
89+
if(temp.data == data):
90+
break
91+
prev = temp #save current node as previous so that we can go on to next node
92+
temp = temp.next
93+
94+
# node not found
95+
if temp == None:
96+
return
97+
98+
prev.next = temp.next
99+
return
100+
101+
# iterative search
102+
def search(self, node, data):
103+
if node == None:
104+
return False
105+
if node.data == data:
106+
return node
107+
return self.search(node.getNext(), data)
108+
109+
# easily retrieve the source code of the SinglyLinkedList class
110+
def get_code(self):
111+
import inspect
112+
return inspect.getsource(SinglyLinkedList)
113+
114+
class DoublyLinkedList(object):
115+
def __init__(self):
116+
self.head = None
117+
118+
# printing the data in the linked list
119+
def get_data(self):
120+
temp = self.head
121+
List = []
122+
while(temp):
123+
# print(temp.data, end=' ')
124+
List.append(temp.data)
125+
temp = temp.next
126+
127+
return List
128+
129+
# for inserting at beginning of linked list
130+
def insert_at_start(self, data):
131+
if self.head == None:
132+
newNode = Node(data)
133+
self.head = newNode
134+
else:
135+
newNode = Node(data)
136+
self.head.previous = newNode
137+
newNode.next = self.head
138+
self.head = newNode
139+
140+
# for inserting at end of linked list
141+
def insert_at_end(self, data):
142+
newNode = Node(data)
143+
temp = self.head
144+
while(temp.next != None):
145+
temp = temp.next
146+
temp.next = newNode
147+
newNode.previous = temp
148+
149+
# deleting a node from linked list
150+
def delete(self, data):
151+
temp = self.head
152+
if(temp.next != None):
153+
# if head node is to be deleted
154+
if(temp.data == data):
155+
temp.next.previous = None
156+
self.head = temp.next
157+
temp.next = None
158+
return
159+
else:
160+
while(temp.next != None):
161+
if(temp.data == data):
162+
break
163+
temp = temp.next
164+
if(temp.next):
165+
# if element to be deleted is in between
166+
temp.previous.next = temp.next
167+
temp.next.previous = temp.previous
168+
temp.next = None
169+
temp.previous = None
170+
else:
171+
# if element to be deleted is the last element
172+
temp.previous.next = None
173+
temp.previous = None
174+
return
175+
176+
if (temp == None):
177+
return
178+
179+
# easily retrieve the source code of the DoublyLinkedList class
180+
def get_code(self):
181+
import inspect
182+
return inspect.getsource(DoublyLinkedList)

0 commit comments

Comments
 (0)