Exercise 11 & Bonus 2

Added Exercise 11 & Bonus 2 to Repo
Added Signature to Exercise 10
This commit is contained in:
2025-05-19 11:34:47 +02:00
parent 49265f1b23
commit fec3fd845f
20 changed files with 640 additions and 19 deletions

View File

@@ -0,0 +1,175 @@
#+title: [Exam 2022.01 (MATH/PHYS/RW)] Circular Linked List
#+author: JirR02
* Circular Linked List
A circular linked list is a variation of a linked list in which the last node points to the first node, completing a full circle of nodes. In other words, this variation of the linked list doesn't have a null element at the end.
Take a brief look at the file Node.hpp.
A Node contains a pointer to the next Node, as well as a value, which is the data stored in the node:
#+BEGIN_src cpp
struct Node {
int value;
Node* next;
}
#+END_src
In our implementation, each Node stores a value of type int. Moreover, we reserve the value 0 to indicate the sentinel Node (see below).
Now open the the file =CircularLinkedList.hpp=.
A =CircularLinkedList= contains a single public member variable =Node* sentinel=. The sentinel node is only there to facilitate the implementation. It is the only node in the list when the list is empty, and it can be identified as being the only node where ~value = 0~.
[[./pictures/1_1.png]]
Notice that, in an empty list, the next pointer of the sentinel node points to the sentinel itself.
A list containing a single value 1 looks like in the following image: the sentinel's next points to the first node of the list, and that node points to the sentinel in a circular fashion.
[[./pictures/one_value.png]]
New values can be inserted in the list either at the beginning or at the end. A node inserted at the beginning comes immediately after the sentinel, while a node inserted at the end comes immediately before the sentinel.
** Your Task
Your task is to implement the following functions in file =tasks.hpp=:
1. =void CircularLinkedList::insertAtBegin(int value)=: given a strictly positive value, inserts the value in a node at the beginning of the list;
1. =void CircularLinkedList::insertAtEnd(int value)=: given a strictly positive value, inserts the value in a node at the end of the list; hint: a sentinel can become a normal node (and vice versa);
1. =void CircularLinkedList::removeValues(int value)=: removes all the nodes containing the provided value; leaves the list in the same state if no node with the provided value is present.
1. =CircularLinkedList::~CircularLinkedList()=: deconstructs the whole list, which includes deallocating all its nodes and the sentinel
** Input Format
Various commands can be entered to test your implementation; each consists of a command followed by appropriate arguments:
- =ib V= : add value V at the beginning
- =ie V= : add value V at the end
- =r V= : removes all nodes with value V
- =s= : show the current status of the list
- =exit= : exits the program without checking for the correct deallocation of all the nodes
- =exitSafe= : exits the program checking the correct deallocation of all the nodes
*** Sample Interactions
#+BEGIN_QUOTE
Copy & Paste: in the ETH exam room use CTRL+SHIFT+C to copy and CTRL+SHIFT+V to paste into the console.
#+END_QUOTE
**** Creating and showing a simple list with two values
Input:
#+BEGIN_src shell
ib 1
ib 2
s
exit
#+END_src
Output:
#+BEGIN_src shell
List=[(2)=>(1)]
#+END_src
**** Inserting two elements and removing one
Input:
#+BEGIN_src shell
ib 1
ie 2
s
r 1
s
exit
#+END_src
Output:
#+BEGIN_src shell
List=[(1)=>(2)]
List=[(2)]
#+END_src
* Solution
#+BEGIN_src cpp
#pragma once
#include "CircularLinkedList.hpp"
#include "Node.hpp"
#include <cassert>
// PRE: value > 0
// POST: insert a new Node containing the value at the beginning of the list
void CircularLinkedList::insertAtBegin(int value) {
assert(value > 0);
Node *ram = new Node(value);
ram->next = sentinel->next;
sentinel->next = ram;
}
// PRE: value > 0
// POST: insert a new Node containing the value at the end of the list
void CircularLinkedList::insertAtEnd(int value) {
assert(value > 0);
Node *last = sentinel->next;
while (last->next != sentinel)
last = last->next;
Node *ram = new Node(value);
last->next = ram;
ram->next = sentinel;
}
// PRE: value > 0
// POST: removes *all* the nodes with the provided value from the list
// and deallocates the memory of the deleted nodes.
void CircularLinkedList::removeValues(int value) {
assert(value > 0);
Node *cur = sentinel;
Node *ram = cur->next;
while (ram != sentinel) {
if (ram->value == value) {
cur->next = ram->next;
delete ram;
ram = cur->next;
} else {
cur = ram;
ram = cur->next;
}
}
}
// POST: Deconstructs the whole list, which includes deallocating
// all its nodes and the sentinel
CircularLinkedList::~CircularLinkedList() {
Node *cur = sentinel;
Node *ram = cur->next;
while (ram != sentinel) {
cur->next = ram->next;
delete ram;
ram = cur->next;
}
delete ram;
}
#+END_src
--------------
Made by JirR02 in Switzerland 🇨🇭

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -0,0 +1,152 @@
#+TITLE: Smart Pointers
#+AUTHOR: JirR02
* Task
The objective of this problem is to implement a reference-count smart pointer, with functionality similar to that of a std::shared_ptr. Smart pointers implement the same functionality as regular pointers, but additionally they automatically take care of deallocating the object they point to when it is no longer needed. Reference-count smart pointers achieve this by allocating and maintaining a counter in memory together with the actual pointed-to object, which represents the number of smart pointers currently referencing the object. Any time a new smart pointer to an object is created or one is destroyed, this counter has to be incremented or decremented, respectively. When the last smart pointer to an object is destroyed, the counter will be decremented to
and the smart pointer will know that the object is no longer referenced; it can then deallocate the object.
The following example illustrates the process:
#+BEGIN_src cpp
Smart a, b, c;
a = Smart(new tracked());
// The smart pointer 'a' now points to the new
// tracked object with a reference count of 1.
c = Smart(new tracked());
// Another smart pointer 'c' now points to the
// second new tracked object with count 1.
b = a;
// 'a' and 'b' now both point to the first object,
// their shared counter is incremented to 2.
c = Smart();
// A null smart pointer is assigned to 'c'; the
// smart pointer previously stored in 'c' is destroyed
// and its counter is decremented. Since the count
// is now zero, the second tracked object is deallocated.
return;
// At the end of the function, the smart
// pointers 'a' and 'b' are both destroyed, their
// counter is decremented twice, and the first
// tracked object is deallocated.
#+END_src
*Locations*: The declarations of the smart pointer class (Smart) and member functions is provided in file =smart.h=. The implementation of member functions should be done in file =smart.cpp=. Smart pointers encapsulate pointers to objects of class tracked, which is declared in file =smart.h=.
*Structure*: In class Smart, member variable =ptr= represents the pointer (potentially shared by several object of class Smart) to the underlying pointed-to object. Member variable =count= represents the (shared) location containing the number of objects of class Smart currently holding the pointed-to object. Alternatively, both pointers may be =nullptr=, which corresponds to the notion of a /null/ smart pointer. A null smart pointer does not manage any memory.
Objects pointed by smart pointers belong to class tracked, which is a linked list node where the next pointer is represented using a smart pointer. In particular, tests will use this structure to build linked lists with shared nodes, and check at the end that everything was correctly deallocated. To that end, every object of class tracked is tracked behind the scenes.
Steps:
1. Implement the default constructor for class =Smart=. The default constructor should create a null smart pointer.
1. Implement the constructor =Smart(tracked* t)=. If =t==nullptr=, this should return a null smart pointer, otherwise, this should create a smart pointer to =t= with a reference count of \(1\). You may assume that =t= points to memory newly allocated by =new= that is not already being managed by another smart pointer.
1. Implement the copy constructor =Smart(const Smart& src)= that returns a new smart pointer to the memory pointed to by =src= and increments the shared reference counter.
1. Implement the destructor =~Smart()=. For non-null pointers, it should decrement the reference counter and deallocate the pointed-to object if the resulting count is zero.
1. Implement the assignment operator =Smart& operator=(const Smart& src)= that creates a copy of the provided smart pointer (incrementing its reference counter) and decrements the counter of the smart pointer of the left hand side of the assignment (and potentially deallocates the memory it points to).
*Optional*: Figure out the situations in which smart pointers are not suitable for memory management, in the sense that they may lead to memory leaks. You may look at the tests which leak memory for inspiration.
*Note*: In order to *debug*, you may execute code that you place in the function =your_own_tests= located in the =smart.cpp= file. The main function has a =switch= statement that chooses between a series of functions for different test scenarios. This =switch= defaults to =your_own_tests=. So if you enter =-1= for =test_id=, =your_own_tests= will be executed.
* Solutions
#+BEGIN_src cpp
#include "smart.h"
#include "tracker.h"
Smart::Smart() {
count = nullptr;
ptr = nullptr;
}
Smart::Smart(tracked *t) /* TODO */ {
if (t == nullptr) {
count = nullptr;
ptr = nullptr;
} else {
ptr = t;
int *counter = new int;
*counter = 1;
count = counter;
}
}
Smart::Smart(const Smart &src) /* TODO */ {
count = src.count;
ptr = src.ptr;
if (count != nullptr)
(*count)++;
}
Smart::~Smart() {
if (count != nullptr) {
(*count)--;
if (*count == 0) {
delete count;
delete ptr;
count = nullptr;
ptr = nullptr;
}
}
}
Smart &Smart::operator=(const Smart &src) {
if (src == *this)
return *this;
if (count != nullptr) {
(*count)--;
if (*count == 0) {
delete count;
delete ptr;
}
}
count = src.count;
ptr = src.ptr;
if (count != nullptr)
(*count)++;
return *this;
}
tracked &Smart::operator*() { return *ptr; }
const tracked &Smart::operator*() const { return *ptr; }
tracked *Smart::operator->() { return ptr; }
const tracked *Smart::operator->() const { return ptr; }
bool Smart::operator==(const Smart &cmp) const { return (ptr == cmp.ptr); }
bool Smart::operator!=(const Smart &cmp) const { return !(*this == cmp); }
int Smart::get_count() const {
if (this->count == nullptr)
return 0;
else
return *(this->count);
}
void your_own_tests() {
// EXTRA TESTS ?
}
// Answer to optional question
/*
FILL IN WITH YOUR ANSWER
*/
#+END_src
--------------
Made by JirR02 in Switzerland 🇨🇭

