1. StudentName : Vinay Kamriya
StudentName : Vinay Kamriya
Year/Branch : 3rd year (CS)
Topic : Linked List
2. Table of content
• Introduction
• Keeping track of linked list
• Basic operations
– Traversing
– Insertion
– Insertion
– Deletion
• Types of linked list
– Singly linked list
– Circular linked list
– Doubly linked list
Spring 2012 Programming and Data Structure 2
3. Introduction
• A linked list is a data structure which can change
during execution.
– Successive elements are connected by pointers.
– Last element points to NULL.
– It can grow or shrink in size during execution of a
– It can grow or shrink in size during execution of a
program.
– It can be made just as long as required.
– It does not waste memory space.
Spring 2012 Programming and Data Structure 3
A B C
head
4. • Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).
• Linked lists provide flexibility in allowing the
items to be rearranged efficiently.
items to be rearranged efficiently.
– Insert an element.
– Delete an element.
Spring 2012 Programming and Data Structure 4
5. Basic Operations on a List
• Traversing the list
• Inserting an item in the list
• Deleting an item from the list
• Deleting an item from the list
Spring 2012 Programming and Data Structure 5
6. Traversing a linked list
Spring 2012 Programming and Data Structure 6
Time complexcity of traversing an linkedlist
- O(n)
7. Pseudo-code for traversal
public static void traversal(Node head)
{
while(head!=null)
{
{
System.out.print(head.data + " ");
head = head.next;
}
}
Spring 2012 Programming and Data Structure 7
9. •For insertion:
–A record is created holding the new
item.
–The next pointer of the new record is
set to link it to the item which is to
follow it in the list.
Spring 2012 Programming and Data Structure 9
follow it in the list.
–The next pointer of the item which is
to precede it must be modified to point
to the new item.
10. Pseudo-code for insertion
public static Node insertAtFirst(Node head , int
data)
{
Node newNode = new Node(data);
newNode.next = head;
return newNode;
Spring 2012 Programming and Data Structure 10
return newNode;
}
11. Illustration: Deletion
A B C
Item to be deleted
curr
tmp
Spring 2012 Programming and Data Structure 11
A B C
curr
12. • For deletion:
– The next pointer of the item immediately
preceding the one to be deleted is altered, and
made to point to the item following the deleted
item.
item.
Spring 2012 Programming and Data Structure 12
13. Pseudo-code for deletion
public static Node deletionAtFirst(Node
head)
{
head = head.next;
Spring 2012 Programming and Data Structure 13
head = head.next;
return head;
}
14. Types of Lists
• Depending on the way in which the links are
used to maintain adjacency, several different
types of linked lists are possible.
– Linear singly-linked list (or simply linear list)
• One we have discussed so far.
Spring 2012 Programming and Data Structure 14
A B C
head
15. – Circular linked list
• The pointer from the last element in the list points back
to the first element.
head
Spring 2012 Programming and Data Structure 15
A B C
16. – Doubly linked list
• Pointers exist between adjacent nodes in both
directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of
• Usually two pointers are maintained to keep track of
the list, head and tail.
Spring 2012 Programming and Data Structure 16
A B C
head tail