Created
August 25, 2022 16:53
-
-
Save jermspeaks/4540fb821d21adee5df29609aec131bb to your computer and use it in GitHub Desktop.
Data Structures in Python
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
# Node for LinkedList | |
class Node: | |
def __init__(self, value): | |
self.value = value | |
self.next = null | |
# A data structure that is a series of nodes, | |
# and each node points to the next node of the list | |
# Linked lists use the “last-in-first-out” method (similar to a stack)where nodes are added to and deleted from the same end. | |
class LinkedList: | |
def __init__(self): | |
self.head = null | |
self.tail = null | |
self.length = 0 | |
def isEmpty(): | |
# Check whether the queue is empty | |
self.length == 0 | |
def push(value): | |
# Add an element to the linked list | |
new_node = Node(value) | |
if not self.head: | |
self.head = new_node | |
self.tail = new_node | |
else: | |
self.tail.next = new_node | |
self.tail = new_node | |
self.length += 1 | |
def pop(): | |
self | |
# Remove an element from the linked list | |
def get(index): | |
# takes an index and returns the node at that index | |
self | |
def delete(index): | |
# takes an index and removes node at that index | |
self | |
def print(): | |
currNode = self.head | |
while (currNode != None): | |
currNode = currNode.next | |
# list = new LinkedList() | |
# list.push("Emma"); | |
# list.push("Sarah"); | |
# list.push("Ivy"); |
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
# A "first-in-last-out" data structure | |
class QueueEmptyError(IndexError): | |
"""Attempt to pop an empty stack.""" | |
class Queue: | |
def __init__(self): | |
self._list = [] | |
def enqueue(item): | |
# Add an item to the back of the queue | |
self._list.insert(0,item) | |
def dequeue(): | |
# Remove an item from the front of the queue | |
if not self._list: | |
raise StackEmptyError | |
else: | |
self._list.pop() | |
def peek(): | |
# Return first item of the queue | |
self | |
# isEmpty() { | |
# // Returns a boolean if the queue is empty | |
# } | |
# get length() { | |
# // Return the length of the queue | |
# } | |
# } | |
# const queue = new Queue(); | |
# console.log(queue); | |
# console.log(queue.length); |
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
# A "first-in-last-out" data structure | |
class StackEmptyError(IndexError): | |
"""Attempt to pop an empty stack.""" | |
class Stack: | |
def __init__(self): | |
self._list = [] | |
def pop(): | |
# Removes the top item from the stack | |
# should we check if list is not empty? => yep | |
if not self._list: | |
raise StackEmptyError() | |
else: | |
return self._list.pop() | |
def push(item): | |
# Adds an item to the top of the stack | |
self._list.append(item) | |
def peek(): | |
# Return the item at the front of the queue (but do not remove it) | |
if not self._list: | |
return StackEmptyError | |
else: | |
return self._list[-1] | |
# def isEmpty(): | |
# # Check whether the queue is empty | |
# get length() { | |
# // Return the length of the stack | |
# let books = new Stack() | |
# books.push("Book 1") | |
# books.push("Book 2") | |
# books.push("Book 3") | |
# print books.peek()) | |
# books.pop() | |
# print books.peek() | |
# print books.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment