DATASTRUCTURE
LINEAR NONLINEAR
ARRAY
TREES&
LINKLIST STACK
QUEUE GRAPH
Linked list
Linked list is a linear data structure. It contains nodes. Each
node contains two parts, i.e. DATA part and LINK part.
The data contains elements and
Link contains address of another node.
Node
Data Link/Address
Start
25 10 15 /
10
START
14 08 30 10 42 N
05 08 10
• The above figure shows the example of marks
obtained by different students can be stored in a
linked list
• Here N-stands for NULL.
• Null indicates the end of the node.
Operations on Linked Lists
The basic operations on linked lists are
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
6. Concatenation
7. Display
Operations on Linked Lists
1. The creation operation is used to create a linked list.
2. Insertion operation is used to insert a new node in the
linked list at the specified position. A new node may be
inserted at the beginning of a linked list , at the end of
the linked list , at the specified position in a linked list.
If the list itself is empty , then the new node is inserted
as a first node.
3. Deletion operation is used to delete on item
from the linked list. It may be deleted from the
beginning, end or specified position in the list.
4. Traversing operation is a process of going
through all the nodes of a linked list from one
end to the another end.
If we start traversing from the first node towards
the last node, It is called forward traversing.
If the traversal start from the last node towards the
first node , it is called back word traversing.
5. Searching operation is a process of
accessing the desired node in the list. We
start searching node –by-node and compare
the data of the node with the key.
6. Concatenation operation is the process of
appending the second list to the end of the
first list. When we concatenate two lists,
the resultant list becomes larger in size.
7. The display operation is used to print each
and every node’s information.
Limitations of Arrays
Arrays are simple to understand and elements of
an array are easily accessible. But arrays have
some limitations.
Arrays have a fixed dimension.
Once the size of an array is decided it can not
be increased or decreased during execuation.
Limitations of Arrays
Array elements are always stored in contiguous memory
locations.
Operations like insertion or deletion of the array are
pretty tedious.
To over come this limitations we use linked list.
Why Linked list ?
• Suppose you are writing a program which will store marks of 100
students in math's. Then our logic would be like this during compile
time –
• int marks[100];
• Now at run time i.e. after executing program if number of students are
101 then how you will store the address of 101th student ?
• Or if you need to store only 40 students then again you are wasting
memory unnecessarily.
• Using linked list you can create memory at run time or free memory at
run time so that you will able to fulfil your need in efficient manner.
Types of Linked Lists
1. Single linked list
2. Double linked list
3. Circular linked list
4. Circular double linked list
SINGLE LINKED LIST :-
A single linked list is one in which all nodes are linked
together in some sequential manner.
Start
500 45 100 60 200 35 N
Singly Linked List
First
10 1000 15 2000 20 NULL
4000 1000 2000
Circular Linked List
• A circular linked list is one which has no beginning and no ending.
The null pointer in the last node of a linked list is replaced with the
address of its first node such a list is called circular linked list.
• Last node contains the address of the first node
45 100 60 200 35 400
400 100 200
Doubly Linked list
A single linked list has some disadvantages
That it can traverse it in one direction.
Many applications require searching backward and forward
travelling sections of a list.
Doubly Linked list
• A two way list is a linear collection of data elements
called nodes.
• When each node is divided into three parts. They are two
link parts and one data part.
node
prev data next
Doubly Linked list
First
NULL 10 2000 1000 15 3000 2000 20 NULL
1000 2000 3000
Contains the address of previous node and next node
Circular Doubly Linked list
First
3000 10 2000 1000 15 3000 2000 20 1000
1000 2000 3000
Contains the address of first node and last node
Insert into a Linked List
START
Node A Node B
Insert into a Linked List
START
Node A Node B
START
(a). Before Insertion
Node A Node B
------------------------
Node N
(b). After Insertion
Insert into a Linked List
START
Data list
Node A Node B
AVAIL
Node N
Free-storage list
Insert into a Linked List
START
Data list
Node A Node B
------------------------
AVAIL
Node N
....
Free-storage list
Insert into a Linked List
1. The nextpointer field of node A now points to the new node N, to
which AVAIL previously pointed.
2. AVAIL now points to the second node in the free pool, to which
node N previously pointed.
3. The nextpointer field of node N now points to node B, to which
node A previously pointed.
Deletion from a Linked List
START
Node A Node B
START
(a). Before Deletion
Node A Node N Node B
(b). After Deletion
Deletion from a Linked List
START
Node A Node N Node B
AVAIL
------
------
------
------
------
------
--- ....
Free-storage list
Deletion from a Linked List
1. The nextpointer field of node A now points to the new node B,
where node N previously pointed.
2. The nextpointer field of node N now points to the original first node
in the free pool, where AVAIL previously pointed.
3. AVAIL now points to the deleted node N.
Wastage of Memory
Pointer Requires extra memory for storage.
Suppose we want to store 3 integer data items then we have to allocate memory –
In case of array –
Memory Required in Array = 3 Integer * Size
= 3 * 2 bytes
= 6 bytes
In case of array –
Memory Required in LL = 3 Integer * Size of Node
= 3 * Size of Node Structure
= 3 * Size(data + address pointer)
= 3 * (2 bytes + x bytes)
= 6 bytes + 3x bytes
(H.W)
Advantages and Disadvantages of Linked List
Applications (H.W)
(H.W)
Difference Between Array and Linked List