single linked list Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/single-linked-list/ Sun, 02 May 2021 05:02:41 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 single linked list Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/single-linked-list/ 32 32 159787465 Convert singly linked list into a circular linked list using Go (Golang) https://vikasboss.github.io/single-linked-list-circular-golang/ https://vikasboss.github.io/single-linked-list-circular-golang/#respond Sun, 02 May 2021 05:01:57 +0000 https://vikasboss.github.io/?p=5570 Overview Convert singly linked list into a circular linked list using Golang Input singly linked list:  Output list that should be a circular linked list: Program There are two important methods in...

The post Convert singly linked list into a circular linked list using Go (Golang) appeared first on Welcome To Golang By Example.

]]>

Overview

Convert singly linked list into a circular linked list using Golang

Input singly linked list: 

"A" -> "B" -> "C" -> "D"

Output list that should be a circular linked list:

"A" -> "B" -> "C" -> "D"
 ^                               |
|____________________|

Program

There are two important methods in this program

  • ToCircular – converts a single linked list to a circular linked list
  • IsCircular – checks if a linked list is a circular linked list
package main

import "fmt"

type node struct {
	data string
	next *node
}

type singlyLinkedList struct {
	len  int
	head *node
}

func initList() *singlyLinkedList {
	return &singlyLinkedList{}
}

func (s *singlyLinkedList) AddFront(data string) {
	node := &node{
		data: data,
	}

	if s.head == nil {
		s.head = node
	} else {
		node.next = s.head
		s.head = node
	}
	s.len++
	return
}

func (s *singlyLinkedList) Traverse() error {
	if s.head == nil {
		return fmt.Errorf("TraverseError: List is empty")
	}
	current := s.head
	for current != nil {
		fmt.Println(current.data)
		current = current.next
	}
	return nil
}

//Function to convert singly linked list to circular linked list
func (s *singlyLinkedList) ToCircular() {
	current := s.head
	for current.next != nil {
		current = current.next
	}
	current.next = s.head
}

func (s *singlyLinkedList) IsCircular() bool {
	if s.head == nil {
		return true
	}
	current := s.head.next
	for current.next != nil && current != s.head {
		current = current.next
	}
	return current == s.head
}

func main() {
	singleList := initList()
	fmt.Printf("AddFront: D\n")
	singleList.AddFront("D")
	fmt.Printf("AddFront: C\n")
	singleList.AddFront("C")
	fmt.Printf("AddFront: B\n")
	singleList.AddFront("B")
	fmt.Printf("AddFront: A\n")
	singleList.AddFront("A")

	err := singleList.Traverse()
	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Printf("Size: %d\n", singleList.len)
	singleList.ToCircular()

	isCircular := singleList.IsCircular()
	fmt.Printf("Is Circular: %t\n", isCircular)
}

Output

AddFront: D
AddFront: C
AddFront: B
AddFront: A
A
B
C
D
Size: 4
Is Circular: true

The post Convert singly linked list into a circular linked list using Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/single-linked-list-circular-golang/feed/ 0 5570
Convert singly linked list into an array using Go (Golang) https://vikasboss.github.io/linked-list-array-go/ https://vikasboss.github.io/linked-list-array-go/#comments Sun, 02 May 2021 04:52:38 +0000 https://vikasboss.github.io/?p=5564 Overview Convert singly linked list into an array using Golang Input singly linked list:  Output array:  Program Output:

The post Convert singly linked list into an array using Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Single List to Array Image

Overview

Convert singly linked list into an array using Golang

Input singly linked list: 

 "A" -> "B" -> "C" -> "D"

Output array: 

["A", "B", "C", "D"]

Program

package main

import "fmt"

type node struct {
	data string
	next *node
}

type singlyLinkedList struct {
	len  int
	head *node
}

func initList() *singlyLinkedList {
	return &singlyLinkedList{}
}

func (s *singlyLinkedList) AddFront(data string) {
	node := &node{
		data: data,
	}

	if s.head == nil {
		s.head = node
	} else {
		node.next = s.head
		s.head = node
	}
	s.len++
	return
}

func (s *singlyLinkedList) Size() int {
	return s.len
}

func (s *singlyLinkedList) Traverse() error {
	if s.head == nil {
		return fmt.Errorf("TraverseError: List is empty")
	}
	current := s.head
	for current != nil {
		fmt.Println(current.data)
		current = current.next
	}
	return nil
}

//Function to convert singly linked list to an array
func (s *singlyLinkedList) ToArray() []string {
	var myarr []string
	current := s.head
	for current.next != nil {
		fmt.Printf("\nAdding Element to array: %s", current.data)
		myarr = append(myarr, current.data)
		current = current.next
	}
	fmt.Printf("\nAdding Element to array: %s", current.data)
	myarr = append(myarr, current.data)
	return myarr
}

