I am trying to implement a stack using a linked list. I have written the code below and MAY YOU PLEASE ASSIST IN IMPROVING THE CODE I want to Insert the names of big five animals and arrange them in order of size = > as follows Elephant (approximately 5 tonnes +), Rhinoceros (approximately 2 tonnes), Buffalo (650kg), Lion (225 kg), and Leopard (96kgs).
How do I carry out the following
Below is my Code
class Node:
# Class to create nodes of linked list
# constructor initializes node automatically
def __init__(self,data):
self.data = data
self.next = None
class Stack:
# head is default NULL
def __init__(self):
self.head = None
# Checks if stack is empty
def isempty(self):
if self.head == None:
return True
else:
return False
# Method to add data to the stack
# adds to the start of the stack
def push(self,data):
if self.head == None:
self.head=Node(data)
else:
newnode = Node(data)
newnode.next = self.head
self.head = newnode
def find_animal(self, data):
big_head = self.head
index = 0
while big_head.next:
if big_head.data == data:
print(f'{big_head.data} at index {index}')
index += 1
big_head = big_head.next
# Remove element that is the current head (start of the stack)
def pop(self):
if self.isempty():
return None
else:
# Removes the head node and makes
#the preceding one the new head
poppednode = self.head
self.head = self.head.next
poppednode.next = None
return poppednode.data
# Returns the head node data
def peek(self):
if self.isempty():
return None
else:
return self.head.data
# Prints out the stack
def display(self):
iternode = self.head
if self.isempty():
print("Stack Underflow")
else:
while(iternode != None):
print(iternode.data,"->",end = " ")
iternode = iternode.next
return
# Driver code
The_Big_Five = Stack()
The_Big_Five.push("Leopard")
The_Big_Five.push("Lion")
The_Big_Five.push("Buffalo")
The_Big_Five.push("Rhinoceros")
The_Big_Five.push("Elephant")
The_Big_Five.display()
Something you want is not an Stack
. It's a Priority Queue
or Heap
.
There are animals heavier than Elephant and lighter than Leopard. How can you figure out which animal will the user push to the stack? so the data you use in Node
class could be a dictionary with a size
key in its simplest form.
class Node: def __init__(self, name: str, size: int): animal_data = {'name': name, 'size': size} self.data = animal_data self.next = None
When you push new data into the Stack
, you must sort stack by the size
key in data
property in Node
class. So whenever you display the stack, heavier animal will display first.
To return the first element, you could use self.head
property but for the last element, you must change the structure of your linked list. This type of linked list you use is Singly Linked List
but you must use Doubly Linked List
that has an extra property called Tail
and different behavior when Pushing
into and Popping
from stack.
Python has heapq
module that implement all you want with list
. To have a Heap
with Linked List
, you must implement it yourself. Also has deque
module that is an implementation of stack and queue with doubly linked list.
And here you could read more about Heap, Queue and Stack in Python:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.