Need help with python code. Write a LikedStack . Test it by: Add numbers from 7 to 22 in the stack. Print the length of the stack Check if the stack is empty Show the item on the top of the stack Remove the last item in the stack Add 45 to the stack Remove all items in the stack Check if the stack is empty """File: linkedstack.pyAuthor: Ken Lambert"""from node import Nodefrom abstractstack import AbstractStackclass LinkedStack(AbstractStack):"""A link-based stack implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""# Accessor methodsdef __iter__(self):"""Supports iteration over a view of self.Visits items from bottom to top of stack."""def visitNodes(node):"""Adds items to tempList from tail to head."""def peek(self):"""Returns the item at the top of the stack.Precondition: the stack is not empty.Raises: KeyError if the stack is empty."""if self.isEmpty():# Mutator methodsdef clear(self):"""Makes self become empty."""def push(self, item):"""Adds item to the top of the stack."""def pop(self):"""Removes and returns the item at the top of the stack.Precondition: the stack is not empty.Raises: KeyError if the stack is empty.Postcondition: the top item is removed from the stack.""" " """File: node.pyNode classes for one-way linked structures and two-waylinked structures."""class Node(object):def __init__(self, data, next = None):"""Instantiates a Node with default next of None"""self.data = dataself.next = nextclass TwoWayNode(Node):def __init__(self, data, previous = None, next = None):Node.__init__(self, data, next)self.previous = previous# Just an empty linknode1 = None# A node containing data and an empty linknode2 = Node("A", None)# A node containing data and a link to node2node3 = Node("B", node2) "" """File: abstractstack.pyAuthor: Ken Lambert"""from abstractcollection import AbstractCollectionclass AbstractStack(AbstractCollection):"""An abstract stack implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""AbstractCollection.__init__(self, sourceCollection)# Mutator methodsdef add(self, item):"""Adds item to self."""self.push(item)"" """File: abstractcollection.pyAuthor: Ken Lambert"""class AbstractCollection(object):"""An abstract collection implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""self.size = 0if sourceCollection:for item in sourceCollection:self.add(item)# Accessor methodsdef isEmpty(self):"""Returns True if len(self) == 0, or False otherwise."""return len(self) == 0def __len__(self):"""Returns the number of items in self."""return self.sizedef __str__(self):"""Returns the string representation of self."""return "[" + ", ".join(map(str, self)) + "]"def __add__(self, other):"""Returns a new bag containing the contentsof self and other."""result = type(self)(self)for item in other:result.add(item)return resultdef __eq__(self, other):"""Returns True if self equals other,or False otherwise."""if self is other: return Trueif type(self) != type(other) or \len(self) != len(other):return FalseotherIter = iter(other)for item in self:if item != next(otherIter):return Falsereturn Truedef count(self, item):"""Returns the number of instances of item in self."""total = 0for nextItem in self:if nextItem == item:total += 1return total "
Need help with python code. Write a LikedStack . Test it by: Add numbers from 7 to 22 in the stack. Print the length of the stack Check if the stack is empty Show the item on the top of the stack Remove the last item in the stack Add 45 to the stack Remove all items in the stack Check if the stack is empty """File: linkedstack.pyAuthor: Ken Lambert"""from node import Nodefrom abstractstack import AbstractStackclass LinkedStack(AbstractStack):"""A link-based stack implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""# Accessor methodsdef __iter__(self):"""Supports iteration over a view of self.Visits items from bottom to top of stack."""def visitNodes(node):"""Adds items to tempList from tail to head."""def peek(self):"""Returns the item at the top of the stack.Precondition: the stack is not empty.Raises: KeyError if the stack is empty."""if self.isEmpty():# Mutator methodsdef clear(self):"""Makes self become empty."""def push(self, item):"""Adds item to the top of the stack."""def pop(self):"""Removes and returns the item at the top of the stack.Precondition: the stack is not empty.Raises: KeyError if the stack is empty.Postcondition: the top item is removed from the stack.""" " """File: node.pyNode classes for one-way linked structures and two-waylinked structures."""class Node(object):def __init__(self, data, next = None):"""Instantiates a Node with default next of None"""self.data = dataself.next = nextclass TwoWayNode(Node):def __init__(self, data, previous = None, next = None):Node.__init__(self, data, next)self.previous = previous# Just an empty linknode1 = None# A node containing data and an empty linknode2 = Node("A", None)# A node containing data and a link to node2node3 = Node("B", node2) "" """File: abstractstack.pyAuthor: Ken Lambert"""from abstractcollection import AbstractCollectionclass AbstractStack(AbstractCollection):"""An abstract stack implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""AbstractCollection.__init__(self, sourceCollection)# Mutator methodsdef add(self, item):"""Adds item to self."""self.push(item)"" """File: abstractcollection.pyAuthor: Ken Lambert"""class AbstractCollection(object):"""An abstract collection implementation."""# Constructordef __init__(self, sourceCollection = None):"""Sets the initial state of self, which includes thecontents of sourceCollection, if it's present."""self.size = 0if sourceCollection:for item in sourceCollection:self.add(item)# Accessor methodsdef isEmpty(self):"""Returns True if len(self) == 0, or False otherwise."""return len(self) == 0def __len__(self):"""Returns the number of items in self."""return self.sizedef __str__(self):"""Returns the string representation of self."""return "[" + ", ".join(map(str, self)) + "]"def __add__(self, other):"""Returns a new bag containing the contentsof self and other."""result = type(self)(self)for item in other:result.add(item)return resultdef __eq__(self, other):"""Returns True if self equals other,or False otherwise."""if self is other: return Trueif type(self) != type(other) or \len(self) != len(other):return FalseotherIter = iter(other)for item in self:if item != next(otherIter):return Falsereturn Truedef count(self, item):"""Returns the number of instances of item in self."""total = 0for nextItem in self:if nextItem == item:total += 1return total "
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 21SA
Related questions
Question
Need help with python code.
Write a LikedStack .
Test it by:
- Add numbers from 7 to 22 in the stack.
- Print the length of the stack
- Check if the stack is empty
- Show the item on the top of the stack
- Remove the last item in the stack
- Add 45 to the stack
- Remove all items in the stack
- Check if the stack is empty
"""
File: linkedstack.py
Author: Ken Lambert
"""
from node import Node
from abstractstack import AbstractStack
class LinkedStack(AbstractStack):
"""A link-based stack implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
# Accessor methods
def __iter__(self):
"""Supports iteration over a view of self.
Visits items from bottom to top of stack."""
def visitNodes(node):
"""Adds items to tempList from tail to head."""
def peek(self):
"""
Returns the item at the top of the stack.
Precondition: the stack is not empty.
Raises: KeyError if the stack is empty."""
if self.isEmpty():
# Mutator methods
def clear(self):
"""Makes self become empty."""
def push(self, item):
"""Adds item to the top of the stack."""
def pop(self):
"""
Removes and returns the item at the top of the stack.
Precondition: the stack is not empty.
Raises: KeyError if the stack is empty.
Postcondition: the top item is removed from the stack."""
File: linkedstack.py
Author: Ken Lambert
"""
from node import Node
from abstractstack import AbstractStack
class LinkedStack(AbstractStack):
"""A link-based stack implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
# Accessor methods
def __iter__(self):
"""Supports iteration over a view of self.
Visits items from bottom to top of stack."""
def visitNodes(node):
"""Adds items to tempList from tail to head."""
def peek(self):
"""
Returns the item at the top of the stack.
Precondition: the stack is not empty.
Raises: KeyError if the stack is empty."""
if self.isEmpty():
# Mutator methods
def clear(self):
"""Makes self become empty."""
def push(self, item):
"""Adds item to the top of the stack."""
def pop(self):
"""
Removes and returns the item at the top of the stack.
Precondition: the stack is not empty.
Raises: KeyError if the stack is empty.
Postcondition: the top item is removed from the stack."""
"
"""
File: node.py
Node classes for one-way linked structures and two-way
linked structures.
"""
class Node(object):
def __init__(self, data, next = None):
"""Instantiates a Node with default next of None"""
self.data = data
self.next = next
class TwoWayNode(Node):
def __init__(self, data, previous = None, next = None):
Node.__init__(self, data, next)
self.previous = previous
# Just an empty link
node1 = None
# A node containing data and an empty link
node2 = Node("A", None)
# A node containing data and a link to node2
node3 = Node("B", node2)
File: node.py
Node classes for one-way linked structures and two-way
linked structures.
"""
class Node(object):
def __init__(self, data, next = None):
"""Instantiates a Node with default next of None"""
self.data = data
self.next = next
class TwoWayNode(Node):
def __init__(self, data, previous = None, next = None):
Node.__init__(self, data, next)
self.previous = previous
# Just an empty link
node1 = None
# A node containing data and an empty link
node2 = Node("A", None)
# A node containing data and a link to node2
node3 = Node("B", node2)
"
"
"
"""
File: abstractstack.py
Author: Ken Lambert
"""
from abstractcollection import AbstractCollection
class AbstractStack(AbstractCollection):
"""An abstract stack implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
AbstractCollection.__init__(self, sourceCollection)
# Mutator methods
def add(self, item):
"""Adds item to self."""
self.push(item)"
"
File: abstractstack.py
Author: Ken Lambert
"""
from abstractcollection import AbstractCollection
class AbstractStack(AbstractCollection):
"""An abstract stack implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
AbstractCollection.__init__(self, sourceCollection)
# Mutator methods
def add(self, item):
"""Adds item to self."""
self.push(item)"
"
"""
File: abstractcollection.py
Author: Ken Lambert
"""
class AbstractCollection(object):
"""An abstract collection implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
return len(self) == 0
def __len__(self):
"""Returns the number of items in self."""
return self.size
def __str__(self):
"""Returns the string representation of self."""
return "[" + ", ".join(map(str, self)) + "]"
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = type(self)(self)
for item in other:
result.add(item)
return result
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
if self is other: return True
if type(self) != type(other) or \
len(self) != len(other):
return False
otherIter = iter(other)
for item in self:
if item != next(otherIter):
return False
return True
def count(self, item):
"""Returns the number of instances of item in self."""
total = 0
for nextItem in self:
if nextItem == item:
total += 1
return total
File: abstractcollection.py
Author: Ken Lambert
"""
class AbstractCollection(object):
"""An abstract collection implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
return len(self) == 0
def __len__(self):
"""Returns the number of items in self."""
return self.size
def __str__(self):
"""Returns the string representation of self."""
return "[" + ", ".join(map(str, self)) + "]"
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = type(self)(self)
for item in other:
result.add(item)
return result
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
if self is other: return True
if type(self) != type(other) or \
len(self) != len(other):
return False
otherIter = iter(other)
for item in self:
if item != next(otherIter):
return False
return True
def count(self, item):
"""Returns the number of instances of item in self."""
total = 0
for nextItem in self:
if nextItem == item:
total += 1
return total
"
AI-Generated Solution
Unlock instant AI solutions
Tap the button
to generate a solution
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning