Permalink
Cannot retrieve contributors at this time
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?
5003CEM/STD_5.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (42 sloc)
3.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, dataval = None): | |
self.dataval = dataval #assigning the data values | |
self.nextval = None #assinging an empty value to the next | |
class SLinkedList: | |
def __init__(self): | |
self.headval = None #assigning an empty value to the head value | |
def listprint(self): | |
printval = self.headval #creating a print value of the head value | |
while printval is not None: #while loop if the print value isn't empty | |
print (printval.dataval) #printing the value | |
printval = printval.nextval #assing the print value to the next value | |
def AtEnd(self, newdata): | |
NewNode = Node(newdata) #creating a new node | |
if self.headval is None: #if the head value is empty | |
self.headval = NewNode #head value is assinged to the new node | |
return | |
last = self.headval #last value is assigned to the head value | |
while(last.nextval): #while loop of the next value | |
last = last.nextval #last value is assigned to the head value | |
last.nextval = NewNode #next value is assinged to the new node | |
def Insert(self, val_before, newdata): | |
if val_before is None: #if the before value is empty | |
print("No node to insert after") #error message for no node | |
return | |
else: | |
NewNode = Node(newdata) #new variable with the location | |
NewNode.nextval = val_before.nextval #assign NewNode to the value of the previous value | |
val_before.nextval = NewNode #assign the previous value to the new node | |
list = SLinkedList() #creating the list | |
list.headval = Node("Mon") #starting the list | |
e2 = Node("Tue") #creating variables for the lists to link | |
e3 = Node("Thur") | |
e4 = Node("Fri") | |
e5 = Node("Sat") | |
list.headval.nextval = e2 #listing the next values | |
e2.nextval = e3 | |
e3.nextval = e4 | |
e4.nextval = e5 | |
list.AtEnd("Sun") #ending the list | |
list.Insert(list.headval.nextval, "Wed") #adding Wed as a value | |
list.listprint() #printing the list |