-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
136 lines (121 loc) · 3.76 KB
/
temp.py
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
class node:
def __init__(self,data):
self.data = data
self.next = None
#self.temp = None
class linkedlist:
def __init__(self):
self.head = None
#self.temp = None
def insert(self,element,temp=None):
if(self.head==None):
self.head = node(element)
else:
if(temp==None):
temp = self.head
while(temp.next is None):
#y = node(element)
temp.next = node(element)
temp = temp.next
break
return temp
def printList(self):
temp = self.head
while (temp):
print(temp.data,end=" ")
temp = temp.next
def deletefirst(self):
temp = self.head.next
self.head = self.head.next
def deletelast(self,x):
temp = self.head
while(temp.next!=None):
y = temp
temp = temp.next
y.next = None
x = y
return x
def deletemiddle(self,position):
dcount = 0
temp =self.head
while(temp):
dcount-=-1
if(dcount==position-1):
tempf = temp
if(dcount==position+1):
temph = temp
tempf.next = temph
break
temp =temp.next
def reverse(self):
temp = self.head
while(temp):
if(newnode.head==None):
x = temp.data
newnode.head = node(x)
else:
tempy = newnode.head
x = temp.data
newnode.head = node(x)
newnode.head.next = tempy
temp=temp.next
return newnode
def splitreverse(self,key):
dcount =0
temp = self.head
while(temp):
dcount -=-1
if(dcount != key):
if(newnode.head==None):
x = temp.data
newnode.head = node(x)
else:
tempy = newnode.head
x = temp.data
newnode.head = node(x)
newnode.head.next = tempy
else:
tempy = temp
x = temp.data
newnode.temp = node(x)
newnode.temp.next = tempy
temp=temp.next
return newnode
if __name__ == "__main__":
llist = linkedlist()
#DRIVER FUNCTION
condition =True
while(condition == True):
print("\n1.Insert\n2.Delete\n3.Print\n4.Exit\n5.Reverse")
option = int(input())
if(option ==1):
element = int(input())
if(llist.head==None):
x = llist.insert(element,temp =None)
else:
x=llist.insert(element,x)
elif(option ==2):
print("\n1.Delete First\n2.Delete Middle\n3.Delete Last")
op = int(input())
#elementd = int(input())
if(op == 1):
llist.deletefirst()
elif(op==2):
position = int(input())
llist.deletemiddle(position)
elif(op==3):
x = llist.deletelast(x)
elif(option == 3):
#continue
llist.printList()
elif(option == 4):
condition = False
elif(option == 5):
newnode = linkedlist()
x = llist.reverse()
newnode.printList()
elif(option==6):
newnode = linkedlist()
key = int(input())
x = llist.splitreverse(key)
newnode.printList()