Skip to content

Commit f6a34aa

Browse files
authored
Add linked list practice exercise (#567)
1 parent e8795b6 commit f6a34aa

14 files changed

+18433
-0
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,20 @@
994994
"strings"
995995
]
996996
},
997+
{
998+
"slug": "linked-list",
999+
"name": "Linked List ",
1000+
"uuid": "6f6c55dd-db6b-40ca-96f2-d9b7232dcbb0",
1001+
"practices": [],
1002+
"prerequisites":[],
1003+
"difficulty": 6,
1004+
"topics": [
1005+
"classes",
1006+
"conditionals",
1007+
"loops",
1008+
"pointers"
1009+
]
1010+
},
9971011
{
9981012
"slug": "simple-linked-list",
9991013
"name": "Simple linked list",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Instructions append
2+
3+
## How this Exercise is Structured on the C++ Track
4+
5+
While linked lists can be implemented in a variety of ways with a variety of underlying data structures, we ask here that you implement your linked list in an OOP fashion.
6+
7+
In the `linked_list_test.cpp` file, you will see that a [__templated__][template classes] `List` class is called.
8+
You are expected to write this class with the following member functions:
9+
10+
- `push` adds an element to the end of the list,
11+
- `pop` removes and returns the last element of the list,
12+
- `shift` removes and returns the first element of the list,
13+
- `unshift` adds an element to the start of the list, and
14+
- `count` returns the total number of elements in the current list.
15+
16+
Finally, we would like you to implement `erase` in addition to the methods outlined above.
17+
`erase` will take one argument, which is the value to be removed from the linked list.
18+
If the value appears more than once, only the **first** occurrence should be removed.
19+
It should return if an element was deleted or not.
20+
21+
Although it is not tested, you might want to raise an exception if `pop` and `shift` are called on an empty `List`.
22+
23+
[template classes]: https://www.learncpp.com/cpp-tutorial/template-classes/
Lines changed: 26 additions & 0 deletions
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+
~~~~
Lines changed: 6 additions & 0 deletions
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.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"vaeng"
4+
],
5+
"files": {
6+
"solution": [
7+
"linked_list.cpp",
8+
"linked_list.h"
9+
],
10+
"test": [
11+
"linked_list_test.cpp"
12+
],
13+
"example": [
14+
".meta/example.cpp",
15+
".meta/example.h"
16+
]
17+
},
18+
"blurb": "Implement a doubly linked list.",
19+
"source": "Classic computer science topic"
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "linked_list.h"
2+
3+
namespace linked_list {
4+
5+
} // namespace linked_list
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <stdexcept>
5+
6+
namespace linked_list {
7+
template <typename T>
8+
struct Node {
9+
Node() {}
10+
Node(std::shared_ptr<Node<T>> prev, std::shared_ptr<Node<T>> next, T value)
11+
: previous(prev), next(next), data(value) {}
12+
std::shared_ptr<Node<T>> previous;
13+
std::shared_ptr<Node<T>> next;
14+
T data;
15+
};
16+
17+
template <typename T>
18+
class List {
19+
public:
20+
List() {
21+
sentinel = std::make_shared<Node<T>>();
22+
sentinel->next = sentinel;
23+
sentinel->previous = sentinel;
24+
}
25+
26+
void push(T entry) { insert(sentinel->previous, sentinel, entry); }
27+
28+
void unshift(T entry) { insert(sentinel, sentinel->next, entry); }
29+
30+
T pop() { return remove(sentinel->previous); }
31+
32+
T shift() { return remove(sentinel->next); }
33+
34+
bool erase(T entry) {
35+
auto ptr = sentinel->next;
36+
while (ptr->data != entry) {
37+
if (ptr == sentinel) return false;
38+
ptr = ptr->next;
39+
}
40+
remove(ptr);
41+
return true;
42+
}
43+
44+
size_t count() { return current_size; }
45+
46+
private:
47+
std::shared_ptr<Node<T>> sentinel;
48+
size_t current_size{};
49+
50+
T remove(std::shared_ptr<Node<T>> to_be_deleted) {
51+
if (current_size < 1)
52+
throw std::runtime_error("Cannot remove elements from empty list.");
53+
T data = to_be_deleted->data;
54+
to_be_deleted->next->previous = to_be_deleted->previous;
55+
to_be_deleted->previous->next = to_be_deleted->next;
56+
--current_size;
57+
return data;
58+
}
59+
60+
void insert(std::shared_ptr<Node<T>> prev, std::shared_ptr<Node<T>> next,
61+
T value) {
62+
auto new_element = std::make_shared<Node<T>>(prev, next, value);
63+
prev->next = new_element;
64+
next->previous = new_element;
65+
++current_size;
66+
}
67+
};
68+
69+
} // namespace linked_list
Lines changed: 67 additions & 0 deletions
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"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Get the exercise name from the current directory
2+
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
3+
4+
# Basic CMake project
5+
cmake_minimum_required(VERSION 3.5.1)
6+
7+
# Name the project after the exercise
8+
project(${exercise} CXX)
9+
10+
# Get a source filename from the exercise name by replacing -'s with _'s
11+
string(REPLACE "-" "_" file ${exercise})
12+
13+
# Implementation could be only a header
14+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
15+
set(exercise_cpp ${file}.cpp)
16+
else()
17+
set(exercise_cpp "")
18+
endif()
19+
20+
# Use the common Catch library?
21+
if(EXERCISM_COMMON_CATCH)
22+
# For Exercism track development only
23+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
24+
elseif(EXERCISM_TEST_SUITE)
25+
# The Exercism test suite is being run, the Docker image already
26+
# includes a pre-built version of Catch.
27+
find_package(Catch2 REQUIRED)
28+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
29+
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
30+
# When Catch is installed system wide we need to include a different
31+
# header, we need this define to use the correct one.
32+
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
33+
else()
34+
# Build executable from sources and headers
35+
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
36+
endif()
37+
38+
set_target_properties(${exercise} PROPERTIES
39+
CXX_STANDARD 17
40+
CXX_STANDARD_REQUIRED OFF
41+
CXX_EXTENSIONS OFF
42+
)
43+
44+
set(CMAKE_BUILD_TYPE Debug)
45+
46+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
47+
set_target_properties(${exercise} PROPERTIES
48+
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
49+
)
50+
endif()
51+
52+
# Configure to run all the tests?
53+
if(${EXERCISM_RUN_ALL_TESTS})
54+
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
55+
endif()
56+
57+
# Tell MSVC not to warn us about unchecked iterators in debug builds
58+
if(${MSVC})
59+
set_target_properties(${exercise} PROPERTIES
60+
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
61+
endif()
62+
63+
# Run the tests on every build
64+
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "linked_list.h"
2+
3+
namespace linked_list {
4+
5+
} // namespace linked_list
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
namespace linked_list {
4+
5+
} // namespace linked_list

0 commit comments

Comments
 (0)