List ADT
List ADT
Algorithms
List ADT
An ADT – from the previous lecture
l1 l2 l3 …. ln ….
List[0] List[1] List[2] List[n]
1.Insertion Operation
•Inserting at the head of the list requires first pushing
the entire array one step to make room for the new
element
a1 a2 a3 a4 …. an
0 1 2 3 n-1
insert(L,0,x)
a1 a2 a3 a4 …. an
0 1 2 3 n-1
x a1 a2 a3 …. an-1 an
0 1 2 3 n-1
Insertion Algorithms
Insert ( L, x, i)
• Access to the list is gained through a pointer to the head of the list
(or a header node).
• This structure is called a singly linked list because each node in the
list is connected to the next by a single link.
head
Class Node
{
Private:
T data;
Node<T<*next;
};
Linked List Declaration
Class SLList{
Private:
Int Lenth;Node * head;//pointer to head
Public:
/specify the list operations
};
List Operations
1. To find the address of the i-th element, we can use the following
algorithm
Traverse (integer i)
prev head //pointer to head
current head //make head the
current
if i ≠ 1 then
current current->next
for j 2 to i do
prev current
current current->next
return prev
2. Insertion Operation
i
Insertion Algorithm
• Inserting after a node
• Suppose that we have a pointer, prev pointing to the node at position i-1
then insert operation can be performed using the following statements
i +1
i
i-1 i
Deletion Algorithm
• To delete a node
• We require two pointers, previous that points to the node at position i-1 and
current that points to the i-th node
• The operation can then be accomplished by
• Lab 2
Other Variations of the Linked List
data
Doubly Linked List
L
a3
* a1 a2 a4 *
Circular Doubly Linked List
L
a3
a1 a2 a4