课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Introduction to a doubly linked list challenge - Go教程
课程: Advanced Go Programming: Data Structures, Code Architecture, and Testing
Introduction to a doubly linked list challenge
(upbeat music) - [Instructor] You'll use all the knowledge we have learned in this section to solve your first challenge, and you'll do that by implementing a doubly linked list. As we've seen, a linked list is a collection of elements that reference each other. A doubly linked list is a specialized case that allows navigation in both directions. However, the list you'll be implementing is not circular, so the ends of the list reference null. Nodes have references to both the next and the previous element, making it easy to traverse the list in either direction. Doubly linked lists are useful for a variety of scenarios, caching functionality, playlists, or undo/redo functionality. They also give us a great opportunity to test our ability to implement our own data structures. I've written some code for us to get us started. Again, I've used generics to make our data structures work for any type. On lines 16 to 19, we declare a node struct which contains references to both the next and the previous node. It also holds a value of type T. On lines 21 to 25, we create a doubly linked list struct which contains two node pointer references to head and tail, as well as a size field. The doubly linked list also takes a type parameter T. Your challenge is to implement the add function, which takes in an index and value to add to the linked list. Right now, this method simply returns a not implemented error, but should return an index exceeds list size error in the case that the index is larger than the current list length. In order to test our list management and node references, I have implemented the print forward and print reverse methods which traverse the list and returned the formatted values. These are implemented at the bottom of this file on lines 46 to 74. You can have a look at them, but they're just helper methods for the purposes of assertion. Take your time to solve this challenge and join me in the next video to see my solution. Good luck.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。