func main() {
	singleList := initList()
	fmt.Printf("AddFront: D\n")
	singleList.AddFront("D")
	fmt.Printf("AddFront: C\n")
	singleList.AddFront("C")
	fmt.Printf("AddFront: B\n")
	singleList.AddFront("B")
	fmt.Printf("AddFront: A\n")
	singleList.AddFront("A")

	fmt.Printf("Size: %d\n", singleList.Size())

	err := singleList.Traverse()
	if err != nil {
		fmt.Println(err.Error())
	}

	myarr := singleList.ToArray()
	fmt.Println()
	fmt.Println(myarr)
}

Output:

AddFront: D
AddFront: C
AddFront: B
AddFront: A
Size: 4
A
B
C
D

Adding Element to array: A
Adding Element to array: B
Adding Element to array: C
Adding Element to array: D
[A B C D]

The post Convert singly linked list into an array using Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/linked-list-array-go/feed/ 1 5564
Linked List in Golang https://vikasboss.github.io/singly-linked-list-in-golang/ https://vikasboss.github.io/singly-linked-list-in-golang/#comments Thu, 12 Dec 2019 17:32:27 +0000 https://vikasboss.github.io/?p=832 Singly-linked list is a simple kind of linked list that allows traversal in one direction i.e forward. Each node in the linked list contains the data part and the pointer to the...

The post Linked List in Golang appeared first on Welcome To Golang By Example.

]]>
Singly-linked list is a simple kind of linked list that allows traversal in one direction i.e forward. Each node in the linked list contains the data part and the pointer to the next node in the linked list.

The linked list implemented below supports the following operations

  1. AddFront
  2. AddBack
  3. RemoveFront
  4. RemoveBack
  5. Traverse
  6. Front
  7. Size
package main

import "fmt"

type ele struct {
    name string
    next *ele
}

type singleList struct {
    len  int
    head *ele
}

func initList() *singleList {
    return &singleList{}
}

func (s *singleList) AddFront(name string) {
    ele := &ele{
        name: name,
    }
    if s.head == nil {
        s.head = ele
    } else {
        ele.next = s.head
        s.head = ele
    }
    s.len++
    return
}

func (s *singleList) AddBack(name string) {
    ele := &ele{
        name: name,
    }
    if s.head == nil {
        s.head = ele
    } else {
        current := s.head
        for current.next != nil {
            current = current.next
        }
        current.next = ele
    }
    s.len++
    return
}

func (s *singleList) RemoveFront() error {
    if s.head == nil {
        return fmt.Errorf("List is empty")
    }
    s.head = s.head.next
    s.len--
    return nil
}

func (s *singleList) RemoveBack() error {
    if s.head == nil {
        return fmt.Errorf("removeBack: List is empty")
    }
    var prev *ele
    current := s.head
    for current.next != nil {
        prev = current
        current = current.next
    }
    if prev != nil {
        prev.next = nil
    } else {
        s.head = nil
    }
    s.len--
    return nil
}

func (s *singleList) Front() (string, error) {
    if s.head == nil {
        return "", fmt.Errorf("Single List is empty")
    }
    return s.head.name, nil
}

func (s *singleList) Size() int {
    return s.len
}

func (s *singleList) Traverse() error {
    if s.head == nil {
        return fmt.Errorf("TranverseError: List is empty")
    }
    current := s.head
    for current != nil {
        fmt.Println(current.name)
        current = current.next
    }
    return nil
}

func main() {
     singleList := initList()
    fmt.Printf("AddFront: A\n")
    singleList.AddFront("A")
    fmt.Printf("AddFront: B\n")
    singleList.AddFront("B")
    fmt.Printf("AddBack: C\n")
    singleList.AddBack("C")

    fmt.Printf("Size: %d\n", singleList.Size())
   
    err := singleList.Traverse()
    if err != nil {
        fmt.Println(err.Error())
    }
    
    fmt.Printf("RemoveFront\n")
    err = singleList.RemoveFront()
    if err != nil {
        fmt.Printf("RemoveFront Error: %s\n", err.Error())
    }
    
    fmt.Printf("RemoveBack\n")
    err = singleList.RemoveBack()
    if err != nil {
        fmt.Printf("RemoveBack Error: %s\n", err.Error())
    }
    
    fmt.Printf("RemoveBack\n")
    err = singleList.RemoveBack()
    if err != nil {
        fmt.Printf("RemoveBack Error: %s\n", err.Error())
    }
    
    fmt.Printf("RemoveBack\n")
    err = singleList.RemoveBack()
    if err != nil {
        fmt.Printf("RemoveBack Error: %s\n", err.Error())
    }
    
    err = singleList.Traverse()
    if err != nil {
        fmt.Println(err.Error())
    }
    
   fmt.Printf("Size: %d\n", singleList.Size())
}

Output:

AddFront: A
AddFront: B
AddBack: C
Size: 3
B
A
C
RemoveFront
RemoveBack
RemoveBack
RemoveBack
RemoveBack Error: removeBack: List is empty
TranverseError: List is empty
Size: 0

The post Linked List in Golang appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/singly-linked-list-in-golang/feed/ 2 832