-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
163 lines (127 loc) · 2.96 KB
/
temp.py
File metadata and controls
163 lines (127 loc) · 2.96 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.max_size = 10
def is_empty(self):
if self.head is None:
return True
return False
def insert_node_at_position(self,data, position):
new = Node(data)
pointer = self.head
counter = 1
while pointer.next is not None:
if counter == position:
new.next = pointer.next
pointer.next = new
break
counter += 1
pointer = pointer.next
def get_copy_data_at_front(self):
if self.is_empty():
return -1
return self.head.data
def get_copy_data_at_back(self):
if self.is_empty():
return -1
head = self.head
while head.next is not None:
head = head.next
return head.data
def remove_node_at_begin(self):
if self.is_empty():
return False
self.head = self.head.next
return True
def remove_node_at_end(self):
if self.is_empty():
return False
head = self.head
if head.next is None:
self.head = None
return
while head.next.next is not None:
head = head.next
head.next = None
return True
def remove_node_at_specific_pos(self, position):
if self.head is None:
return
if position == 0:
self.head = self.head.next
return self.head
index = 0
current = self.head
prev = self.head
temp = self.head
while current is not None:
if index == position:
temp = current.next
break
prev = current
current = current.next
index += 1
prev.next = temp
return prev
def push_at_start(self, new_data):
new_nod = Node(new_data)
new_nod.next = self.head
self.head = new_nod
def push_at_end(self, new_data):
if self.head is None:
self.push_at_start(new_data)
return
head = self.head
while head.next is not None:
head = head.next
new_nod = Node(new_data)
head.next = new_nod
def printList(self):
temp = self.head
while (temp):
print(temp.data,end=" ")
temp = temp.next
def node_contains_data(self,data):
head = self.data
while head is not None:
if head.data == data:
return head
head = head.next
return None
def get_value_at_specific_index(self,index):
head = self.head
while head is not None and index > 0 :
head = head.next
index = index -1
return -1 if head is None else head.data
def check_linklist_full(self):
head = self.head
count = 0
while head is not None:
head = head.next
count += 1
if count >= self.max_size:
return True
return False
if __name__=='__main__':
# Start with the empty list
l = LinkedList()
l.push_at_end(6)
l.push_at_start(7)
l.push_at_end(1)
l.push_at_start(4)
l.printList()
print(l.is_empty())
l.insert_node_at_position(8,2)
l.printList()
l.remove_node_at_specific_pos(2)
print("After Removal")
l.printList()
print("Get value at front : ",l.get_copy_data_at_front())
print("Get value at back : ",l.get_copy_data_at_back())
print("Get value at 2 : ",l.get_value_at_specific_index(2))
print(l.check_linklist_full())