View File

@@ -0,0 +1,98 @@
#+TITLE: [Hidden tests][Exam 2019.02 RW] Sorted List
#+AUTHOR: JirR02
* Sorted Linked List
In this task you complete the functions =add= and =remove= of a singly linked sorted (in ascending order) list.
One invariant of the sorted linked list is the following: for subsequent nodes =n= and =next= it holds \(n.value \leq next.value \).
[[./pictures/SortedLinkedList.png]]
*** Chosen memory management
We do not want to deal with the memory management by ourselves. Therefore we use =std::shared_ptr= instead of normal pointers. Behind the scenes, shared pointers contain a counter that counts the number of references to the pointer. As soon as this counter reaches the value zero, the object behind the pointer is removed, i.e. =delete= is called automatically.
Otherwise, a shared pointer has nearly the same functionality as a normal pointer. For this exercise:
- Where we previously used the pointer type node*, we now use =std::shared_ptr<node>=
- Members of a shared pointer are accessed like on a normal pointer: using the =->= operator.
- Assignments and comparisons work in the same way as for normal pointers.
- Only allocation, like new node(=value=), is replaced by =std::make_shared<node>(value)=.
- Calls to delete can be omitted completely. Shared pointers call delete automatically when required.
Consider for example the finished implementation of the =print= function and the already implemented part of the add function.
** Tasks
1. Complete the function =void sorted_list::add(int value)= such that a new node with the provided value is inserted into the linked list. The list needs to remain sorted in ascending order. Several nodes with the same value may be contained. The order of the nodes with same value is unimportant.
1. Complete the function bool =sorted_list::remove(int value)= such that a node with the given value is removed from the list. If no node with this value exists, the function should return =false=. Otherwise one (and only one) element is removed and the function returns =true=. *Hint*: help yourself with copy&paste from the previous function add.
** The command interface
The main function implements the command interface that understands the following commands:
- =add {int}= ("add" followed by a sequence of integers) -- calls add on a sorted list for all the integers provided. Example: add 1 2 3 adds numbers 1, 2 and 3 to the list.
- =remove {int}= ("remove" followed by a sequence of integers) -- calls remove on a sorted list for all integers provided. Example: remove 3 6 removes an element with value 3 and an element with value 6 from the list (as far as available).
- =print= -- calls print on the linked list.
- =end= -- ends the program.
* Solution
#+BEGIN_src cpp
#include "sorted_list.h"
// post: add a new node with value to the sorted linked list
// several nodes with the same value are possible
void sorted_list::add(int value) {
// this creates a new node in dynamic memory, wrapped in a shared pointer
// analogously to new node(value) for normal pointers
node_ptr newNode = std::make_shared<node>(value);
node_ptr prev = nullptr;
node_ptr n = first;
while (n != nullptr && n->value < value) {
prev = n;
n = n->next;
}
if (prev == nullptr) {
newNode->next = first;
first = newNode;
} else {
newNode->next = n;
prev->next = newNode;
}
}
// post: remove the first node which holds 'value', if any.
// if there is no such node, return false
// otherwise return true
bool sorted_list::remove(int value) {
node_ptr searchNode = std::make_shared<node>(value);
node_ptr prev = nullptr;
node_ptr n = first;
while (n != nullptr && n->value != searchNode->value) {
prev = n;
n = n->next;
}
if (n != nullptr) {
if (prev != nullptr) {
prev->next = n->next;
return true;
} else {
first = n->next;
return true;
}
} else {
return false;
}
}
#+END_src
--------------
Made by JirR02 in Switzerland 🇨🇭

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB