-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
70 lines (62 loc) · 2.08 KB
/
Copy pathlinked_list.py
File metadata and controls
70 lines (62 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
self.temp=None
def insert_begin(self,data):
if self.head is None:
self.head=Node(data)
self.head.next=None
else:
self.new_node=Node(data)
self.new_node.next=self.head
self.head=self.new_node
def insert_end(self,data):
if self.head is None:
self.head=Node(data)
self.head.next=None
else:
self.new_node=Node(data)
self.temp=self.head
while(self.temp.next != None):
self.temp=self.temp.next
self.temp.next=self.new_node
self.new_node=None
def insert_pos(self,data,pos):
if self.head is None:
self.head=Node(data)
self.head.next=None
else:
self.new_node=Node(data)
self.temp=self.head
while(self.temp.data != pos):
self.temp=self.temp.next
self.new_node.next=self.temp.next
self.temp.next=self.new_node
def display(self):
if self.head is None:
print("Underflow!!")
else:
self.temp=self.head
while(self.temp != None):
print(str(self.temp.data)+" -> ",end=" ")
self.temp=self.temp.next
print("None")
l1=LinkedList()
print("For Insertion at Begin ")
l1.insert_begin(int(input("enter element : ")))
l1.insert_begin(int(input("enter element : ")))
l1.insert_begin(int(input("enter element : ")))
l1.display()
print("\nFor Insertion at End ")
l1.insert_end(int(input("enter element : ")))
l1.insert_end(int(input("enter element : ")))
l1.insert_end(int(input("enter element : ")))
l1.display()
print("\nFor Insertion at Specified Position ")
l1.insert_pos(int(input("enter element : ")),int(input("enter position : ")))
l1.insert_pos(int(input("enter element : ")),int(input("enter position : ")))
l1.display()