diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-02-19 09:43:52 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-02-19 09:43:52 +0200 |
commit | 8834da60a01294fd509c7cebf3b129fcc378d152 (patch) | |
tree | a1c6c4bd71e95780f87d35240754c5b54d3042ae /notebooks/data_structures.ipynb | |
parent | 34bd7099d27656b4454015b0c410ca1713db5271 (diff) | |
download | cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.gz cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.bz2 cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.zip |
chore: changing to ipynb
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 +} |