Unit 4 2
Unit 4 2
Linked List
4.1 Pointers Revision
Pointer: A pointer is a variable which contains an address of another variable in memory.
Declared by * indicator..
We can create a pointer variable in C using
following syntax:
Declaration:int*ptr
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:
1. Malloc ( )
2. Calloc( )
Prepared By: Department of Computer Engineering Page 1
Subject Name: Data Structure And Algorithm Unit No: 4 Subject Code: 4330704
3. Realloc( )
4. Free ( )
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)
2... Address or printer to next node (LINK).
NEW_NODE=AVAIL
AVAIL = AVAIL->LINK
FIRST = NEW_NODE
Else
Step 3: Exit
FIRST = NEW_NODE
Step 4: Exit
Else
NEW_NODE=AVAIL
AVAIL = AVAIL->LINK
FIRST = NEW_NODE
Else
SAVE = FIRST
SAVE = SAVE->LINK
SAVE->LINK = NEW_NODE
Step 3: Exit
Else
NEW_NODE=AVAIL
AVAIL = AVAIL->LINK
Else
SAVE = FIRST
PRED = SAVE
SAVE = SAVE->LINK
If X = SAVE->INFO then
NEW_NODE->LINK= SAVE->LINK
SAVE->LINK=NEW_NODE
Else
Step 3: Exit
Prepared By: Department of Computer Engineering Page 5
Subject Name: Data Structure And Algorithm Unit No: 4 Subject Code: 4330704
Return FIRST->INFO
FIRST=NULL
Else
Return FIRST->INFO
FIRST=FIRST->LINK
Step 3: Exit
Return FIRST->INFO
FIRST=NULL
Else
SAVE=FIRST
PRED=SAVE
SAVE=SAVE->LINK
Return SAVE->INFO
PRED->LINK=NULL
Step 3: Exit
Step 1: FLAG = 0
SAVE=FIRST
FLAG = 1
SAVE=SAVE->LINK
Else
SAVE=SAVE->LINK
Else
Step 5: Exit
Step 1: Count = 0
SAVE = FIRST
SAVE=SAVE->LINK
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 below:
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.
4.9 Difference between circular linked list and singly linked list
Every node is accessible from a given node, that is from this given node be reached by chaining
through the list.
To delete a node from a singly linked list, it is required to have the first node address. Such a
requirement does not exist for a circular linked list.
Certain operations such as splitting, concatenation, becomes more efficient in circular link list.
The disadvantage of a circular list is, without some care in processing, it is possible to get into an
infinite loop !......
In circular linked list, the detection of the end by placing a special node easily identified in the
circular list is called a list head.
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous
as well as the next node in the sequence.
Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node
in sequence (next pointer), and pointer to the previous node (previous pointer).
A sample node in a doubly linked list is shown in the figure.
4.11 Difference between Doubly linked list and singly linked list