Professional Documents
Culture Documents
Lists
(Singly Linked List, Doubly Linked List and Circular List)
1
Array Declaration
int a[7];
Limitations of arrays
we can use arrays whenever we have to store and manipulate
collections of elements. But it has limitations
◦ The dimension of an array is determined the moment the array is created, and cannot be
changed later on
◦ The array occupies an amount of memory that is proportional to its size, independently of
the number of elements that are actually of interest;
◦ If we want to insert a new value in a specific position, or remove it, for each such
operation we may need to move many elements (on the average, half of the elements of
the array); this is very inefficient.
Insertion in Linear Array-an element at the beginning of the array
DATA start
LINK 105 110 117 123 0
1 110 3
2
Storage Pool
3 117 9
Node
start
4 Values
5 105 1 Storage Getnode() – provides array index x if it is a free node (eg. 2, 4, 6..)
6 functions Ret(x) – returns node x to the storage pool
Declaration int DATA[9], LINK[9], start;
7
And coding Start = 5;
8 DATA[5] = 105; LINK[5] =1;
9 123 0 Operations Insert, delete
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 3 105 110 117 123 0
2
Y 3 117 9
Y
start 4
5 105 1
Insert(X, 119) – STEPS
6 (assume X = 1)
7 Step 1 : Getnode(Y)
(assume Y = 4)
8
9 123 0
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 3 105 110 117 123 0
2
Y 3 117 9 119
3 Y
start 4 119
5 105 1
Insert(X, 119) – STEPS
6
(assume X = 1)
7 Step 1 : Getnode(Y)
8 (assume Y = 4)
Step 2: Copy data in the node Y
9 123 0 DATA[Y] = 119
Step 3 : Copy link of X to link of Y
LINK(Y) = LINK(X)
Linked List using arrays – insertion step
DATA LINK X
X start
1 110 4 105 110 117 123 0
2
Y 3 117 9 119
3 Y
start 4 119
5 105 1 Insert(X, 119) – STEPS
6 (assume X = 1)
Step 1 : Getnode(Y)
7 (assume Y = 4)
8 Step 2: Copy data in the node Y
DATA[Y] = 119
9 123 0
Step 3 : Copy link of X to link of Y
LINK(Y) = LINK(X)
Step 4 : Copy Y to link of X
LINK(X) = Y
Linked List using arrays – insertion result
DATA LINK
start
1 110 4 105 110 117 123 0
2
3 117 9 119
3
start 4 119
5 105 1
6
7
8
9 123 0
Linked List using arrays – Deletion step
DATA LINK X Y
X
start
1 110 3 105 110 117 123 0
2
3 117 9
Y
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node Y positioned after X)
(assume X = 1 and so Y is 3)
7
8
9 123 0
Linked List using arrays – Deletion step
DATA LINK X Y
X
start
1 110 9 105 110 117 123 0
2
3 117 9
Y
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node positioned after X)
(assume X = 1 and so Y is 3)
7 Step 1 : detach / delink the node Y
8 LINK(X) = LINK(Y)
9 123 0
Linked List using arrays – Deletion step
DATA LINK X
X
start
1 110 9 105 110 123 0
2
3
4
5
start 105 1 Delete (X, Y) – STEPS
6 (delete the node positioned after X)
(assume X = 1 and so Y is 3)
7 Step 1 : detach / delink the node
8 LINK(X) = LINK(Y)
Step 2: delete / remove node Y
9 123 0 RET(Y)
Linked List using arrays – Deletion result
DATA LINK
start
1 110 9 105 110 123 0
2
3
4
5
start 105 1
6
7
8
9 123 0
Linked List using arrays – Additional operations
1. display(start) – algorithm to display the data in the linked list – start
2. InsertFront(int list, int d) – algorithm to insert the data d in the first position of the linked list
3. DeleteFront(int list)– algorithm to delete the first node of the linked list
4. InsertEnd(start, data) – algorithm to insert the data d at last position in the linked list
5. DeleteEnd(start, data) – algorithm to delete last node in the linked list
start
105 110 117 123 0
Linked List using arrays – Operations - Display
start
105 110 117 123 0
Step1 : p = start;
Step 2 : while (p != 0)
Step 3 : print(DATA[p])
Step 4 : p = LINK[p]
Step 5 : Endwhile
Step 6 : End display
start
105 110 117 123 0
p
Linked List using arrays – Operations - InsertFront
list
105 110 117 123 0
list
105 110 117 123 0
d
p
list
105 110 117 123 0
p
list
p
105 110 117 123 0
list
110 117 123 0
DeleteFront(int list)– algorithm to delete the first node of the linked list
DeleteFront(int list)
Step1 : p = list;
Step 2 : list = LINK[list];
Step 3 : RET(p);
Step 4 : End DeleteFront
Linked List using arrays – Operations - InsertEnd
start
105 110 117 123 0
start
105 110 117 123 0
p
Y
start
105 110 117 123 d 0
p
InsertEnd(start, data) – algorithm to insert the data d at last position in the linked list
InsertEnd(int start, int d)
Step1 : p = start;
Step 2 : while (LINK[p] != 0)
Step 3 : p = LINK[p]
Step 4 : Endwhile
Step 5 : Getnode(Y);
Step 6 : DATA[Y] = d
Step 7 : LINK[p] = Y
Step 8 : LINK[Y] = 0
Step 9 : End InsertEnd
Linked List using arrays – Operations - DeleteEnd
start
105 110 117 123 0
start
105 110 117 123 0
p q
start
105 110 117 0 123 0
p q
void create();
void display();
void insert_begin();
void insert_end();
void delete_begin();
void delete_end();
START2
Inserting a Node at the Beginning of a Circular Linked List
Inserting a Node at the End of a Circular Linked List
Deleting a Node from a Circular Linked List
Deleting the Last Node from a Circular Linked List
Doubly Linked Lists
START
0 H E L L O 0
DLL Using Arrays – Node structure with 3 arrays
Input
Step 1
NEW_NODE
Step 2
Output
Doubly Linked List Operations - Insertion
Inserting a Node at the End of a Doubly Linked List
Input
Step 1
Step 2
Step 3
Result
Doubly Linked List Operations - Insertion
Inserting a Node After a Given Node in a Doubly Linked List
Input
Step 1
Step 2
Step 3
Step 4
Output
Doubly Linked List Operations - Deletion
Deleting the First Node from a Doubly Linked List
Input
Step 1
Output
Doubly Linked List Operations - Deletion
Deleting the Last Node from a Doubly Linked List
Input
Step 1
Step 2
Step 3
Output
Doubly Linked List Operations - Deletion
Deleting the Node After a Given Node in a Doubly Linked List
Input
Step 1
Step 2
Step 3
Step 4
Output
APPLICATIONS OF LINKED LISTS
◦ Linked lists can be used to represent polynomials and the different operations that can
be performed on them.
◦ Consider a polynomial 6x3 + 9x2 + 7x + 1.
◦ Every individual term in a polynomial consists of two parts, a coefficient and a power.
◦ Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their
powers respectively.
◦ The linked list representation of the polynomial
Polynomial Addition
Polynomial Addition using Single Linked list
List 1 = 5X3+2X2+4
List 2 = 8X2+10X+3
R-List = 5X3+10X2+10X+7
struct node
{
int co-ef, exp;
struct node *next;
};