From the course: Advanced Go Programming: Data Structures, Code Architecture, and Testing

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Solution: Doubly linked list

Solution: Doubly linked list

(upbeat music) - [Instructor] Review your solution as you have a look at my implementation of the Add function of the doubly linked list. There are four base cases to consider. Adding to an empty list. Adding an element to the head of the list. Adding an element to the tail of the list. And finally, adding an element at a given index. The code begins by saving the current size of the list to a variable. As always, our error case comes first. On lines 31 to 33, the code checks whether the index exceeds the size of the list and returns an error if it does. Once the error case is handled, we know we'll add the element to the list. On lines 34 to 37, we create an element with the given value and increment the size of the linked list. As there is no underlying array that contains the elements of the linked list, it's best to manage the size of the list manually. On lines 40 to 43, handle the case of the empty list by…

Contents