list Algorithm

An algorithm is a step-by-step procedure or a set of rules and instructions to solve a particular problem or perform a specific task. The list algorithm, in the context of computer science and data structures, is a collection of techniques and methods designed to efficiently manage and manipulate lists. Lists are a fundamental data structure that store multiple elements in a linear order, allowing for easy access, addition, and removal of elements. List algorithms play a crucial role in various applications, such as sorting, searching, and merging, as they provide efficient ways to handle and process data in lists. List algorithms can be implemented using different data structures such as arrays, linked lists, or dynamic arrays, and their efficiency may vary based on the chosen structure. Some well-known list algorithms include the binary search algorithm, which efficiently searches for a specific element in a sorted list; the merge sort algorithm, which is an efficient and stable sorting technique that divides a list into smaller sublists and combines them in a sorted order; and the quick sort algorithm, which is another sorting technique that selects a 'pivot' element and rearranges the list such that all elements less than the pivot come before it, and all elements greater than the pivot come after it. These algorithms, along with countless others, form the foundation of computer programming and data manipulation, enabling the development of complex software and applications.
package list
 
import "sync"
import "testing"
 
type List struct {
    head *Item
    last *Item
    len int
    locker sync.RWMutex
}
 
type Item struct {
    Val interface{}
    next *Item
    prev *Item
    list *List
}
 
func New() *List {
    list := &List{}
    list.len = 0
    return list
}
 
func Insert(value interface{}, list *List) *List {
    newItem := &Item{value, list.head, list.last, list}
    list.locker.Lock()
    defer list.locker.Unlock()
 
    if list.head == nil {
        list.head = newItem
        list.last = newItem
    } else {
        list.head.prev = newItem
        list.head = newItem
        list.last.next = newItem
    }
 
    list.len++
 
    return list
}
 
func (list *List) First() *Item {
    return list.head
}
 
func (list *List) Last() *Item {
    return list.last
}
 
func (item *Item) Prev() *Item {
    return item.prev
}
 
func (item *Item) Next() *Item {
    return item.next
}
 
func Has(value interface{}, list *List) bool {
    if list.head == nil{
        return false
    }
    first := list.First()

    for {
        if first.Val == value {
            return true
        } else {
            if first.next != nil {
                first = first.next
            } else {
                return false
            }
        }
    }
 
    return false
}
 
func Remove(value interface{}, list *List) *List {
    list.locker.RLock()
    
    if list.head == nil {
    return list
    }

    list.locker.RUnlock()

    list.locker.RLock()
    first := list.First()
    last := list.Last()
    list.locker.RUnlock()
    list.locker.Lock()
    defer list.locker.Unlock()
    
    for {
        if last.next == nil {
            return list
        }
 
        if first.Val == value {
            first.prev.next = first.next
            first.next.prev = first.prev
            first.prev = nil
            first.next = nil
            first.Val = nil
            first.list = nil
            list.len--
            return list
        } else {
            first = first.next
        }
    }
}
 
func Length(list *List) int {
    return list.len
}

func Test_New(t *testing.T) {
    list := New()
    list = Insert(1, list)
    list = Insert(2, list)
    list = Insert(3, list)
    list = Insert(10, list)
    list = Insert(103, list)
    list = Insert(56, list)

    has := Has(103, list)
        if has != true {
                t.Error("[Error] Has(103, list) doesn't work as expected'")
        }

        if Length(list) != 6 {
                t.Error("[Error] Length(list) doensn't work as expected'")
        }

        list = Remove(10, list)

        has = Has(10, list)
        if has != false {
                t.Error("[Error] Has(10, list) doesn't work as expected after removing 10 from list'")
        }

        if Length(list) != 5 {
                t.Error("[Error] Length(list) doensn't work as expected after removing data from list'")
        }
}

LANGUAGE:

DARK MODE: