class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def printList(self):
temp=self.head
while(temp):
print(int(self.head.data))
temp=self.head.next
def insertEnd(self,data):
newnode=Node(data)
if self.head is None:
self.head=newnode
else:
last=self.head
while(last.next):
last=last.next
last.next=newnode
def delEnd(self):
if self.head is None:
print("Empty")
else:
last=self.head
while(last.next):
last=last.next
print(last.data)
del last
lst=LinkedList()
while(1):
choice=int(input("1:To Insert at end \n2:To delete at end \n3:To print list"))
if choice==1:
value=int(input("Enter the value: "))
lst.insertEnd(value)
elif choice==2:
lst.delEnd()
elif choice==3:
lst.printList()
In your loop in printList
, you're not using temp
when you should:
while(temp):
print(int(self.head.data))
temp=self.head.next
If the list has more than two nodes, this loop runs forever, since temp
always gets set to self.head.next
over and over.
You want:
while temp:
print(int(temp.data))
temp = temp.next
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.