Recap - Linked List Builder
Part of the Object Oriented Programming section of Coddy's GO journey — lesson 24 of 107.
Challenge
EasyLet's build a linked list from scratch! You'll create a data structure where each node points to the next, forming a chain of connected values in memory.
You'll organize your code across two files:
linkedlist.go: Define aNodestruct withValue(int) andNext(*Node) fields. Create aLinkedListstruct that holds a pointer to theHeadnode. Implement these methods on*LinkedList:Append- adds a new node with the given value to the end of the listPrepend- adds a new node with the given value to the beginning of the listPrint- traverses the list and prints all values separated by->, ending withnil
main.go: Read values from input, build a linked list by appending and prepending nodes, then print the final list structure.
The following inputs will be provided:
- Line 1: First value to append
- Line 2: Second value to append
- Line 3: Value to prepend
Your Print method should output the list in this format:
[value1] -> [value2] -> [value3] -> nilFor example, given 10, 20, and 5, your output should be:
5 -> 10 -> 20 -> nilThe Append method needs to traverse to the end of the list (where Next is nil) before adding the new node. The Prepend method should create a new node, point its Next to the current head, then update the head to the new node. Remember that an empty list has a nil head, so handle that case in your Append method.
Try it yourself
package main
import "fmt"
func main() {
// Read input values
var val1, val2, val3 int
fmt.Scanln(&val1)
fmt.Scanln(&val2)
fmt.Scanln(&val3)
// Create a new linked list
list := &LinkedList{}
// TODO: Use Append to add val1 and val2 to the list
// TODO: Use Prepend to add val3 to the beginning of the list
// TODO: Print the final list structure
}
All lessons in Object Oriented Programming
1Fundamentals of Go OOP
External FilesGo Workspace & ModulesPackages & ImportsExported vs Unexported NamesIntroduction to OOP in GoStructs as ClassesDefining Methods on StructsPointer vs Value ReceiversStruct InitializationConstructor FunctionsRecap - Simple Calculator4Interfaces
Introduction to InterfacesImplicit ImplementationInterface as ContractEmpty Interface (any)Type AssertionType SwitchInterface CompositionStringer & Error InterfacesRecap - Shape Calculator7Encapsulation
Exported vs Unexported FieldsPackage-Level EncapsulationGetter & Setter MethodsInformation Hiding in GoRecap - Student Records10Generics (Go 1.18+)
Introduction to GenericsType ParametersType ConstraintsGeneric StructsGeneric Methods WorkaroundRecap - Generic Collection2Types & Structs Deep Dive
Basic & Composite TypesCustom Type DefinitionsStruct TagsAnonymous StructsNested StructsZero Values & DefaultsRecap - Contact Book5Composition Over Inheritance
Why Go Has No InheritanceStruct Embedding BasicsMethod PromotionEmbedding Multiple StructsEmbedding vs AggregationShadowing Embedded MethodsRecap - Employee Hierarchy8Error Handling & OOP
The error InterfaceCustom Error TypesError Wrapping (fmt.Errorf)Sentinel Errorserrors.Is() and errors.As()Panic, Defer, and RecoverRecap - File Parser3Pointers & Memory
Pointer Basics in GoPointers to StructsPass by Value vs ReferenceThe new() FunctionGarbage Collection in GoRecap - Linked List Builder6Polymorphism in Go
Polymorphism via InterfacesDuck Typing in GoInterface Satisfaction RulesPolymorphic CollectionsDependency InjectionRecap - Payment Processor9Concurrency & OOP
Goroutines BasicsChannels & CommunicationBuffered vs Unbuffered ChanSelect Statementsync.Mutex & sync.RWMutexsync.WaitGroupThread-Safe Struct DesignRecap - Worker Pool