single Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/single/ Sun, 02 May 2021 05:07:22 +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 Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/single/ 32 32 159787465 Check if a linked list is circular in Go (Golang) https://vikasboss.github.io/linked-list-is-circular-go/ https://vikasboss.github.io/linked-list-is-circular-go/#respond Sun, 02 May 2021 05:07:04 +0000 https://vikasboss.github.io/?p=5578 Overview Check if a linked list is circular. A linked list is circular is all nodes are connected in the form of a cylce. Program In the below program, we first create...

The post Check if a linked list is circular in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Linked List - Is circular image

Overview

Check if a linked list is circular. A linked list is circular is all nodes are connected in the form of a cylce.

Program

In the below program, we first create a linked list. We then check if it is circular. It will print false first. After that, we convert the linked list into a circular linked list. We then again check if it is circular. It prints true now

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())
    }
    isCircular := singleList.IsCircular()
    fmt.Printf("Before: Is Circular: %t\n", isCircular)

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

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

Output

AddFront: D
AddFront: C
AddFront: B
AddFront: A
A
B
C
D
Before: Is Circular: false
Size: 4
After: Is Circular: true

The post Check if a linked list is circular in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/linked-list-is-circular-go/feed/ 0 5578
Double, Single and Back quotes in Go (Golang) https://vikasboss.github.io/double-single-back-quotes-go/ https://vikasboss.github.io/double-single-back-quotes-go/#respond Mon, 06 Jan 2020 16:59:08 +0000 https://vikasboss.github.io/?p=1110 Double quotes It is used to define a string. A string defined within double quotes will honor escaping characters. For, eg for when printing a string having \n there will be a...

The post Double, Single and Back quotes in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Double quotes

It is used to define a string. A string defined within double quotes will honor escaping characters. For, eg for when printing a string having \n there will be a new line printed. Similarly, \t will have tab printed.

Back Quotes

It is also used to define a string. A string encoded in back quotes is a raw literal string and doesn’t honor any kind of escaping.

Single quotes

To declare either a byte or a rune we use single quotes. While declaring byte we have to specify the type. If we don’t specify the type, then the default type is meant as a rune. A single quote will allow only one character. On declaring a byte or rune with two characters within a single quote, the compiler will raise an error as below

invalid character literal (more than one character)

Let’s see an example of all things discussed above.

  • Notice in below output that string enclosed in back quotes doesn’t honor \n or \t
  • Uncomment the below line to see the compiler error we discussed above.
r = 'ab'

Example:

package main

import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    //String in double quotes
    x := "tit\nfor\ttat"
    fmt.Println("Priting String in Double Quotes:")
    fmt.Printf("x is: %s\n", x)
    
   //String in back quotes
    y := `tit\nfor\ttat`
    fmt.Println("\nPriting String in Back Quotes:")
    fmt.Printf("y is: %s\n", y)
   
    //Declaring a byte with single quotes
    var b byte = 'a'
    fmt.Println("\nPriting Byte:")
    //Print Size, Type and Character
    fmt.Printf("Size: %d\nType: %s\nCharacter: %c\n", unsafe.Sizeof(b), reflect.TypeOf(b), b)
    
    //Declaring a rune with single quotes
    r := '£'
    fmt.Println("\nPriting Rune:")
    //Print Size, Type, CodePoint and Character
    fmt.Printf("Size: %d\nType: %s\nUnicode CodePoint: %U\nCharacter: %c\n", unsafe.Sizeof(r), reflect.TypeOf(r), r, r)
    //Below will raise a compiler error - invalid character literal (more than one character)
    //r = 'ab'
}

Output:

Priting String in Double Quotes:
x is: tit
for tat

Priting String in Back Quotes:
y is: tit\nfor\ttat

Priting Byte:
Size: 1
Type: uint8
Character: a

Priting Rune:
Size: 4
Type: int32
Unicode CodePoint: U+00A3
Character: £

The post Double, Single and Back quotes in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/double-single-back-quotes-go/feed/ 0 1110