4.linked List
4.linked List
ALGORITHMS-1 (CS)
Prof. M.G.Noel A.S Fernando
Professor in Computer Science
Head, Dept. of Information Systems Engineering, Head, External Degree &
Extension programmes , UCSC
Lecture 4
List Data Structures
Arrays
Stacks
Queues
Linked(singly) list
Doubly linked List
Circular Linked List
Linked list based Stacks
and Queues
Array based and Linked List
structures
• The array is a linear collection of data
elements in which the elements are stored
in consecutive memory locations.
Array based and Linked List structures
• The array is a linear collection of data elements
in which the elements are stored in consecutive
memory locations.
Insertions and deletions are inefficient :elements are Insertions and deletions are efficient :no shifting
usually shifted
Random access : efficient indexing No random access
No memory wastage, if array is full or almost full. Since memory is dynamically allocated (according to our
Otherwise, memory wastage requirement) there is no waste of memory
Sequential access is faster [Reason : Elements in Sequential access is not faster [Reason : Elements not in
contiguous memory locations] contiguous memory locations]
Basic Terminologies
• A linked list, is a linear collection of data
elements.
• These data elements are called nodes.
• It acts as a building block to implement data
structures such as stacks, queues, and their
variations.
• A linked list can be perceived as a train or a
sequence of nodes in which each node contains
one or more data fields and a pointer to the next
node.
Simple linked list
Linked lists contain a pointer variable START that stores the address of the first node
in the list.
We can traverse the entire list using START which contains the address of the first
node;
If START = NULL, then the linked list is empty and contains no nodes
In C, we can implement a linked list
using the following code:
AVAIL is
advanced
Case 2: Inserting a Node at the End of
a Linked List
• Suppose we want to add a new node with data 9 as the last
node of the list.
• Then the following changes will be done in the linked list.
• we take a pointer variable PTR and initialize it with START.
That is, PTR now points to the first node of the linked list.
• Using a while loop, we traverse through the linked list to
reach the last node. Once we reach the last node, we
change the NEXT pointer of the last node to store the
address of the new node.
• the NEXT field of the new node contains NULL,which
signifies the end of the linked list
Case 2: Inserting a Node at the End of
a Linked List: cont..
Case 2: Inserting a Node at the End of
a Linked List: cont..
Case 3: Inserting a Node After a Given
Node in a Linked List
• Suppose we want to add a new node with
value 9 after the node containing data 3.
Case 3: Inserting a Node After a Given
Node in a Linked List
Case 3: Inserting a Node After a Given
Node in a Linked List
Case 3: Inserting a Node After a Given
Node in a Linked List cont.….
Case 4: Inserting a Node Before a
Given Node in a Linked List