aboutsummaryrefslogtreecommitdiff
path: root/notebooks/p41-mj17.ipynb
blob: d60ea403f30115829268df2004e2923b9426a748 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# May June 2017 Question 4\n",
    "> As homework due for March 04, 2025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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": 2,
   "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": 3,
   "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"
     ]
    }
   ],
   "source": [
    "# Q4 c) iv)\n",
    "contacts = LinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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
}