package main
import "fmt"
type Node struct {
data int
next* Node
}
type SLList struct {
head* Node
tail* Node
size int
}
func (list* SLList) Print() {
if list.size == 0 {
fmt.Println("SLList empty")
return
}
node := list.head
for node != nil {
fmt.Print(node.data, " ")
node = node.next
}
fmt.Print("(", list.size, ") ")
if list.head != nil {
fmt.Print("(", list.head.data, "..", list.tail.data, ")")
}
fmt.Println()
}
func (list* SLList) PushLeft(data int) {
node := Node{data: data}
node.next = list.head
list.head = &node
if list.size == 0 {
list.tail = &node
}
list.size++
}
func (list* SLList) PopLeft() int {
if list.size == 0 {
return 0
}
data := list.head.data
list.head = list.head.next
list.size--
if list.size == 0 {
list.tail = nil
}
return data
}
func (list* SLList) PushRight(data int) {
if list.size == 0 {
list.PushLeft(data)
return
}
node := Node{data: data}
list.tail.next = &node
list.tail = &node
list.size++
}
func (list* SLList) PopRight() int {
if list.size == 0 {
fmt.Println("LList empty")
return 0
}
if list.size == 1 {
return list.PopLeft()
}
node := list.head
for node.next.next != nil {
node = node.next
}
data := node.next.data
node.next = nil
list.tail = node
list.size--
return data
}
func (list* SLList) Reverse() {
curr := list.head
var prev* Node
var next* Node
for curr != nil {
next = curr.next
curr.next = prev
prev = curr
curr = next
}
list.head = prev
}
func main() {
list := new(SLList)
for i := 5; i > 0; i-- {
list.PushRight(i * 100)
list.Print()
}
list.Reverse()
list.Print()
for i := 0; i < 7; i++ {
fmt.Println(list.PopRight())
list.Print()
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: