Browse Source

adding Go linked list

Charles Reid 5 years ago
  1. 35
  2. 13


@ -0,0 +1,35 @@
// Start by defining a node.
// The data type is interface{}, which means we can define a type as a struct, just like we do here, and pass it as a data type.
// A linked list node is a burrito for data.
type Node struct {
data interface{}
next *Node
// Define an Append function for the Node struct.
// This appends some data to the linked list, right after node n (not at the end).
func ( n *Node ) Append(data interface{}) *Node {
// If we are on the last node at the end of the list:
if n.Next == nil {
// The next item in the list is the data we are appending
n.Next = &Node{ data, nil }
} else {
// The next item in the list is the data we are appending,
// but we also link it to the (old) next item in the list,
// so we don't lose that data forever.
tmp := &Node{data,}
n.Next = tmp
// Now return a pointer that points at the new node.
return n.Next
// Define a Print function for the Node struct.
// This prints the data.
func (n *Node) String() string {
return fmt.Sprintf("Node: %v",


@ -0,0 +1,13 @@
# Go Data Structures
## Linked List
Go allows you to create pointers to structs (equivalent to classes in Go).
The node is defined to take a generic "interface" type (meaning, we can create a struct as a type, and use that as the linked list element data).
We then define some functions on the Node struct, including an Append() method and a String() method.
This class illustrates how we can use structs as slimmed down objects, and use function-like syntax with those structs to define methods on that struct's data.