|
100 | 100 | ]
|
101 | 101 | }
|
102 | 102 | },
|
| 103 | + "SinglyLinkedList": { |
| 104 | + "addFront": { |
| 105 | + "code": [ |
| 106 | + ["procedure addFront(data)"], |
| 107 | + [" if size == 0"], |
| 108 | + [" head ← new Node(data)"], |
| 109 | + [" else"], |
| 110 | + [" Node newHead ← new Node(data)"], |
| 111 | + [" newHead.next ← head"], |
| 112 | + [" head ← newHead"], |
| 113 | + [" end if"], |
| 114 | + [" size++"], |
| 115 | + ["end procedure"] |
| 116 | + ], |
| 117 | + "english": [ |
| 118 | + ["procedure addFront(data)"], |
| 119 | + [" if size == 0"], |
| 120 | + [" head ← new Node(data)"], |
| 121 | + [" else"], |
| 122 | + [" Node newHead ← new Node(data)"], |
| 123 | + [" newHead.next ← head"], |
| 124 | + [" head ← newHead"], |
| 125 | + [" end if"], |
| 126 | + [" size++"], |
| 127 | + ["end procedure"] |
| 128 | + ] |
| 129 | + }, |
| 130 | + "addBack": { |
| 131 | + "code": [ |
| 132 | + ["procedure addBack(data)"], |
| 133 | + [" if size == 0"], |
| 134 | + [" head ← new Node(data)"], |
| 135 | + [" else"], |
| 136 | + [" Node curr ← head"], |
| 137 | + [" for i ← 0 to size - 2, i++:"], |
| 138 | + [" curr = curr.next"], |
| 139 | + [" curr.next ← new Node(data)"], |
| 140 | + [" end if"], |
| 141 | + [" size++"], |
| 142 | + ["end procedure"] |
| 143 | + ], |
| 144 | + "english": [ |
| 145 | + ["procedure addBack(data)"], |
| 146 | + [" if (list is empty):"], |
| 147 | + [" head points to new node with data"], |
| 148 | + [" else:"], |
| 149 | + [" curr points to head"], |
| 150 | + [" for (i from front to node before back):"], |
| 151 | + [" curr moves to next node"], |
| 152 | + [" curr.next points to new node with data"], |
| 153 | + [" end if"], |
| 154 | + [" increment size"], |
| 155 | + ["end procedure"] |
| 156 | + ] |
| 157 | + }, |
| 158 | + "addIndex": { |
| 159 | + "code": [ |
| 160 | + ["procedure addAtIndex(index, data)"], |
| 161 | + [" if index == 0"], |
| 162 | + [" addFront(data)"], |
| 163 | + [" else if index == size"], |
| 164 | + [" addBack(data)"], |
| 165 | + [" else"], |
| 166 | + [" Node curr ← head"], |
| 167 | + [" for i ← 0 to index - 2, i++:"], |
| 168 | + [" curr = curr.next"], |
| 169 | + [" end for"], |
| 170 | + [" Node newNode ← new Node(data)"], |
| 171 | + [" newNode.next ← curr.next"], |
| 172 | + [" curr ← newNode"], |
| 173 | + [" size++"], |
| 174 | + [" end if"], |
| 175 | + ["end procedure"] |
| 176 | + ], |
| 177 | + "english": [ |
| 178 | + ["procedure addAtIndex(index, data)"], |
| 179 | + [" if (index is at the front):"], |
| 180 | + [" call addFront with data"], |
| 181 | + [" else (index is at the back):"], |
| 182 | + [" call addBack with data"], |
| 183 | + [" else:"], |
| 184 | + [" curr points to head"], |
| 185 | + [" for (i from front to node before index):"], |
| 186 | + [" curr moves to next node"], |
| 187 | + [" end for"], |
| 188 | + [" create newNode node with data"], |
| 189 | + [" newNode.next points to curr.next"], |
| 190 | + [" curr points to newNode"], |
| 191 | + [" increment size"], |
| 192 | + [" end if"], |
| 193 | + ["end procedure"] |
| 194 | + ] |
| 195 | + }, |
| 196 | + "removeFront": { |
| 197 | + "code": [ |
| 198 | + ["procedure removeFront()"], |
| 199 | + [" T data ← head.data"], |
| 200 | + [" head ← head.next"], |
| 201 | + [" size--"], |
| 202 | + [" return data"], |
| 203 | + ["end procedure"] |
| 204 | + ], |
| 205 | + "english": [ |
| 206 | + ["procedure removeFront()"], |
| 207 | + [" copy data at head to temp"], |
| 208 | + [" head moves to next node"], |
| 209 | + [" decrement size"], |
| 210 | + [" return temp"], |
| 211 | + ["end procedure"] |
| 212 | + ] |
| 213 | + }, |
| 214 | + "removeBack": { |
| 215 | + "code": [ |
| 216 | + ["procedure removeBack()"], |
| 217 | + [" if size == 1:"], |
| 218 | + [" removeFront()"], |
| 219 | + [" else"], |
| 220 | + [" Node curr ← head"], |
| 221 | + [" for i ← 0 to size - 2"], |
| 222 | + [" curr ← curr.next"], |
| 223 | + [" end for"], |
| 224 | + [" T data ← curr.next.data"], |
| 225 | + [" curr.next ← null"], |
| 226 | + [" size--"], |
| 227 | + [" return data"], |
| 228 | + [" end if"], |
| 229 | + ["end procedure"] |
| 230 | + ], |
| 231 | + "english": [ |
| 232 | + ["procedure removeBack()"], |
| 233 | + [" if (list has 1 node):"], |
| 234 | + [" call removeFront"], |
| 235 | + [" else:"], |
| 236 | + [" curr points to head"], |
| 237 | + [" for (i from front to node before back):"], |
| 238 | + [" curr moves to next node"], |
| 239 | + [" end for"], |
| 240 | + [" copy data at curr.next to temp"], |
| 241 | + [" null out curr.next"], |
| 242 | + [" decrement size"], |
| 243 | + [" return temp"], |
| 244 | + [" end if"], |
| 245 | + ["end procedure"] |
| 246 | + ] |
| 247 | + }, |
| 248 | + "removeIndex":{ |
| 249 | + "code": [ |
| 250 | + ["procedure removeFromIndex(index)"], |
| 251 | + [" if index == 0"], |
| 252 | + [" removeFront()"], |
| 253 | + [" else if index == size - 1"], |
| 254 | + [" removeBack()"], |
| 255 | + [" else"], |
| 256 | + [" Node curr ← head"], |
| 257 | + [" for i ← 0 to index - 2, i++"], |
| 258 | + [" curr ← curr.next"], |
| 259 | + [" end for"], |
| 260 | + [" T data ← curr.next.data"], |
| 261 | + [" curr.next ← curr.next.next"], |
| 262 | + [" size--"], |
| 263 | + [" return data"], |
| 264 | + [" end if"], |
| 265 | + ["end procedure"] |
| 266 | + ], |
| 267 | + "english": [ |
| 268 | + ["procedure removeFromIndex(index)"], |
| 269 | + [" if (index is at the front):"], |
| 270 | + [" call removeFront"], |
| 271 | + [" else if (index is at the back):"], |
| 272 | + [" call removeBack"], |
| 273 | + [" else:"], |
| 274 | + [" curr points to head"], |
| 275 | + [" for (i from front to node before index):"], |
| 276 | + [" curr moves to next node"], |
| 277 | + [" end for"], |
| 278 | + [" copy data at curr to temp"], |
| 279 | + [" curr.next points to curr.next.next"], |
| 280 | + [" decrement size"], |
| 281 | + [" return temp"], |
| 282 | + [" end if"], |
| 283 | + ["end procedure"] |
| 284 | + ] |
| 285 | + } |
| 286 | + }, |
103 | 287 | "DoublyLinkedList": {
|
104 | 288 | "addFront": {
|
105 | 289 | "code": [
|
|
1941 | 2125 | [" end for"],
|
1942 | 2126 | [" while PQ.size > 0 and VS.size < G.vertices.size:"],
|
1943 | 2127 | [" edge(u, w) ← PQ.dequeue()"],
|
1944 |
| - [" MST.add(edge(u, w))"], |
1945 |
| - [" VS.add(w)"], |
1946 |
| - [" for all edge(w, x) adjacent to w:"], |
1947 |
| - [" if x not in VS:"], |
1948 |
| - [" PQ.enqueue(edge(w, x))"], |
1949 |
| - [" end if"], |
1950 |
| - [" end for"], |
| 2128 | + [" if w not in VS:"], |
| 2129 | + [" MST.add(edge(u, w))"], |
| 2130 | + [" VS.add(w)"], |
| 2131 | + [" for all edge(w, x) adjacent to w:"], |
| 2132 | + [" if x not in VS:"], |
| 2133 | + [" PQ.enqueue(edge(w, x))"], |
| 2134 | + [" end if"], |
| 2135 | + [" end for"], |
| 2136 | + [" end if"], |
1951 | 2137 | [" end while"],
|
1952 | 2138 | [" return MST"],
|
1953 | 2139 | ["end procedure"]
|
|
1963 | 2149 | [" end for"],
|
1964 | 2150 | [" while (queue is not empty and visited set is not full):"],
|
1965 | 2151 | [" dequeue edge between vertices V, W"],
|
1966 |
| - [" add edge to MST"], |
1967 |
| - [" add W to visited set"], |
1968 |
| - [" for each edge adjacent to W:"], |
1969 |
| - [" if (other endpoint vertex is not visited):"], |
1970 |
| - [" enqueue edge between W and other endpoint vertex"], |
1971 |
| - [" end if"], |
1972 |
| - [" end for"], |
| 2152 | + [" if (W is not visited):"], |
| 2153 | + [" add edge to MST"], |
| 2154 | + [" add W to visited set"], |
| 2155 | + [" for each edge adjacent to W:"], |
| 2156 | + [" if (other endpoint vertex is not visited):"], |
| 2157 | + [" enqueue edge between W and other endpoint vertex"], |
| 2158 | + [" end if"], |
| 2159 | + [" end for"], |
| 2160 | + [" end if"], |
1973 | 2161 | [" end while"],
|
1974 | 2162 | [" return MST"],
|
1975 | 2163 | ["end procedure"]
|
|
0 commit comments