diff options
Diffstat (limited to 'notebooks/data_structures.ipynb')
-rw-r--r-- | notebooks/data_structures.ipynb | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/notebooks/data_structures.ipynb b/notebooks/data_structures.ipynb new file mode 100644 index 0000000..f116cf5 --- /dev/null +++ b/notebooks/data_structures.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ADT - Stack\n", + "Push and pop methods." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stack = [None for index in range(0,10)]\n", + "basePointer = 0\n", + "topPointer = -1\n", + "stackFull = 10\n", + "\n", + "def push(item):\n", + " global stack, topPointer\n", + " topPointer += 1\n", + " if topPointer >= stackFull: \n", + " print(\"ERROR Cannot insert more.\")\n", + " return\n", + " stack[topPointer] = item\n", + " print(stack)\n", + "def pop():\n", + " global stack, topPointer\n", + " if topPointer < basePointer:\n", + " print(\"ERROR List is empty, cannot pop.\")\n", + " return\n", + " itemPopped = stack[topPointer]\n", + " print(f\"I popped this {itemPopped}\")\n", + " stack[topPointer] = None\n", + " topPointer -= 1\n", + " print(stack)\n", + "def test():\n", + " push(1)\n", + " push(2)\n", + " push(69)\n", + "\n", + " pop()\n", + " pop()\n", + "\n", + " print(stack)\n", + "\n", + " pop()\n", + " pop()\n", + "\n", + "test()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ADT - Queue\n", + "Enqueue and dequeue methods." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "queue = [None for index in range(0, 10)]\n", + "frontPointer = 0\n", + "rearPointer = -1\n", + "queueLength = 0\n", + "queueFull = 10\n", + "def dequeue():\n", + " global queue, queueLength, queueFull, rearPointer, frontPointer\n", + " if queueLength == 0:\n", + " print(\"ERR:: Cannot dequeue an empty queue!\") \n", + " return\n", + " item = queue[frontPointer]\n", + " queue[frontPointer] = None\n", + " frontPointer += 1\n", + " print(f\"Dequeued element {item}\")\n", + " return\n", + " \n", + "def enqueue(item):\n", + " global queue, queueLength, queueFull, rearPointer\n", + " if queueLength < queueFull:\n", + " if rearPointer < len(queue) - 1:\n", + " rearPointer += 1\n", + " else:\n", + " rearPointer = 0\n", + " queueLength += 1\n", + " queue[rearPointer] = item\n", + " print(queue)\n", + " return\n", + " print(\"ERROR queue length exceeded!\")\n", + " return\n", + "for i in range(13):\n", + " enqueue(10 * i)\n", + "dequeue()\n", + "dequeue()\n", + "dequeue()\n", + "print(queue)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.12.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} |