Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,21 @@
"loops",
"strings"
]
},
{
"slug": "simple-linked-list",
"name": "Simple linked list",
"uuid": "ffccabe9-9779-4914-9cff-c6f5696c8afe",
"practices": ["pointers"],
"prerequisites":[],
"difficulty": 4,
"status": "beta",
"topics": [
"classes",
"conditionals",
"loops",
"pointers"
]
}
],
"foregone": [
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/simple-linked-list/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Implementation Hints

We have provided the general structure of a `List` class for you.
It has the private variables `head` of type `Element*` and `current_size` of type `size_t` that you can use.

The `Element` class was given as well, it has two public variables: `data` of type `int` and `next` of type `Element*`.

You can see the details in `simple_linked_list.h`. You do not have to change that file, but you can if it fits your needs.

The tests use the functions as they are supplied in `simple_linked_list.cpp`, don't change their signature. You can add more functions and members if you want to.

## Can I use smart pointers?

Although the header-file includes raw pointers, you are free to chose a different implementation.
15 changes: 15 additions & 0 deletions exercises/practice/simple-linked-list/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Instructions

Write a simple linked list implementation that uses Elements and a List.

The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures.
They're pervasive in functional programming languages, such as Clojure, Erlang, or Haskell, but far less common in imperative languages such as Ruby or Python.

The simplest kind of linked list is a singly linked list.
Each element in the list contains data and a "next" field pointing to the next element in the list of elements.

This variant of linked lists is often used to represent sequences or push-down stacks (also called a LIFO stack; Last In, First Out).

As a first take, lets create a singly linked list to contain integers, and provide functions to reverse a linked list.

When implementing this in a language with built-in linked lists, implement your own abstract data type.
21 changes: 21 additions & 0 deletions exercises/practice/simple-linked-list/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"vaeng"
],
"files": {
"solution": [
"simple_linked_list.cpp",
"simple_linked_list.h"
],
"test": [
"simple_linked_list_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Write a simple linked list implementation that uses Elements and a List.",
"source": "Inspired by 'Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby', singly linked-lists.",
"source_url": "https://web.archive.org/web/20160731005714/http://brpreiss.com/books/opus8/html/page96.html"
}
46 changes: 46 additions & 0 deletions exercises/practice/simple-linked-list/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "simple_linked_list.h"

#include <stdexcept>

namespace simple_linked_list {
List::~List() {
while (head != nullptr) {
Element* next = head->next;
delete head;
head = next;
}
}

size_t List::size() { return current_size; }

void List::push(int entry) {
auto element = new Element{entry};
element->next = head;
head = element;
current_size++;
}

int List::pop() {
if (head == nullptr) {
throw std::runtime_error("Cannot pop from empty list.");
} else {
auto element = head;
head = head->next;
int data = element->data;
delete element;
current_size--;
return data;
}
}

void List::reverse() {
Element* new_head = nullptr;
while (head != nullptr) {
auto temp = new_head;
new_head = head;
head = head->next;
new_head->next = temp;
}
head = new_head;
}
} // namespace simple_linked_list
37 changes: 37 additions & 0 deletions exercises/practice/simple-linked-list/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#if !defined(SIMPLE_LINKED_LIST_H)
#define SIMPLE_LINKED_LIST_H

#include <cstddef>

namespace simple_linked_list {

class Element {
public:
Element(int data) : data{data} {};
int data{};
Element* next{nullptr};
};

class List {
public:
List() = default;
~List();

List(const List&) = delete;
List& operator=(const List&) = delete;
List(const List&&) = delete;
List& operator=(const List&&) = delete;

size_t size();
void push(int enty);
int pop();
void reverse();

private:
Element* head{nullptr};
size_t current_size{0};
};

} // namespace simple_linked_list

#endif
64 changes: 64 additions & 0 deletions exercises/practice/simple-linked-list/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
32 changes: 32 additions & 0 deletions exercises/practice/simple-linked-list/simple_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "simple_linked_list.h"

#include <stdexcept>

namespace simple_linked_list {

size_t List::size() {
// TODO: Return the correct size of the list.
return 0;
}

void List::push(int entry) {
// TODO: Implement a function that pushes an Element with `entry` as data to
// the front of the list.
}

int List::pop() {
// TODO: Implement a function that returns the data value of the first
// element in the list then discard that element.
return 0;
}

void List::reverse() {
// TODO: Implement a function to reverse the order of the elements in the
// list.
}

List::~List() {
// TODO: Ensure that all resources are freed on destruction
}

} // namespace simple_linked_list
40 changes: 40 additions & 0 deletions exercises/practice/simple-linked-list/simple_linked_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#if !defined(SIMPLE_LINKED_LIST_H)
#define SIMPLE_LINKED_LIST_H

#include <cstddef>

namespace simple_linked_list {

class Element {
public:
Element(int data) : data{data} {};
int data{};
Element* next{nullptr};
};

class List {
public:
List() = default;
~List();

// Moving and copying is not needed to solve the exercise.
// If you want to change these, make sure to correctly
// free / move / copy the allocated resources.
List(const List&) = delete;
List& operator=(const List&) = delete;
List(const List&&) = delete;
List& operator=(const List&&) = delete;

size_t size();
void push(int enty);
int pop();
void reverse();

private:
Element* head{nullptr};
size_t current_size{0};
};

} // namespace simple_linked_list

#endif
Loading