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?
AdvancedALgorithmsComplete/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.
79 lines (64 sloc)
3.37 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 | |
self.nextval = None | |
class SLinkedList: | |
''' | |
CLass for Linked List, made of Nodes which have Values and reference to the next Node, | |
Class attributes: allValues - a list of all the possible values inside a linked list | |
''' | |
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): | |
''' | |
Class method for inserting values inside the linked list | |
Input: any type. Two values- a value of a node after which new value will be inserted, and value of the new node to be inserted into the linked list | |
Output: boolean. True if val_before argument had been found in a linked list, False if it had not or value of val_before is None | |
''' | |
if val_before is None: #if value of val_before variable is None | |
print("No node to insert after") | |
return False | |
else: #if value of val_before variable is not None | |
curVal = self.headval #create curVal variable and set it to the firs value of the linked list | |
while curVal != None: #repeat until value of curVal is None | |
if curVal.dataval == val_before: #if data attribute of curVal Node is equal to the value of val_before parameter | |
oldNextVal = curVal.nextval #create old next value variable and set it to a reference to the Node referenced in curVal Node nextval attribute | |
curVal.nextval = Node(newdata) #set curVal Node nextval attribute to an instance of Node class with newdata parameter passed as argument | |
curVal.nextval.nextval = oldNextVal #set value of nextval attibute of the Node referenced in curVal Node nextval attribute to Node reference stored in oldNextVal variable | |
return True | |
curVal = curVal.nextval #set curVal variable to the value of curVal nextval attribute | |
return False | |
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.listprint() | |
list.Insert("Tue","Weds") | |
print("\n\nInserted List:") | |
list.listprint() | |
#In insert I had deleted two arguments out of three because there is no reason for them to be there, these arguments have to be calculated, which means that we would need an extra method that only provides these values | |
#The method had been generalized, as it still solves the task given, but does additional work by adding all of the days instead of only one. Just in case I have added additional commented code, which when uncommented, will solve the task as specified | |
#In Insert function I avoid hard coding values like weekdays inside the Class in order to make it reusable |