Unit-4 Linked-List
Unit-4 Linked-List
Unit-4 Linked-List
LINKED LIST
LINKED LIST SYLLABUS:
2
CONTINUE…
Deleting the first and last node from a linked list,
Searching a node in Linked List, Count the
number of nodes in linked list.
4.8 Concepts of circular linked list
4
4.1POINTERS REVISION
5
4.2 REVISION OF STRUCTURES
6
CONTINUE…
Definition of a structure:
struct <struct-type>{
<type> <identifier_list>; Each identifier
<type> <identifier_list>; defines a member
... of the structure.
} ;
Example:
struct Date { The “Date” structure
int day;
int month; has 3 members,
int year; day, month & year.
} ;
7
4.3 REVISION OF STRUCTURE USING POINTERS
8
4.4 DYNAMIC MEMORY ALLOCATION
In the Dynamic memory allocation , the memory
is allocated to a variable or program at the run
time.
The only way to access this dynamically allocated
memory is through pointer.
Types of dynamic memory allocation.
Malloc( )
Calloc( )
Realloc( )
Free( )
9
4.5 LINK LIST REPRESENTATION
Representation
10
CONTINUE…
A singly linked list is to expand each node to
contain a link or pointer to the next node. This is
also called as a one way chain.
First contain the address of the first node of the
lists..
Each node in the list consist of two parts::
1.. Information(INFO)
11
4.6 TYPES OF LINK LIST
Singly Linked List
Singly Circular Linked List
12
SINGLY LINKED LIST
14
DOUBLY LINKED LIST
16
4.7 BASIC OPERATIONS OF LINKED LIST
Creation: This operation is used to create
constituent node as and when required.
Insertion: This operation is used to insert a new
node in the linked list.
At the beginning of the list
At a certain position and
At the end.
Deletion: This operation is used delete node
from the list.
At the beginning of the list
At a certain position and
At the end. 17
CONTINUE…
Other Linked list common operation :
Traversing: It is a process of going through all
the nodes of a linked list from the end to the
other end.
Concatenation: The process of appending the
second list to the end of the first list.
Display: This operation is used to print each and
every node’s information.
18
4.8 CONCEPT OF CIRCULAR LINKED LIST
A list in which last node contains a link or
pointer to the first node in the list is known as
circular linked list..
Representation of circular linked list is shown
bellow:
19
CONTINUE…
In a circular linked list there are two methods to
know if a node is the first node or not.
Either a external pointer, list, points the first
node or
A header node is placed as the first node of the
circular list.
The header node can be separated from the
others by either heaving a sentinel value as the
info part or having a dedicated flag variable to
specify if the node is a header node or not.
20
CONTINUE…
The structure definition of the circular linked
lists and the linear linked list is the same:
struct node{ int info;
struct node *next;
};
typedef struct node *NODEPTR;
21
CONTINUE…
22
ALGORITHM COUNT NUMBER OF NODES IN
SINGLE LINKED LIST
Step 1: Count = 0
SAVE = FIRST
Step 2: Repeat step 3 while SAVE ≠ NULL
Step 3: Count= Count + 1
SAVE=SAVE->LINK
Step 4: Return Count
23
ALGORITHM TO SEARCH NODE IN SINGLE
LINKED LIST
Step 1: FLAG = 0
SAVE=FIRST
Step 2: Repeat step 3 while SAVE ≠ NULL
Step 3: If SAVE->INFO = X then
FLAG = 1
SAVE=SAVE->LINK
Else
SAVE=SAVE->LINK
Step 4: If FLAG = 1 then
Write “Search is Successful”
Else
Write “Search is not successful” 24
Step 5: Exit
INSERT NEW NODE IN ORDERED SINGLY
LINKED LIST
26
CONTINUE…
NEW_NODE->LINK=NULL
SAVE->LINK = NEW_NODE
Else
NEW_NODE->INFO = X
NEW_NODE->LINK=SAVE
PRED->LINK = NEW_NODE
Step 3: Exit
27
CONTINUE…
NEW_NODE->INFO = X
NEW_NODE->LINK=SAVE
PRED->LINK = NEW_NODE
Step 3: Exit
28
ALGORITHMS FOR SINGLY LINKED LIST
BEGINING
30
ALGORITHM TO INSERT NEW NODE AT END
OF LINKED LIST
32
ALGORITHM TO INSERT NEW NODE AT
SPECIFIC LOCATION SINGLE LINK LIST
34
DELETE FIRST NODE FROM SINGLE LINKED
LIST
35
ALGORITHM TO DELETE LAST NODE FROM
SINGLE LINKED LIST
Step1.check[overflow] if(ptr=NULL)
write: overflow and exit
Step2.set Info[ptr]=>item;
Step3.if(start=>NULL)
Step4.set prev[ptr] => next[ptr] => NULL
Step5.set start => end => ptr
Else
Step 6.set prev[ptr] => NULL
Step7. next[ptr] => start
Step8. set prev[start] => ptr 37
CONTINUE…
Step9.set start = >ptr
[end if]
Step10.Exit.
38
INSERTION AT LOCATION IN DOUBLY LINKED
LIST
41
INSERTION AT LAST IN DOUBLY LINKED LIST
43
DELETE AN ELEMENT AT BEGINNING
OF DOUBLY LINKED LIST
45
CONTINUE…
free(first)
first=>ffirst
prev[first]=>NULL
end if
Step 3: exit
46
DELETE AN ELEMENT AT LAST OF
DOUBLY LINKED LIST
47
CONTINUE…
if first=>last then
first=>NULL
last=>NULL
free(first)
else
t=prev[last]
free(last)
last=>t
next[last]=>NULL
Step 3: exit 48
DELETE AN ELEMENT AT ANY PLACE
DOUBLY LINKED LIST
52
4.7 CONCEPT OF CIRCULAR LINKED LIST
A list in which last node contains a link or
pointer to the first node in the list is known as
circular linked list..
Representation of circular linked list is shown
bellow:
53
4.7 CONCEPT OF CIRCULAR LINKED LIST
A list in which last node contains a link or
pointer to the first node in the list is known as
circular linked list..
Representation of circular linked list is shown
bellow:
54
CONTINUE…
In a circular linked list there are two methods to
know if a node is the first node or not.
Either a external pointer, list, points the first
node or
A header node is placed as the first node of the
circular list.
The header node can be separated from the
others by either heaving a sentinel value as the
info part or having a dedicated flag variable to
specify if the node is a header node or not.
55
CONTINUE…
The structure definition of the circular linked
lists and the linear linked list is the same:
struct node{ int info;
struct node *next;
};
typedef struct node *NODEPTR;
56
57