2. TOPICS COVERED
• Definition
• Representation of linked list
• Operations of linked list
• Implementation of the list
• Array implementation
• Linked list implementation
• Cursor implementation
• Ajflajf
Dr. SINDHIA LINGASWAMY, VIT 2
3. DEFINITION
• Linear collection of data elements called nodes.
• Linked list is a sequence of links which contains items. Each link contains
a connection to another link.
• Important terms to understand the concept of linked list.
• Link − each link of a linked list can store a data called an element.
• Next − each link of a linked list contains a link to the next link called next.
• Linked list − a linked list contains the connection link to the first link
called first.
Dr. SINDHIA LINGASWAMY, VIT 3
4. REPRESENTATION OF LINKED LIST
Dr. SINDHIA LINGASWAMY, VIT 4
H D1
NEXT
D2
NEXT
D3
NEXT
• Linked List contains a link element called first / Header.
• Each node carries a data field(s) and a link field called next.
• Each node is linked with its next node using its next link.
• Last node carries a link as null to mark the end of the list.
5. OPERATIONS
• The basic operations supported by a list.
• Insert(X,Y) − insert an element X at the position Y.
• Deletion(X) − deletes an element X.
• Find(X) − Returns the position of X
• Next (i) – Returns the position of its successor element i+1
• Previous (i)- Returns the position of its predecessor i-1
• Display − Print the complete list.
• MakeEmpty − Makes the list empty.
Dr. SINDHIA LINGASWAMY, VIT 5
6. TYPES OF LINKED LIST
•Singly linked list
•Doubly linked list
•Circular linked list
Dr. SINDHIA LINGASWAMY, VIT 6
7. SINGLY LINKED LIST
• A linked list which each node contains only one link positioning to
the next node in the list.
Dr. SINDHIA LINGASWAMY, VIT 7
H 10 20 30
20 30 4010
8. DECLARATION OF LINKED LIST
Struct node;
typedef struct Node *List;
typedef struct Node *Position;
int IsLast( Position P, List L);
int IsEmpty(List L);
position Find (int X, List L);
void Delete (int X, List L);
position FindPrev(int X, List L);
position FindNext(int X, List L);
void Insert(int X, List L, Position P);
void DeleteList(List L);
Struct Node
{
int element;
position Next;
}
Dr. SINDHIA LINGASWAMY, VIT 8
9. INSERTION
void Insert ( int X, List L, Position P)
{
position Newnode;
Newnode = malloc(size of (Struct Node));
if (Newnode != NULL )
{
Newnode → Element = X;
Newnode → Next = P → Next ;
P → Next = Newnode;
}
}
Dr. SINDHIA LINGASWAMY, VIT 9
11. ISEMPTY
• Check whether the list is empty
int IsEmpty ( List L )
{
if ( L → Next == NULL )
return (1);
}
Empty List
Dr. SINDHIA LINGASWAMY, VIT 11
Header L
12. ISLAST
• Check whether the current position is last.
int IsLast ( position P, List L )
{
if ( P → Next == NULL);
return (1);
P
}
Dr. SINDHIA LINGASWAMY, VIT 12
L 10 20 30
13. FIND
•Returns the position of X
position Find (int X, List L)
{
position P;
P = L → Next ;
while ( P ! = NULL && P → Element ! = X)
P = P → Next;
return P; P
}
Dr. SINDHIA LINGASWAMY, VIT 13
L 10 20 30
14. PREVIOUS
position FindPrev (int X, List L)
{
position P;
P = L;
while ( P → Next ! = NULL && P → Next → Element != X)
P = P → Next ;
return P;
}
P X
Dr. SINDHIA LINGASWAMY, VIT 14
L 10 20 30
15. FINDNEXT
position FindNext ( int X, List L)
{
P = L → Next;
while ( P → Next ! = NULL && P → Element ! = X)
P = P → Next ;
return P → Next;
}
X, P P → Next
Dr. SINDHIA LINGASWAMY, VIT 15
L 10 20 30
16. Delete an Element
void Delete (int X, List L)
{ X
position P, Temp;
P = FindPrev( X,L );
if ( ! IsLast ( P, L)) P Temp
{
Temp = P → Next;
P → Next = Temp → Next;
Free (Temp);
}
}
Dr. SINDHIA LINGASWAMY, VIT 16
L 10 20 30
L 10 30
17. DELETE THE LIST
Void DeleteList ( List L)
{
position P, Temp;
P = L → Next;
L → Next = NULL;
while ( P ! = NULL )
{
Temp = P → Next;
free (P);
P = Temp;
}
}
Dr. SINDHIA LINGASWAMY, VIT 17
18. Dr. SINDHIA LINGASWAMY, VIT 18
L 10 30
10 30
30
L → Next = NULL
Temp = P → Next
P
P Temp
Free ( P )
P = Temp
Temp , P = NULL
19. DOUBLY LINKED LIST
◦ Doubly linked list is a linked data structure that consists of a set of
sequentially linked records called nodes.
◦ Each node contains three fields
◦ one is data part which contain data only.
◦ two other field is links part that are point or references to the
previous or to the next node in the sequence of nodes.
◦ The beginning and ending nodes' previous and next links,
respectively, point to some kind of terminator, typically a sentinel
node or null to facilitate traversal of the list.
Dr. SINDHIA LINGASWAMY, VIT 19
21. COMPARED WITH SLL
◦ Advantages
◦ Can be traversed in either direction (may be essential for some
programs)
◦ Some operations, such as deletion and inserting before a node,
become easier
◦ Disadvantages
◦ Requires more space
◦ List manipulations are slower (because more links must be changed)
◦ Greater chance of having bugs (because more links must be
manipulated) Dr. SINDHIA LINGASWAMY, VIT 21
25. ROUTINE – DELETION
void Delete ( int X, List L)
{
position P;
P = Find(X,L);
if ( IsLast(P,L))
{
Temp = P ;
P→Blink→Flink – NULL;
free (Temp);
}
else
{
Temp = P;
P→Blink →Flink = P →Flink;
P→Flink→Blink = P→Blink;
Free (Temp);
}
}
Dr. SINDHIA LINGASWAMY, VIT 25
26. CIRCULAR LINKED LIST
• The pointer of the las t node points to the first node
• Can be implemented as
• Singly linked circular list
• Doubly linked circular list
Dr. SINDHIA LINGASWAMY, VIT 26
5700 20 300 1000 30 5700 300 40 1000
10 20 30