1
Linked Lists
Session III
Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil.,
PhD., D.Litt.,
Director, Department of Computer Science, Jairams
Arts and Science College, Karur.
Data Structures & Algorithms
2
• Linked Lists
• Application of Linked List
• Types of Linked List
• Basic operations of Linked List
• Simulation using Linked Lists
• Other List Structures
Data Structures & Algorithms
Introduction to Linked Lists
3
Linked List
• Very flexible dynamic data structure
• Widely used in real time programming.
• Possible to grow and shrink size at any time.
• Def:- A linked list is a chain of structures in which each structure consists
of data as well as pointers, which store the address (link) of the next
logical structure in the list.
Data Structures & Algorithms
4
• The major goal behind linked list data structure is to eliminate the data
movement associated with insertions into and deletions from the
middle of the list.
• Advantage -not necessary to know the number of elements and allocate
memory beforehand.
• Disadvantage -having array is even if it is dynamically allocated one is that it
cannot grow.
• A linked list is a structure that guide to algorithms, which minimize data
movement as insertion and deletion occur in an ordered list.
• Each element called node contains data field and pointer field (in that
pointer field contains an address].
• The last node in the list contains a NULL pointer to indicate that it is the end
or tail of the list.
Data Structures & Algorithms
Applications of Linked List
• Directory structures
• Operating systems for process management
• Data management in data base systems.
5
Types Of Linked List
• Single linked list - each node contains data and a single
link, which attaches it to the next node in the list.
Data Structures & Algorithms
• Each node of the list has two elements
• the item being stored in the list and
• a pointer to the next item in the list
• The last node in the list contains a NULL pointer to indicate that it is
the end or tail of the list.
6
Doubly Linked List
• Doubly linked lists have a pointer to the preceding item as well as one
to the next.
Data Structures & Algorithms
• Permits scanning or searching of the list in both directions.
• Many applications require searching backwards and
forwards through the sections of a list:
• For example,
• Searching for a common name in a telephone directory
would probably need much scanning backwards
and forwards through a small region of the whole
list, so the backward links become very useful.
7
• Linked list structure in which the last element point to the first
element of the lists is called circularly linked list.
– It is a list in which the link field of the last element of the list
contains a pointer to the first element of the list.
– The last element of the list no longer points to a NIL value
Data Structures & Algorithms
Circular Linked List
• If the external pointer points to the current "tail" of the list, then the "head“
is found trivially via tail->next, permitting us to have either LIFO or
FIFO lists with only one external pointer.
• In modern processors, the few bytes of memory saved in this way would
probably not be regarded as significant.
• A circularly linked list would more likely be used in an application which
required "round-robin" scheduling or processing.
8
Basic Operation of Linked List
• List is dynamic structure that is number of nodes on a list may
vary dramatically based on a list operation.
• The operations are given below:
– Initializing a Linked List
– Adding element in a linked List
– Traversing a node in a Linked List
– Removing element in a Linked List
Data Structures & Algorithms
9
Data Structures & Algorithms
SLList:: SLList()
{ Head = new List;
Tail=Head;
Currentptr =Head;
}
Initialization of a Linked List
Initialization
Head
Current ptr Allocated
Instance
Tail
• Initialization refers- creation of a node and setting the required pointers.
• Link pointers of a node have to be set to the following node.
• A node is created as an instance, and then a pointer is assigned.
• For the first node the head pointer, tail pointer and current pointer would be to
the same node.
10
Adding a Node
Data Structures & Algorithms
Add a Node
Before Head
Tail
Step1 Head
New Node
Next
Tail
Tail Node
Step 2 Head NULL
• Add nodes in two possible places, at the beginning or the end of the list, default
method is at the end.
• Makes linked list act as a queue with the head node being the oldest and end
pointing to the newest objects.
11
• A node is added to the end of the list, then move the tail pointer to point to
the new instance
• Following this is accessing the new node through the tail pointer
• Since a new instance is allocated and assigned to the tail pointer
void SLList: : AddANode ()
{
Tail->Next = new List;
Tail= Tail ->Next;
}
• The below code adding of a node to the end of the list, and then moving the
end pointer
Data Structures & Algorithms
12
Traversing the List
ListPtr SLList :: Previous (long index)
{ ListPtr temp=Head;
for (long count=0;count<index-1;count++)
{temp=temp->Next; }
return temp;}
ListPtr SLList :: Previous (ListPtr index)
{ListPtr temp=Head;
If ( index==Head) //special case, index IS the head :)
{ return Head;
}
while (temp->Next != index)
{ temp= temp->Next; }
return temp;
}
Data Structures & Algorithms
• Traversing a list refers - movement from one node to another in the sequence
of its creation
• Accessing a particular node for insertion or deletion involves the traversing of
all the previous nodes till the required position is reached
13
• A node is created which uses a class node pointer declared at CurrentPtr.
• For any given node the two possibilities would be either to move forward in
a list or move backward.
• Function called in the code checks the node position, whether it is a head or
a tail node.
• If the node passed to the function happens to be a head node, the pointer is
restricted from moving backward.
• Similarly if the node happens to be a tail node, the forward traversing is
restricted.
void SLList :: Advance()
{ if ( CurrentPtr ->Next != NULL)
{ CurrentPtr = CurrentPtr ->Next; }}
void SLList :: Rewind()
{ if ( CurrentPtr != Head)
{ CurrentPtr = Previous (CurrentPtr); }}
Data Structures & Algorithms
14
Deleting a Node
• Deleting a node refers to reassigning the pointer from the deleted node to
the succeeding node.
• Deleting nodes from a linked list can be:
– Deleting a head node
– Deleting a middle node
– Deleting a tail node
Data Structures & Algorithms
15
void SLList :: DeleteANode(ListPtr corpse)
{
ListPtr temp;
If ( corpse == Head) //case 1 corpse = Head
{
temp=Head;
Head= Head ->Next;
delete temp;
}
else if ( corpse == Tail) //case 2 corpse is at the end
{
temp = Tail;
Tail= Previous (Tail);
Tail->Next=NULL;
delete temp;
}
else //case 3 corpse is in middle somewhere
{
temp=Previous (corpse);
temp->Next=corpse->Next;
delete corpse;
}
CurrentPtr =Head; //Reset the class tempptr
}
Data Structures & Algorithms
16
Linked implementation of Stack
• Operation of adding an element to front of a linked list is similar to pushing
an element onto a stack.
• Both case a new item is added as the only immediately accessible item in a
collection.
• Stack can be accessed only through its top element, a list can be accessed
only from the pointer to its first element.
• Operation of removing the first element from a linked list is analogous to
popping a stack.
• Both cases the only immediately accessible item of a collection is removed
from that collection, and the next item becomes immediately
accessible.
• A stack can be implemented by another way:
– A stack may be represent as a linear list.
– First node of the list is the top of the stack.
– External pointer s points to such a linked list for the operation of push
and pop
• Advantage
– Implementation of the stacks is that all stacks being used by a program
Data Structures & Algorithms
5 3 6 8 null
Stack
17
Linked implementation of Queue
• Queue item can be deleted from the front of the queue and inserted at the
rear
• Pointer to the first element of a list represent the front of the queue another
pointer to the last element of the list represents rear of the queue
5 3 6 8 null
rear
Front
• Disadvantage of representing stack or queue:
•A node in a linked list occupy more storage than array, since linked list
require two piece of information: one is data and another one is address
•Additional time spent in managing the available list
• Advantage – all the stacks and queues of a program have access to the
same free list of nodes
Data Structures & Algorithms
18
Simulation using Linked List
• Application of queues, priority queues and linked lists is a simulation
model a real world situation
• Every action is simulation is called Event.
Simulation Process
– Simulation proceeds by finding the next event to occur
– Event list-to keep track of events, the program uses an
ascending priority queue necessary to know the next event
to occur.
– Event list is a n ascending priority queue represented by an
ordered linked list
– Event is used as part of the program to control the entire process
– Event driven Simulation- which proceeds by changing the
simulated situation in response to the occurrence of one of
Data Structures & Algorithms
19
Other List Structure
Stack as a Circular List
– Circular list can be used to represent a stack or queue
– Stack be a pointer to the last node of a circular list
– First node is the top of the stack
– Empty stack is represented by a null list
– Empty function is used to check whether the stack is empty or
not (return * pstack ==null ? True : False )
– Push ( &stack , x) an integer x onto the stack, where stack is a
pointer to a circular list acting as a stack.
– Push function calls on the function empty, which tests whether its parameter is
NULL.
• Push routine is slightly more complex for circular list than it is
linear list.
– Pop (& Stack) function for stack implemented as a circular list
– Call empty function for checking underflow condition.
– Call freenode function to pop the value from the stack
Data Structures & Algorithms
20
Queue as a Circular list
• To represent a queue as a circular list easy than as a linear list
• In linear list queue is represented by two pointers (Front and rear)
• Using Circular list a queue may be specified by a single pointer q to that list
• Node ( p) is the rear of the queue the following node is its front
• Empty function is same as for stack
• Remove( pq ) is identical to pop function in stack
• Insert ( &q , x)-inserting an element x is in the rear of the circular queue
• Element is inserted into the front of the queue and the circular list pointer is
then advanced one element ,So the new element becomes rear
Data Structures & Algorithms

Data Structures 3

  • 1.
    1 Linked Lists Session III Dr.V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt., Director, Department of Computer Science, Jairams Arts and Science College, Karur. Data Structures & Algorithms
  • 2.
    2 • Linked Lists •Application of Linked List • Types of Linked List • Basic operations of Linked List • Simulation using Linked Lists • Other List Structures Data Structures & Algorithms Introduction to Linked Lists
  • 3.
    3 Linked List • Veryflexible dynamic data structure • Widely used in real time programming. • Possible to grow and shrink size at any time. • Def:- A linked list is a chain of structures in which each structure consists of data as well as pointers, which store the address (link) of the next logical structure in the list. Data Structures & Algorithms
  • 4.
    4 • The majorgoal behind linked list data structure is to eliminate the data movement associated with insertions into and deletions from the middle of the list. • Advantage -not necessary to know the number of elements and allocate memory beforehand. • Disadvantage -having array is even if it is dynamically allocated one is that it cannot grow. • A linked list is a structure that guide to algorithms, which minimize data movement as insertion and deletion occur in an ordered list. • Each element called node contains data field and pointer field (in that pointer field contains an address]. • The last node in the list contains a NULL pointer to indicate that it is the end or tail of the list. Data Structures & Algorithms Applications of Linked List • Directory structures • Operating systems for process management • Data management in data base systems.
  • 5.
    5 Types Of LinkedList • Single linked list - each node contains data and a single link, which attaches it to the next node in the list. Data Structures & Algorithms • Each node of the list has two elements • the item being stored in the list and • a pointer to the next item in the list • The last node in the list contains a NULL pointer to indicate that it is the end or tail of the list.
  • 6.
    6 Doubly Linked List •Doubly linked lists have a pointer to the preceding item as well as one to the next. Data Structures & Algorithms • Permits scanning or searching of the list in both directions. • Many applications require searching backwards and forwards through the sections of a list: • For example, • Searching for a common name in a telephone directory would probably need much scanning backwards and forwards through a small region of the whole list, so the backward links become very useful.
  • 7.
    7 • Linked liststructure in which the last element point to the first element of the lists is called circularly linked list. – It is a list in which the link field of the last element of the list contains a pointer to the first element of the list. – The last element of the list no longer points to a NIL value Data Structures & Algorithms Circular Linked List • If the external pointer points to the current "tail" of the list, then the "head“ is found trivially via tail->next, permitting us to have either LIFO or FIFO lists with only one external pointer. • In modern processors, the few bytes of memory saved in this way would probably not be regarded as significant. • A circularly linked list would more likely be used in an application which required "round-robin" scheduling or processing.
  • 8.
    8 Basic Operation ofLinked List • List is dynamic structure that is number of nodes on a list may vary dramatically based on a list operation. • The operations are given below: – Initializing a Linked List – Adding element in a linked List – Traversing a node in a Linked List – Removing element in a Linked List Data Structures & Algorithms
  • 9.
    9 Data Structures &Algorithms SLList:: SLList() { Head = new List; Tail=Head; Currentptr =Head; } Initialization of a Linked List Initialization Head Current ptr Allocated Instance Tail • Initialization refers- creation of a node and setting the required pointers. • Link pointers of a node have to be set to the following node. • A node is created as an instance, and then a pointer is assigned. • For the first node the head pointer, tail pointer and current pointer would be to the same node.
  • 10.
    10 Adding a Node DataStructures & Algorithms Add a Node Before Head Tail Step1 Head New Node Next Tail Tail Node Step 2 Head NULL • Add nodes in two possible places, at the beginning or the end of the list, default method is at the end. • Makes linked list act as a queue with the head node being the oldest and end pointing to the newest objects.
  • 11.
    11 • A nodeis added to the end of the list, then move the tail pointer to point to the new instance • Following this is accessing the new node through the tail pointer • Since a new instance is allocated and assigned to the tail pointer void SLList: : AddANode () { Tail->Next = new List; Tail= Tail ->Next; } • The below code adding of a node to the end of the list, and then moving the end pointer Data Structures & Algorithms
  • 12.
    12 Traversing the List ListPtrSLList :: Previous (long index) { ListPtr temp=Head; for (long count=0;count<index-1;count++) {temp=temp->Next; } return temp;} ListPtr SLList :: Previous (ListPtr index) {ListPtr temp=Head; If ( index==Head) //special case, index IS the head :) { return Head; } while (temp->Next != index) { temp= temp->Next; } return temp; } Data Structures & Algorithms • Traversing a list refers - movement from one node to another in the sequence of its creation • Accessing a particular node for insertion or deletion involves the traversing of all the previous nodes till the required position is reached
  • 13.
    13 • A nodeis created which uses a class node pointer declared at CurrentPtr. • For any given node the two possibilities would be either to move forward in a list or move backward. • Function called in the code checks the node position, whether it is a head or a tail node. • If the node passed to the function happens to be a head node, the pointer is restricted from moving backward. • Similarly if the node happens to be a tail node, the forward traversing is restricted. void SLList :: Advance() { if ( CurrentPtr ->Next != NULL) { CurrentPtr = CurrentPtr ->Next; }} void SLList :: Rewind() { if ( CurrentPtr != Head) { CurrentPtr = Previous (CurrentPtr); }} Data Structures & Algorithms
  • 14.
    14 Deleting a Node •Deleting a node refers to reassigning the pointer from the deleted node to the succeeding node. • Deleting nodes from a linked list can be: – Deleting a head node – Deleting a middle node – Deleting a tail node Data Structures & Algorithms
  • 15.
    15 void SLList ::DeleteANode(ListPtr corpse) { ListPtr temp; If ( corpse == Head) //case 1 corpse = Head { temp=Head; Head= Head ->Next; delete temp; } else if ( corpse == Tail) //case 2 corpse is at the end { temp = Tail; Tail= Previous (Tail); Tail->Next=NULL; delete temp; } else //case 3 corpse is in middle somewhere { temp=Previous (corpse); temp->Next=corpse->Next; delete corpse; } CurrentPtr =Head; //Reset the class tempptr } Data Structures & Algorithms
  • 16.
    16 Linked implementation ofStack • Operation of adding an element to front of a linked list is similar to pushing an element onto a stack. • Both case a new item is added as the only immediately accessible item in a collection. • Stack can be accessed only through its top element, a list can be accessed only from the pointer to its first element. • Operation of removing the first element from a linked list is analogous to popping a stack. • Both cases the only immediately accessible item of a collection is removed from that collection, and the next item becomes immediately accessible. • A stack can be implemented by another way: – A stack may be represent as a linear list. – First node of the list is the top of the stack. – External pointer s points to such a linked list for the operation of push and pop • Advantage – Implementation of the stacks is that all stacks being used by a program Data Structures & Algorithms 5 3 6 8 null Stack
  • 17.
    17 Linked implementation ofQueue • Queue item can be deleted from the front of the queue and inserted at the rear • Pointer to the first element of a list represent the front of the queue another pointer to the last element of the list represents rear of the queue 5 3 6 8 null rear Front • Disadvantage of representing stack or queue: •A node in a linked list occupy more storage than array, since linked list require two piece of information: one is data and another one is address •Additional time spent in managing the available list • Advantage – all the stacks and queues of a program have access to the same free list of nodes Data Structures & Algorithms
  • 18.
    18 Simulation using LinkedList • Application of queues, priority queues and linked lists is a simulation model a real world situation • Every action is simulation is called Event. Simulation Process – Simulation proceeds by finding the next event to occur – Event list-to keep track of events, the program uses an ascending priority queue necessary to know the next event to occur. – Event list is a n ascending priority queue represented by an ordered linked list – Event is used as part of the program to control the entire process – Event driven Simulation- which proceeds by changing the simulated situation in response to the occurrence of one of Data Structures & Algorithms
  • 19.
    19 Other List Structure Stackas a Circular List – Circular list can be used to represent a stack or queue – Stack be a pointer to the last node of a circular list – First node is the top of the stack – Empty stack is represented by a null list – Empty function is used to check whether the stack is empty or not (return * pstack ==null ? True : False ) – Push ( &stack , x) an integer x onto the stack, where stack is a pointer to a circular list acting as a stack. – Push function calls on the function empty, which tests whether its parameter is NULL. • Push routine is slightly more complex for circular list than it is linear list. – Pop (& Stack) function for stack implemented as a circular list – Call empty function for checking underflow condition. – Call freenode function to pop the value from the stack Data Structures & Algorithms
  • 20.
    20 Queue as aCircular list • To represent a queue as a circular list easy than as a linear list • In linear list queue is represented by two pointers (Front and rear) • Using Circular list a queue may be specified by a single pointer q to that list • Node ( p) is the rear of the queue the following node is its front • Empty function is same as for stack • Remove( pq ) is identical to pop function in stack • Insert ( &q , x)-inserting an element x is in the rear of the circular queue • Element is inserted into the front of the queue and the circular list pointer is then advanced one element ,So the new element becomes rear Data Structures & Algorithms