Implementing Pointer Methods in Go: Linked List Manipulation
This challenge focuses on understanding and utilizing pointer methods in Go, a crucial concept for working with data structures like linked lists. You'll be implementing a simple linked list and its associated methods, demonstrating how to modify the list's structure using pointers. This exercise reinforces the importance of pointers for efficient memory management and data manipulation in Go.
Problem Description
You are tasked with creating a basic singly linked list in Go. The linked list will consist of nodes, each containing an integer value and a pointer to the next node in the list. You need to implement the following methods for this linked list:
Append(value int): Adds a new node with the givenvalueto the end of the list.Prepend(value int): Adds a new node with the givenvalueto the beginning of the list.Display(): Prints the values of all nodes in the list, separated by "->". If the list is empty, print "Empty".Get(index int) (int, bool): Returns the value at the givenindexin the list. Returns the value andtrueif the index is valid; otherwise, returns 0 andfalse.
You should define a Node struct and a LinkedList struct. The LinkedList struct should contain a pointer to the head of the list. All methods should be pointer methods on the LinkedList struct.
Examples
Example 1:
Input:
list := LinkedList{}
list.Append(1)
list.Append(2)
list.Append(3)
list.Display()
Output:
1->2->3
Explanation: The list starts empty. Three nodes with values 1, 2, and 3 are appended to the end, resulting in the displayed sequence.
Example 2:
Input:
list := LinkedList{}
list.Prepend(1)
list.Prepend(2)
list.Prepend(3)
list.Display()
Output:
3->2->1
Explanation: The list starts empty. Three nodes with values 3, 2, and 1 are prepended to the beginning, resulting in the displayed sequence.
Example 3:
Input:
list := LinkedList{}
value, ok := list.Get(0)
fmt.Println(value, ok)
Output:
0 false
Explanation: The list is empty. Attempting to get the value at index 0 returns 0 and false, indicating an invalid index.
Example 4:
Input:
list := LinkedList{}
list.Append(1)
list.Append(2)
value, ok := list.Get(1)
fmt.Println(value, ok)
Output:
2 true
Explanation: The list contains two nodes with values 1 and 2. Getting the value at index 1 returns 2 and true, indicating a valid index.
Constraints
- The value stored in each node will be an integer.
- The
indexparameter inGet()will be a non-negative integer. - The linked list can grow to a maximum length of 1000 nodes. (This is for memory management considerations, not a strict requirement for correctness).
- The
Display()method should handle empty lists gracefully.
Notes
- Remember that pointer methods receive a pointer to the receiver struct as their first argument. This allows you to modify the original struct within the method.
- Consider how to handle edge cases like an empty list or an invalid index in
Get(). - Think about how to efficiently traverse the linked list in
Get()to find the node at the specified index. - Pay close attention to memory management when creating and manipulating nodes. While Go has garbage collection, understanding pointer usage is still important.
- The
LinkedListstruct should only contain a pointer to the head node. No other data is needed.