[Go] Linked List 구현

2022. 7. 23. 13:11·Go
반응형
package datastruct

import (
    "fmt"
)

type LinkedList struct {
    Head *Node
    Tail *Node
    Size int
}

type Node struct {
    Data any
    Next *Node
}

func NewLinkedList() *LinkedList {
    return &LinkedList{
        Head: nil,
        Tail: nil,
        Size: 0,
    }
}

func newNode(value any) *Node {
    return &Node{
        Data: value,
        Next: nil,
    }
}

func (list *LinkedList) Lpush(value any) {
    node := newNode(value)

    if list.Head == nil {
        list.Head = node
        list.Tail = node

    } else {
        node.Next = list.Head
        list.Head = node
    }
    list.Size += 1
}

func (list *LinkedList) Rpush(value any) {
    node := newNode(value)

    if list.Tail == nil {
        list.Head = node
        list.Tail = node

    } else {
        list.Tail.Next = node
        list.Tail = node
    }
    list.Size += 1
}

func (list *LinkedList) ShowNodes() {
    if list.Size == 0 {
        fmt.Println("LinkedList is empty")

    } else {
        node := list.Head
        res := fmt.Sprintf("%v → ", node.Data)

        for i := 1; i < list.Size; i++ {
            node = node.Next
            res += fmt.Sprintf("%v → ", node.Data)
        }
        fmt.Println(res)
    }
}

func (list *LinkedList) Lpop() *Node {
    if list.Size == 0 {
        return nil

    } else if list.Size == 1 {
        head := list.Head
        list.Head = nil
        list.Tail = nil
        list.Size -= 1
        return head

    } else {
        head := list.Head
        list.Head = head.Next
        list.Size -= 1
        return head
    }
}

func (list *LinkedList) Rpop() *Node {
    if list.Size == 0 {
        return nil

    } else if list.Size == 1 {
        tail := list.Tail
        list.Head = nil
        list.Tail = nil

        list.Size -= 1
        return tail

    } else {
        tail := list.Tail

        node := list.Head
        for i := 1; i < list.Size; i++ {
            node = node.Next
            if i == list.Size-2 {
                list.Tail = node
                list.Tail.Next = nil
            }
        }

        list.Size -= 1
        return tail
    }
}

func LinkedListMain() {
    linkedList := NewLinkedList()

    // lpush
    linkedList.Lpush(1)
    linkedList.Lpush(2)
    linkedList.Lpush(3)

    // rpush
    linkedList.Rpush(4)
    linkedList.Rpush(5)
    linkedList.Rpush(6)

    // show
    linkedList.ShowNodes()

    // lpop
    linkedList.Lpop()
    linkedList.Lpop()
    linkedList.ShowNodes()

    // rpop
    linkedList.Rpop()
    linkedList.Rpop()
    linkedList.ShowNodes()

    // pop
    linkedList.Lpop()
    linkedList.ShowNodes()
    linkedList.Rpop()
    linkedList.ShowNodes()

    // push
    linkedList.Rpush(4)
    linkedList.Rpush(5)
    linkedList.ShowNodes()

    // pop
    linkedList.Rpop()
    linkedList.Rpop()
    linkedList.ShowNodes()
}
728x90
반응형
저작자표시 비영리 변경금지 (새창열림)
'Go' 카테고리의 다른 글
  • [Go] Channel 이용한 Queue
  • [Go] Int, String, Struct 정렬
  • [Go] Panic recover 하기
  • [Go] File 정보 확인
상쾌한기분
상쾌한기분
  • 상쾌한기분
    상쾌한기분
    상쾌한기분
  • 전체
    오늘
    어제
    • 분류 전체보기 (251)
      • Python (44)
        • Python (26)
        • Django (6)
        • Flask (4)
        • Open Source (6)
      • Kotlin & Java (5)
        • Spring (2)
        • 프로젝트 (1)
      • Go (11)
      • Database (24)
        • MySQL (21)
        • Redis (3)
      • Infrastructure (2)
        • CDC (4)
        • Kafka (5)
        • Prometheus (2)
        • Fluentd (11)
        • Docker (1)
        • Airflow (2)
        • VPN (2)
      • IT (26)
        • AI (9)
        • Langchain (8)
        • Web (18)
        • Git (8)
        • 리팩토링 (9)
        • Micro Service Architecture (8)
        • Clean Code (16)
        • Design Pattern (0)
        • 수학 (1)
        • 알고리즘 (14)
      • OS (14)
        • Centos (10)
        • Ubuntu (3)
        • Mac (1)
      • Search Engine (2)
        • ElasticSearch (1)
        • Lucene Solr (1)
      • PHP (2)
        • Laravel (1)
        • Codeigniter (1)
  • 블로그 메뉴

    • Github 방문
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    prompt
    performance
    docker
    MYSQL
    ollama
    python
    Golang
    fluentd
    파이썬
    Redis
    오블완
    백준
    CDC
    git
    go
    LLM
    티스토리챌린지
    Langchain
    Kafka
    http
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
상쾌한기분
[Go] Linked List 구현
상단으로

티스토리툴바