Linked List
Linked List
Another method is to store an individual list item in whatever location is available and link
the individual item into an ordered sequence using pointers.
An element of a list is called a node. A node can consist of several data items and a
pointer, which is a variable that stores the address of the node it points to.
A pointer that does not point at anything is called a null pointer. It is usually represented
by ∅. A variable that stores the address of the first element is called a start pointer.
In Figure below, the data value in the node box represents the key field of that node. There
are likely to be many data items associated with each node. The arrows represent the
pointers. It does not show at which address a node is stored, so the diagram does not give
the value of the pointer, only where it conceptually links to.
A new node, A, is inserted at the beginning of the list. The content of startPointer is copied
into the new node's pointer field and star tpointer is set to point to the new node, A.
1
In Figure below, a new node, P, is inserted at the end of the list. The pointer field of node
L points to the new node, P. The pointer field of the new node, P, contains the null
pointer.
To delete the first node in the list, we copy the pointer field of the node to be deleted into
StartPointer.
To delete the last node in the list , we set the pointer field for the previous node to the
null pointer.
2
Sometimes the nodes are linked together in order of key field value to produce an ordered
linked list. This means a new node may need to be inserted or deleted from between two
existing nodes.
To insert a new node, C, between existing nodes, Band D, we copy the pointer field of node
B into the pointer field of the new node, C. We change the pointer field of node B to point
to the new node, C.
To delete a node, D, within the list, we copy the pointer field of the node to be deleted,
D, into the pointer field of node B.
3
Pseudo Code: Singly Linked List Node Structure
Structure Node
Data: integer
Next: Node
End Structure
4
3. Insert at a Specific Position
Procedure InsertAtPosition(head, newData, position)
Create newNode
newNode.Data ← newData
If position = 1 Then
newNode.Next ← head
head ← newNode
Else
current ← head
count ← 1
5
1. Delete from the Beginning
Procedure DeleteAtBeginning(head)
If head = NULL Then
Output "List is empty"
Else
temp ← head
head ← head.Next
Delete temp
End If
End Procedure
6
3. Delete at a Specific Position
Procedure DeleteAtPosition(head, position)
If head = NULL Then
Output "List is empty"
Else If position = 1 Then
temp ← head
head ← head.Next
Delete temp
Else
current ← head
count ← 1