Wednesday, 28 October 2020

Chapter 17 // Exercise 14 - Principles & Practice Using C++

  In this exercise I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.

Chapter 17 // Exercise 14

Could the "list of gods" example from Section 17.10.1 have been written using a singly-linked list; that is,could we have left the prev member out of Link? Why might we want to do that? For what kind of examples would it make sense to use a singly-linked list? Re-implement that example using only a singly-linked list.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2017/Exercise%2014

Eurgh why. Without prev it would be very hard to find the start of the list. The pointer can be changed to point at something else when an element is inserted/removed. We would need to store a pointer to the first value.

Insertion would be trickier as well as you would have to start from a, advance n times to the insertion point so you know the previous then assign the new successors. Just use a vector.

I tried googling best use cases for a Singly linked list and I couldn't really find any.

And with that, another chapter bites the dust. This was a good chapter though. I have so many notes on pointers and memory allocation. I'm also halfway through the book! Just 590 pages out of 1216.

No comments:

Post a Comment