summaryrefslogtreecommitdiff
path: root/notebooks/data_structures.ipynb
blob: f116cf5e43da8822fefbf45f8ca45003d9ccc86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
}