This repository contains Python implementations of fundamental data structures: Node, Stack, Queue, and DoublyLinkedList. They were adapted from exercises in my university labs. Feel free to use them as you wish.
The Node class is the building block for linked data structures. Each node contains:
value: The data stored in the node.next: A reference to the nextNodein the sequence.prev: A reference to the previousNode(used in doubly linked lists).
The Stack class implements a last-in, first-out (LIFO) stack with the following operations:
is_empty(): Returns whether the stack is empty.push(value): Adds an element to the top of the stack.pop(): Removes and returns the value at the top of the stack.peek(): Returns the value at the top without removing it.size(): Returns the number of elements in the stack.
The Queue class implements a first-in, first-out (FIFO) queue with the following operations:
is_empty(): Returns whether the queue is empty.enqueue(value): Adds an element to the end of the queue.dequeue(): Removes and returns the value at the front of the queue.size(): Returns the number of elements in the queue.
The DoublyLinkedList class implements a doubly linked list with bidirectional links between nodes and the following operations:
is_empty(): Returns whether the list is empty.append(value): Adds an element to the end of the list.insertBefore(node, node_to_insert): Insertsnode_to_insertimmediately beforenode.insertAfter(node, node_to_insert): Insertsnode_to_insertimmediately afternode.remove(value): Removes the first node with the given value and returnsTrueif found, otherwiseFalse.remove_node(node_to_remove): Removes the specifiedNodefrom the list.__str__(): Returns a string representation of the list values.
These implementations provide basic functionality and can serve as building blocks for more complex algorithms and applications.