Skip to content

Commit 18bd2db

Browse files
authored
add linked list exercise (#432)
* add linked list exercise
1 parent d6d72dd commit 18bd2db

File tree

8 files changed

+443
-0
lines changed

8 files changed

+443
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@
926926
"practices": [],
927927
"prerequisites": [],
928928
"difficulty": 6
929+
},
930+
{
931+
"slug": "linked-list",
932+
"name": "Linked List",
933+
"uuid": "ede3b6fb-b399-44a8-8aee-7bdcabec408e",
934+
"practices": [],
935+
"prerequisites": [],
936+
"difficulty": 5
929937
}
930938
]
931939
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Instructions
2+
3+
Your team has decided to use a doubly linked list to represent each train route in the schedule.
4+
Each station along the train's route will be represented by a node in the linked list.
5+
6+
You don't need to worry about arrival and departure times at the stations.
7+
Each station will simply be represented by a number.
8+
9+
Routes can be extended, adding stations to the beginning or end of a route.
10+
They can also be shortened by removing stations from the beginning or the end of a route.
11+
12+
Sometimes a station gets closed down, and in that case the station needs to be removed from the route, even if it is not at the beginning or end of the route.
13+
14+
The size of a route is measured not by how far the train travels, but by how many stations it stops at.
15+
16+
~~~~exercism/note
17+
The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures.
18+
As the name suggests, it is a list of nodes that are linked together.
19+
It is a list of "nodes", where each node links to its neighbor or neighbors.
20+
In a **singly linked list** each node links only to the node that follows it.
21+
In a **doubly linked list** each node links to both the node that comes before, as well as the node that comes after.
22+
23+
If you want to dig deeper into linked lists, check out [this article][intro-linked-list] that explains it using nice drawings.
24+
25+
[intro-linked-list]: https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d
26+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
You are working on a project to develop a train scheduling system for a busy railway network.
4+
5+
You've been asked to develop a prototype for the train routes in the scheduling system.
6+
Each route consists of a sequence of train stations that a given train stops at.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"kmarker1101"
4+
],
5+
"files": {
6+
"solution": [
7+
"linked-list.el"
8+
],
9+
"test": [
10+
"linked-list-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Implement a doubly linked list.",
17+
"source": "Classic computer science topic"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
;;; linked-list.el --- Linked List (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(require 'cl-lib)
9+
10+
11+
(cl-defstruct node
12+
value prev next)
13+
14+
15+
(cl-defstruct dll
16+
head tail)
17+
18+
19+
(defun dll-create ()
20+
(make-dll :head nil :tail nil))
21+
22+
23+
(defun dll-push (list value)
24+
(let ((new-node (make-node :value value :prev (dll-tail list) :next nil)))
25+
(if (dll-tail list)
26+
(setf (node-next (dll-tail list)) new-node)
27+
(setf (dll-head list) new-node))
28+
(setf (dll-tail list) new-node)))
29+
30+
31+
(defun dll-pop (list)
32+
(when (dll-tail list)
33+
(let ((value (node-value (dll-tail list)))
34+
(new-tail (node-prev (dll-tail list))))
35+
(if new-tail
36+
(setf (node-next new-tail) nil)
37+
(setf (dll-head list) nil))
38+
(setf (dll-tail list) new-tail)
39+
value)))
40+
41+
42+
(defun dll-unshift (list value)
43+
(let ((new-node (make-node :value value :prev nil :next (dll-head list))))
44+
(if (dll-head list)
45+
(setf (node-prev (dll-head list)) new-node)
46+
(setf (dll-tail list) new-node))
47+
(setf (dll-head list) new-node)))
48+
49+
50+
(defun dll-shift (list)
51+
(when (dll-head list)
52+
(let ((value (node-value (dll-head list)))
53+
(new-head (node-next (dll-head list))))
54+
(if new-head
55+
(setf (node-prev new-head) nil)
56+
(setf (dll-tail list) nil))
57+
(setf (dll-head list) new-head)
58+
value)))
59+
60+
61+
(defun dll-count (list)
62+
(let ((count 0)
63+
(current (dll-head list)))
64+
(while current
65+
(setq count (1+ count))
66+
(setq current (node-next current)))
67+
count))
68+
69+
70+
(defun dll-delete (list value)
71+
(let ((current (dll-head list)))
72+
(while current
73+
(if (equal (node-value current) value)
74+
(progn
75+
(let ((prev-node (node-prev current))
76+
(next-node (node-next current)))
77+
(if prev-node
78+
(setf (node-next prev-node) next-node)
79+
(setf (dll-head list) next-node))
80+
(if next-node
81+
(setf (node-prev next-node) prev-node)
82+
(setf (dll-tail list) prev-node)))
83+
(setq current nil))
84+
(setq current (node-next current))))))
85+
86+
87+
(provide 'linked-list)
88+
;;; linked-list.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[7f7e3987-b954-41b8-8084-99beca08752c]
13+
description = "pop gets element from the list"
14+
15+
[c3f67e5d-cfa2-4c3e-a18f-7ce999c3c885]
16+
description = "push/pop respectively add/remove at the end of the list"
17+
18+
[00ea24ce-4f5c-4432-abb4-cc6e85462657]
19+
description = "shift gets an element from the list"
20+
21+
[37962ee0-3324-4a29-b588-5a4c861e6564]
22+
description = "shift gets first element from the list"
23+
24+
[30a3586b-e9dc-43fb-9a73-2770cec2c718]
25+
description = "unshift adds element at start of the list"
26+
27+
[042f71e4-a8a7-4cf0-8953-7e4f3a21c42d]
28+
description = "pop, push, shift, and unshift can be used in any order"
29+
30+
[88f65c0c-4532-4093-8295-2384fb2f37df]
31+
description = "count an empty list"
32+
33+
[fc055689-5cbe-4cd9-b994-02e2abbb40a5]
34+
description = "count a list with items"
35+
36+
[8272cef5-130d-40ea-b7f6-5ffd0790d650]
37+
description = "count is correct after mutation"
38+
39+
[229b8f7a-bd8a-4798-b64f-0dc0bb356d95]
40+
description = "popping to empty doesn't break the list"
41+
42+
[4e1948b4-514e-424b-a3cf-a1ebbfa2d1ad]
43+
description = "shifting to empty doesn't break the list"
44+
45+
[e8f7c600-d597-4f79-949d-8ad8bae895a6]
46+
description = "deletes the only element"
47+
48+
[fd65e422-51f3-45c0-9fd0-c33da638f89b]
49+
description = "deletes the element with the specified value from the list"
50+
51+
[59db191a-b17f-4ab7-9c5c-60711ec1d013]
52+
description = "deletes the element with the specified value from the list, re-assigns tail"
53+
54+
[58242222-5d39-415b-951d-8128247f8993]
55+
description = "deletes the element with the specified value from the list, re-assigns head"
56+
57+
[ee3729ee-3405-4bd2-9bad-de0d4aa5d647]
58+
description = "deletes the first of two elements"
59+
60+
[47e3b3b4-b82c-4c23-8c1a-ceb9b17cb9fb]
61+
description = "deletes the second of two elements"
62+
63+
[7b420958-f285-4922-b8f9-10d9dcab5179]
64+
description = "delete does not modify the list if the element is not found"
65+
66+
[7e04828f-6082-44e3-a059-201c63252a76]
67+
description = "deletes only the first occurrence"

0 commit comments

Comments
 (0)