Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
class Node:
def __init__(self, dataval=None):
self.dataval = dataval
self.nextval = None
class SLinkedList:
def __init__(self):
self.headval = None
def listprint(self):
printval = self.headval
while printval is not None:
print(printval.dataval)
printval = printval.nextval
def AtBeginning(self, newdata):
NewNode = Node(newdata)
def AtEnd(self, newdata):
NewNode = Node(newdata)
if self.headval is None:
self.headval = NewNode
return
last = self.headval
while (last.nextval):
last = last.nextval
last.nextval = NewNode
def Insert(self, val_before, newdata):
'''Insert method - adding a new node into linked list.'''
if val_before is None: # if there isn't any value before the new node
print("No node to insert after") # print info message
return
else:
newNode = Node(newdata) # create new node
newNode.nextval = val_before.nextval # assign a next value for new node that was a next
# node of the value before
val_before.nextval = newNode # set the next value of the value before to the added node
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Thur")
e4 = Node("Fri")
e5 = Node("Sat")
list.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4
e4.nextval = e5
list.AtEnd("Sun")
list.Insert(e2, "Weds")
list.listprint()