LinkedList

package
v0.0.0-...-988b5a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2023 License: MIT Imports: 0 Imported by: 0

README

Linked List

A linked list is a way to store a collection of elements. Like an array these can be character or integers. Each element in a linked list is stored in the form of a node. A node is a collection of two sub-elements or parts. A data part that stores the element and a next part that stores the link to the next node.

A linked list is formed when many such nodes are linked together to form a chain. Each node points to the next node present in the order. The first node is always used as a reference to traverse the list and is called HEAD. The last node points to NULL.

Linked List

Pseudocode for Basic Operations

Add node
node addNode(node head, int value) {
   node temp, p; // declare two nodes temp and p
   temp = createNode(); // assume createNode creates a new node with data = 0 and next pointing to NULL.
   temp->data = value; // add element's value to data part of node
   if (head == NULL) {
       head = temp;     // when linked list is empty
   }
   else {
       p = head; // assign head to p 
       while (p->next != NULL) {
           p = p->next; // traverse the list until p is the last node. The last node always points to NULL.
       }
       p->next = temp; // Point the previous last node to the new node created.
   }
   return head;

}
Insert
function insertAfter(Node node, Node newNode)
    if node = null
        newNode.next := newNode
    else
        newNode.next := node.next
        node.next := newNode
Iterate
function iterate(someNode)
   if someNode ≠ null
     node := someNode
     do
       do something with node.value
       node := node.next
     while node ≠ someNode

Complexities

Time Complexity
Access Search Insertion Deletion
O(n) O(n) O(1) O(n)
Space Complexity

O(n)

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNode

func NewNode(val int) *node

Types

type LinkedList

type LinkedList struct {
	// contains filtered or unexported fields
}

func (*LinkedList) Append

func (ll *LinkedList) Append(val int)

func (*LinkedList) Count

func (ll *LinkedList) Count() int

func (*LinkedList) Display

func (ll *LinkedList) Display()

func (*LinkedList) DisplayReverse

func (ll *LinkedList) DisplayReverse()

func (*LinkedList) Prepend

func (ll *LinkedList) Prepend(val int)

func (*LinkedList) RemoveAtBeg

func (ll *LinkedList) RemoveAtBeg() int

func (*LinkedList) RemoveAtEnd

func (ll *LinkedList) RemoveAtEnd() int

func (*LinkedList) Reverse

func (ll *LinkedList) Reverse()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL