Skip to content

Commit 6b0fd59

Browse files
Dwipam KatariyaDwipam Katariya
authored andcommitted
Single linked list
1 parent 1448fa8 commit 6b0fd59

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

practice/linked_list.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Node:
2+
def __init__(self, key):
3+
self.key = key
4+
self.next = None
5+
6+
class LinkedList:
7+
def __init__(self):
8+
self.head = None
9+
10+
def push(self, key):
11+
node = Node(key)
12+
node.next = self.head
13+
self.head = node
14+
15+
def pop(self):
16+
if self.head.next != None:
17+
temp = self.head
18+
self.head = self.head.next
19+
temp = None
20+
21+
def search(self, search_key):
22+
temp = self.head
23+
while temp != None:
24+
if temp.key == search_key:
25+
return "Found"
26+
temp = temp.next
27+
return "Not Found"
28+
29+
def traverse(self):
30+
temp = self.head
31+
while temp != None:
32+
print temp.key
33+
temp = temp.next
34+
35+
list_ = LinkedList()
36+
list_.push(2)
37+
list_.push(5)
38+
list_.push(8)
39+
list_.push(9)
40+
list_.push(1)
41+
list_.push(3)
42+
list_.push(6)
43+
list_.push(4)
44+
list_.push(7)
45+
print list_.search(10)
46+
print list_.search(2)
47+
list_.traverse()
48+
list_.pop()
49+
print "\n"
50+
list_.traverse()
51+
52+

0 commit comments

Comments
 (0)