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 "

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
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: 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: 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
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Knowledge Booster
Stack
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.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education