class node:
def __init__(self, pdata):
self.data = pdata
self.pointer = None
class linkedlist:
def __init__(self):
self.head = None
def insertfront(self, pdata):
newnode = node(pdata)
if self.head is None:
self.head = newnode
return
else:
newnode.pointer = self.head
self.head = newnode
def insertindex(self, data, index):
newnode = node(data)
currentnode = self.head
pos = 0
if pos == index:
self.insertfront(data)
else:
while (currentnode != None and pos+1 != index):
pos+=1
currentnode = currentnode.pointer
if currentnode != None:
newnode.pointer = currentnode.pointer
currentnode.pointer = newnode
else:
print("index not present")
def insertEnd(self, data):
newnode = node(data)
if self.head is None:
self.insertfront(data)
return
else:
currentnode = self.head
while (currentnode.pointer):
currentnode = currentnode.pointer
currentnode.pointer = newnode
def Output(self):
currentnode = self.head
while (currentnode.pointer != None):
print(currentnode.data)
currentnode = currentnode.pointer
print(currentnode.data)
def Delfront(self):
if self.head == None:
return
else:
self.head = self.head.pointer
def Dellast(self):
if self.head == None:
return
elif self.head.pointer == None:
self.Delfront()
else:
currentnode = self.head
while currentnode.pointer.pointer != None:
currentnode = currentnode.pointer
currentnode.pointer = None
#def deleteatend(self):
print("af, means add to front of linked listn"
"al, means add to last of linked listn"
"ai, means add to index of linked listn"
"df, means delete from front of linked listn"
"dl, means delete from last of linked listn"
"di, means delete from index of linked listn"
"o, means output linked listn")
x = ""
llist = linkedlist()
while x != "e":
x = input("What do you want to do to the linked listn>>>")
if x == "af":
y = input("Enter the data")
llist.insertfront(y)
elif x == "al":
y = input("Enter the data")
llist.insertEnd(y)
elif x == "ai":
y = input("Enter the data")
index = int(input("Enter the index location"))
llist.insertindex(y, index - 1)
elif x == "df":
llist.Delfront()
elif x == "dl":
llist.Dellast()
elif x == "o":
print("Llist is starting>")
llist.Output()
print("<Thats all folks")
else:
print("please input something appropriate")