summaryrefslogtreecommitdiff
path: root/notebooks/p41-mj17.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'notebooks/p41-mj17.ipynb')
-rw-r--r--notebooks/p41-mj17.ipynb145
1 files changed, 145 insertions, 0 deletions
diff --git a/notebooks/p41-mj17.ipynb b/notebooks/p41-mj17.ipynb
new file mode 100644
index 0000000..b0b3bdd
--- /dev/null
+++ b/notebooks/p41-mj17.ipynb
@@ -0,0 +1,145 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# May June 2017 Question 4"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Q4b\n",
+ "class Node:\n",
+ " def __init__(self, data, pointer):\n",
+ " self.__Data = data\n",
+ " self.__Pointer = pointer\n",
+ " def CreateNode(data, pointer):\n",
+ " pass\n",
+ " def SetData(data):\n",
+ " self.__Data = data\n",
+ " def SetPointer(pointer):\n",
+ " self.__Pointer = pointer\n",
+ " def GetData():\n",
+ " return self.__Data\n",
+ " def GetPointer():\n",
+ " return self.__Pointer\n",
+ " ### DEBUG FN\n",
+ " def __str__(self):\n",
+ " return f\"[{self.__Data}] -> ({self.__Pointer})\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Q4 c)\n",
+ "- i. A null pointer is a pointer that does not point to anything, indicating the end of a list.\n",
+ "- ii. Value: -1; -1 is one less than the smallest index that can occur in the array, so it can be used to index a non-existent element."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[None, None, None, None, None, None, None, None]\n",
+ "at: 0\n",
+ "at: 1\n",
+ "at: 2\n",
+ "at: 3\n",
+ "at: 4\n",
+ "at: 5\n",
+ "at: 6\n",
+ "[0] [None] -> (1)\n",
+ "[1] [None] -> (2)\n",
+ "[2] [None] -> (3)\n",
+ "[3] [None] -> (4)\n",
+ "[4] [None] -> (5)\n",
+ "[5] [None] -> (6)\n",
+ "[6] [None] -> (7)\n",
+ "[7] [None] -> (-1)\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Q4 c) iii)\n",
+ "NULL_POINTER = -1\n",
+ "NODE_ARRAY_LENGTH = 8\n",
+ "class LinkedList:\n",
+ " def __init__(self):\n",
+ " self.__HeadPointer = -1\n",
+ " self.__FreePointer = 0\n",
+ " node_arr = [None for i in range(NODE_ARRAY_LENGTH)]\n",
+ " print(node_arr)\n",
+ " for i in range(len(node_arr) - 1):\n",
+ " print(f\"at: {i}\")\n",
+ " node_arr[i] = Node(None, i+1)\n",
+ " node_arr[len(node_arr) - 1] = Node(None, NULL_POINTER)\n",
+ " self.__NodeArray = node_arr\n",
+ " ## DEBUG FN\n",
+ " def printme(self):\n",
+ " i = 0\n",
+ " for elem in self.__NodeArray:\n",
+ " print(f\"[{i}] {elem}\")\n",
+ " i += 1\n",
+ "## DEBUG CODE\n",
+ "l = LinkedList()\n",
+ "l.printme()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Q4 c) iv)\n",
+ "contacts = LinkedList()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# ... from Q4 c) iii) ...\n",
+ "def OutputListToConsole(self):\n",
+ " i = 0\n",
+ " for elem in self.__NodeArray:\n",
+ " print(f\"[{i}] {elem}\")\n",
+ " i += 1"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": ".venv",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.7"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}