You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
// ====================================================
|
|
|
|
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
|
|
|
|
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
|
|
|
|
// and you are welcome to redistribute it under certain conditions; See
|
|
|
|
// file LICENSE, which is part of this source code package, for details.
|
|
|
|
// ====================================================
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
data int
|
|
|
|
next *Node
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns an initialized list
|
|
|
|
func (n *Node) Init() *Node {
|
|
|
|
n.data = -1
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns an new list
|
|
|
|
func New() *Node {
|
|
|
|
return new(Node).Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the first node in list
|
|
|
|
func (n *Node) Next() *Node {
|
|
|
|
return n.next
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the last node in list if exist, otherwise returns current
|
|
|
|
func (n *Node) Back() *Node {
|
|
|
|
current := n.next
|
|
|
|
for current != nil && current.next != nil {
|
|
|
|
current = current.next
|
|
|
|
}
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
|
|
|
//This function prints contents of linked list starting from the given node
|
|
|
|
func printList(n *Node){
|
|
|
|
for n != nil {
|
|
|
|
fmt.Println(n.data)
|
|
|
|
n = n.next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
//To allocate dynamically a new Node in C language : head = (struct Node*) malloc(sizeof(struct Node));
|
|
|
|
head := New()
|
|
|
|
second := New()
|
|
|
|
third := New()
|
|
|
|
|
|
|
|
//Assign data in first node
|
|
|
|
head.data = 1
|
|
|
|
//Link first node with second node
|
|
|
|
head.next = second
|
|
|
|
|
|
|
|
second.data = 2
|
|
|
|
second.next = third
|
|
|
|
|
|
|
|
third.data = 3
|
|
|
|
third.next = nil
|
|
|
|
|
|
|
|
printList(head)
|
|
|
|
}
|