Created
December 15, 2022 23:22
-
-
Save abdulrahmanAlotaibi/baead997de7ffc3497d84242a805d03c to your computer and use it in GitHub Desktop.
Linkedlist ADT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main (){ | |
var l = LinkedList{} | |
var p Person= Person{"Abdulrahman",3} | |
l.insert(&p) | |
l.insert(&Person{"John", 19}) | |
l.insert(&Person{"Bob", 19}) | |
l.insert(&Person{"Jack", 19}) | |
l.insert(&Person{"Martin", 19}) | |
l.remove() | |
l.display() | |
} | |
type Person struct { | |
name string | |
age int | |
} | |
var p = Person{"",3} | |
type Node struct { | |
data *Person | |
next *Node | |
} | |
type LinkedList struct { | |
head *Node | |
current *Node | |
} | |
func (l *LinkedList) isEmpty() bool { | |
return l.head == nil | |
} | |
func (l *LinkedList) findNext(){ | |
l.current = l.current.next | |
} | |
func (l *LinkedList) insert(val *Person){ | |
if l.head == nil { | |
l.current = &Node{val,nil} | |
l.head = l.current | |
} else { | |
tmp:= l.current.next | |
n:= Node{val, tmp} | |
l.current.next = &n | |
l.current = l.current.next | |
} | |
} | |
func (l *LinkedList) remove() { | |
if l.current == l.head { | |
l.head = l.head.next | |
} else { | |
temp:= l.head | |
for temp.next != l.current { | |
temp = temp.next | |
} | |
temp.next = l.current.next | |
} | |
if l.current.next == nil { | |
l.current = l.head | |
}else{ | |
l.current = l.current.next | |
} | |
} | |
func (l *LinkedList) display(){ | |
p:= l.head | |
for p != nil { | |
fmt.Printf("%v -> ", p.data) | |
p = p.next | |
} | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment