diff --git a/linked-list-1-introduction/linked-list-1-introduction.go b/linked-list-1-introduction/linked-list-1-introduction.go index 3cd113f..4c749cc 100644 --- a/linked-list-1-introduction/linked-list-1-introduction.go +++ b/linked-list-1-introduction/linked-list-1-introduction.go @@ -48,7 +48,6 @@ func printList(n *Node){ } func main() { - //To allocate dynamically a new Node in C language : head = (struct Node*) malloc(sizeof(struct Node)); head := New() second := New() diff --git a/linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go b/linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go index 7b7775d..01b9c06 100644 --- a/linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go +++ b/linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go @@ -113,7 +113,6 @@ func printList(n *Node){ } func main() { - //Start with the empty list head := New() diff --git a/linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go b/linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go index 83e452f..4129c40 100644 --- a/linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go +++ b/linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go @@ -119,9 +119,6 @@ func DeleteNodeWithPosition(head_ref **Node, delete_position int){ //Unlink the deleted node from list temp.next = next - - - } //This function prints contents of linked list starting from the given node @@ -133,7 +130,6 @@ func printList(n *Node){ } func main() { - //Start with the empty list head := New() diff --git a/linked-list-find-length/linked-list-find-length.go b/linked-list-find-length/linked-list-find-length.go index e69de29..6b5dae9 100644 --- a/linked-list-find-length/linked-list-find-length.go +++ b/linked-list-find-length/linked-list-find-length.go @@ -0,0 +1,94 @@ +// ==================================================== +// 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 +} + +func Push(head_ref **Node, new_data int) { + //new_node := Node{data: new_data, next: (*head_ref)} + //*head_ref = new_node + + //1. Allocate new node + new_node := New() + + //2. Put in the data + new_node.data = new_data + + //3. Make next of new node as head + new_node.next = (*head_ref) + + //4. Move the head to point to new_node + *head_ref = new_node +} + +func GetCount(head *Node) int { + var count int = 0 + current := head + + for(current != nil){ + count++ + current = current.next + } + + return count +} + +//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() { + //Start with the empty list + head := New() + + Push(&head, 1) + + Push(&head, 3) + + Push(&head, 1) + + Push(&head, 2) + + Push(&head, 1) + + fmt.Printf("Count of Nodes is %d", GetCount(head)) +} \ No newline at end of file