Golang program for implementation of Linked List

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In computer science, a linked list was a linear collection of data elements, in which linear order isn't given by their ph ysical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Here's source code of the Go program to Implement single unsorted Linked List

Linked List in golangpackage main import "FMT" type Node struct {prev *node next *node key interface{}} type List struct {head *node tail *node} func (L *list) Insert (Key interface{}) {list: = &node{next:l. Head, Key:key,} if l.head! = Nil {l.head.prev = list} L.head = list L: = L.head for        L.next! = Nil {L = l.next} l.tail = L} func (L *list) Display () {List: = L.head for List! = Nil { Fmt. Printf ("%+v", list.key) List = List.next} fmt. Println ()} func Display (list *node) {for list! = nil {fmt. Printf ("%v", list.key) List = List.next} fmt. Println ()} func showbackwards (list *node) {for list! = nil {fmt. Printf ("%v <-", list.key) List = List.prev} fmt.        Println ()} func (L *list) Reverse () {curr: = l.head var prev *node l.tail = L.head for Curr! = Nil { Next: = Curr.next Curr.next = prEV prev = Curr Curr = next} l.head = prev Display (l.head)} func main () {link: = list{} link . Insert (5) Link. Insert (9) Link. Insert (+) link. Insert (+) link. Insert () Link. Insert (*) fmt. Println ("\n==============================\n") fmt. Printf ("Head:%v\n", Link.head.key) fmt. Printf ("Tail:%v\n", Link.tail.key) link. Display () fmt. Println ("\n==============================\n") fmt. Printf ("Head:%v\n", Link.head.key) fmt. Printf ("Tail:%v\n", Link.tail.key) link. Reverse () fmt. Println ("\n==============================\n")}
C:\golang\time>go run link.go==============================Head: 36Tail: 536 ->28 ->22 ->13 ->9 ->5 ->==============================head: 36tail: 55 ->9 ->13 ->22 ->28 ->36 ->==============================C:\golang\time>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.