I am trying to implement a stack using a linked list based off of just a node class. I am having some issues with the pop method of my class which doesn't seem to exhibit mutability. When I use the pop class method it returns the top of the stack correctly, but it fails to update the stack.
x=stack_linked(1)
x=x.insert(2)
x=x.insert(3)
x.print() # This is correct and prints 3,2,1
print(x.pop()) # This is correct and prints 3, but doesn't actually modify my list
x.print() # This prints 3,2,1
Why is self not mutable? Also how can I modify my class without completely blowing it up or creating a wrapper for it? Here is my class.
class stack_linked(object):
def __init__(self,data):
self.data=data
self.next=None
def insert(self,front):
front=stack_linked(front)
front.next=self
return front
def peek(self):
if self==None:
return None
else
return self.data
def pop(self):
front=self
self=self.next # some sort of issue here
return front
def print(self):
x=self
if x==None:
print("Empty")
else:
print(x.data)
while x.next !=None:
x=x.next
print(x.data)
selfin an instance method doesn't cause the whole instance to be replaced.popshould return the data, not the node..