Linked List Algorithm
On the other hand, since simple associated lists by themselves do not let random access to the data or any form of efficient indexing, many basic operations — such as obtaining the last node of the list, finding a node that contains a given datum, or locating the place where a new node should be inserted — may necessitate iterate through most or all of the list components. They can be used to implement several other common abstract data types, including lists, stacks, queues, associative arrays, and S-expressions, though it is not uncommon to implement those data structures directly without use a associated list as the basis. The problem of machine translation for natural language processing led Victor Yngve at Massachusetts Institute of technology (MIT) to use associated lists as data structures in his COMIT programming language for computer research in the field of linguistics. Several operating systems developed by Technical system adviser (originally of West Lafayette Indiana, and later of Chapel Hill, North Carolina) used singly associated lists as file structures. The now-classic diagram consisting of blocks representing list nodes with arrows indicating to successive list nodes looks in" program the logic theory machine" by Newell and Shaw in Proc.
package LinkedList
type Node struct {
data int
next *Node
}
type LinkedList struct {
head *Node
}
func (list *LinkedList) InsertFirst(i int) {
data := &Node{data: i}
if list.head != nil {
data.next = list.head
}
list.head = data
}
func (list *LinkedList) InsertLast(i int) {
data := &Node{data: i}
if list.head == nil {
list.head = data
return
}
current := list.head
for current.next != nil {
current = current.next
}
current.next = data
}
func (list *LinkedList) RemoveByValue(i int) bool {
if list.head == nil {
return false
}
if list.head.data == i {
list.head = list.head.next
return true
}
current := list.head
for current.next != nil {
if current.next.data == i {
current.next = current.next.next
return true
}
current = current.next
}
return false
}
func (list *LinkedList) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
if i < 0 {
return false
}
if i == 0 {
list.head = list.head.next
return true
}
current := list.head
for u := 1; u < i; u++ {
if current.next.next == nil {
return false
}
current = current.next
}
current.next = current.next.next
return true
}
func (list *LinkedList) SearchValue(i int) bool {
if list.head == nil {
return false
}
current := list.head
for current != nil {
if current.data == i {
return true
}
current = current.next
}
return false
}
func (list *LinkedList) GetFirst() (int, bool) {
if list.head == nil {
return 0, false
}
return list.head.data, true
}
func (list *LinkedList) GetLast() (int, bool) {
if list.head == nil {
return 0, false
}
current := list.head
for current.next != nil {
current = current.next
}
return current.data, true
}
func (list *LinkedList) GetSize() int {
count := 0
current := list.head
for current != nil {
count += 1
current = current.next
}
return count
}
func (list *LinkedList) GetItems() []int {
var items []int
current := list.head
for current != nil {
items = append(items, current.data)
current = current.next
}
return items
}