2. Objectives
Recognize need of a data structure, which dynamically can shrink
and grow
Realization of linked list as dynamic data structure to
understand the well-defined, clear, and simple approach of
program design
Utilize flexibility of the same easily and effectively to understand
sequential organization of data
Learn variants of linked list and use them for appropriate
applications
4. Introduction To Static & Dynamic Memory
Allocation
Sr. No. Static Memory Dynamic Memory
1. Static memory allocation is done
at compile time
Memory allocation is done at run
time
2. Prior to allocation of memory
some fixed amount of it must be
decided
No need to know amount of
memory prior to allocation
3. Wastage of memory or shortage
of memory
No wastage of memory or
shortage of memory
4. Faster Execution than dynamic
memory
Slower execution than static
memory
5. Ex. Arrays Ex. Linked list
5. Memory Model
Stack can grow in downward direction
OS Program & Program code
Memory for static variables
Stack memory for local variables
Heap Memory
Heap can grow in upward direction
C/C++ uses the memory which is divided into 4 parts Program code, static
area, local data and heap
Static area stores static variables, global data
The stack is for local variables
Heap memory is to allocate and de-allocate memory at run time.
Stack and heap are part of dynamic memory management. These areas can
grow towards each other.
6. malloc: Allocating a block of memory
6
General form
int *ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type
The malloc() returns a pointer (of cast-type) to an area of memory with size
byte-size. If there is not enough space a NULL pointer is returned.
Example
• x=(int *) malloc(100*sizeof(int));
• cptr=(char *) malloc(10);
• st= (struct *) malloc(sizeof(struct store));
C++ supports malloc function and also has another operator new that
perform the task of allocating
Example int *p = new int[10]
7. calloc: Allocating multiple block of memory
7
While malloc() allocates a single block of storage space,
calloc() allocates multiple blocks of storage, each of the same
size, and then set all bytes to zero. If there is not enough space
a NULL pointer is returned.
General form
ptr=(cast-type *) calloc(n, element-size);
Example ptr = (int*) calloc(5, 5*sizeof(int))
ptr = (float*) calloc(25, sizeof(float));
8. realloc( ); Altering the size of a block
8
It is likely that the previous allocated memory is not
sufficient and we need additional space for more elements.
It is also possible that the memory allocated is much
larger than necessary and we want to reduce it.
General form
ptr=realloc(ptr, 100);
• Modifies the size of previously allocated space by malloc() or
calloc()
Example
10. free( ); Releasing the used space
1
0
“free” method in C is used to dynamically de-
allocate the memory.
The memory allocated using functions malloc() and
calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the dynamic
memory allocation takes place.
It helps to reduce wastage of memory by freeing it.
General form
free(ptr_name);
Example
free(ptr); // available in both C & C++
delete ptr; // available in C++ only
12. WHAT IS A LINKEDLIST?
1
2
A linked list is a collection of items:
It can have an arbitrary length
Objects / elements can be inserted or removed at
arbitrary locations in the list
A list can be traversed in order one item at a time
Every linked list having two fields
Data next
10 5010
1005
1005
Head
8 2010
5010
20 NULL
2010
13. LIST AS AN ADT
1
3
Linked List, a linear collection of data items, called
nodes, where order is given by means of pointers.
Elements:
Each node is divided into two parts:
Data
Link ( pointing towards the next node)
(Common) Operations of Linked List
IsEmpty: determine whether or not the list is empty
InsertNode: insert a new node at a particular position
FindNode: find a node with a given value
DeleteNode: delete a node with a given value
DisplayList: print all the nodes in the list
14. LINKED LIST TERMINOLOGIES
Traversal of List
Means to visit every element or node in the list
beginning from first to last.
Predecessor and Successor
In the list of elements, for any location n, (n-1) is
predecessor and (n+1) is successor
In other words, for any location n in the list, the left
element is predecessor and the right element is
successor.
Also, the first element does not have predecessor
and the last element does not have successor. 6
17. Singly LINKED LISTS
Each node contains at least
A piece of data (any type)
Pointer to the next node in the list
Head: pointer to the first node
The last node points to NULL
https://youtu.be/iNUS9iZwrVA
A
Head
A linked list is a series of connected nodes
B C
A
data pointer
node
17
18. LISTS – ANOTHER PERSPECTIVE
A list is a linear collection of varying length of
homogeneous components.
Homogeneous: All components are of the same
type.
Linear: Components are ordered in a line (hence
called Linear linked lists).
18
20. AN INTEGER LINKED LIST
Head
10 13 5 2
First Node of List
data next NULL
Last Node of List
20
21. THE NULL POINTER
21
NULL is a special pointer value that does not reference
any memory cell.
If a pointer is not currently in use, it should be set to
NULL so that one can determine that it is not pointing
to a valid address:
int *p;
p = NULL;
22. CREATING A LIST NODE
struct Node {
int data; // data in node
Node *next;
};
// Pointer to next node
Node *p;
p = new Node;
p - > data = 10;
p - > next = NULL;
p 10
22
23. CREATING A LIST NODE
class Node {
public:
int data; // data in node
Node *next; // Pointer to next node
};
Node *p;
p = new Node;
p -> data = 10;
p -> next = NULL;
p 10
23
24. JOINING TWO NODES
Node *p, *q;
p = new Node;
p - > data = 10;
p - > next = NULL;
q = new Node;
q - > data = 6;
q - > next = NULL;
p - > next = q;
p 10
q 6
6
p 10
q
Basic Concepts
20
25. Expression
p
p - > data
p - > next
p - > next -
p - > next -
> data
> next
6
p 10
ACCESSING LIST DATA
Node 1 Node 2
Value
Pointer to first node (head)
10
Pointer to next node
6
NULL pointer
Basic Concepts
21
26. BASIC LINKED LIST OPERATIONS
31
Traversing through the list
Node Insertion
Insertion at the beginning of the list
Insertion at the end of the list
Insertion in between of the list
Node Deletion
Deletion at the beginning of the list
Deletion at the end of the list
Deletion from in between node of the list
27. NODE INSERTION
36
Insertion at the beginning of the list
Insertion at the end of the list
Insertion in the in between of the list
28. NODE INSERTION AT THE BEGINNING
Steps:
Create a node
Set the node data values
Connect the pointers
48 17 142
head /
Step 1
Step 2
Initial list
List after Step 3
head 93
29. NODE INSERTION AT THE BEGINNING
48 17 142
head //
Initial list
Node *ptr;
ptr = new Node;
ptr - > data = 93;
ptr - > next = head;
head = ptr; 93
head 93
Rearrange:
ptr
ptr
head
30. NODE INSERTION AT THE BEGINNING
Node * insert_beg(Node *head, int x)
{
Node *ptr;
ptr = new Node;
ptr -> data = X;
ptr -> next = NULL;
if(head = = NULL)
{
return (ptr);
}
ptr -> next = head
head = ptr;
return (head)
}
31. NODE INSERTION AT THE END
Steps:
Create a Node
Set the node data values
Connect the pointers
48 17 142
head //
Step 1 Step 2
List after Step 3
Initial list
32. NODE INSERTION AT THE END
48 17 142
head //
Initial list
ptr
Node * ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
Need a pointer at the last
node
33. NODE INSERTION AT THE END
48 17 142
head //
Initial list
ptr
Node * p;
p = new Node;
p -> data = 150;
p -> next = NULL;
150 //
p
34. NODE INSERTION AT THE END
48 17 142
head
ptr
ptr -> next = p;
150 //
p
35. NODE INSERTION AT THE END
Node * insert_end(Node *head, int x)
{
Node *ptr, *p;
p = new Node;
p -> data = X;
p -> next = NULL;
if(head = = NULL)
{
return (p);
}
ptr = head;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = p
return (head)
}
36. NODE INSERTION IN-BETWEEN
Steps:
Create a Node
Set the node data values
Break pointer connection
Re-connect the pointers
Step 1 Step 2
Step 3
Step 4
37. NODE INSERTION IN-BETWEEN
Need a pointer on the node after which a new node is to be
Inserted. For instance, if new node is to be inserted after
the node having value ‘17’, we need a pointer at this node
ptr
How to get pointer on desired node?
38. NODE INSERTION AT ARBITRARY POSITION
Suppose we want to insert a node after the node having
value ‘x’:
ptr
Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}
39. NODE INSERTION AT ARBITRARY POSITION
ptr
Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}
Node * p;
p = new Node;
p -> data =
100; p -> next
= NULL;
150 //
p
40. NODE INSERTION AT IN-BETWEEN / ARBITRARY
ptr
Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}
Node * p;
p = new Node;
p -> data = 150;
p -> next = NULL;
p -> next = ptr -> next;
ptr -> next = p;
150
p
41. NODE INSERTION IN-BETWEEN
Node * insert_between(Node *head, int x, int y)
{
Node *ptr, *p;
p = new Node;
p -> data = X;
p -> next = NULL;
if(head = = NULL)
{
return (p);
}
ptr = head;
while(ptr -> data != y)
ptr = ptr -> next;
p -> next = ptr -> next;
ptr -> next = p;
return (head)
}
42. NODE DELETION
Deleting from the beginning of the list
Deleting from the end of the list
Deleting from in-between of the list
43. DELETING FROM THE BEGINNING
Steps:
Take head pointer to 2nd node
Free the 1st node
4 17
head 42
6
4 17
head
42
6
4 17
head 42
44. DELETING FROM THE BEGINNING
4 17 42
6
4 17 42
6
4 17
head 42
Node * ptr;
ptr = head;
head = head ->next;
delete ptr;
head
ptr
head
head
ptr
45. NODE DELETION FROM BEGINNING
Node * delete_beg(Node *head)
{
Node *ptr;
if(head = = NULL)
{
cout<<“No element available to delete”;
return;
}
ptr = head;
head = head -> next;
free(ptr)
return (head)
}
46. DELETING FROM THE END
Steps:
Take pointer at the end of list
Set previous node pointer to NULL
Delete the node
4 17
6
head
4 17
6
head 42
47. DELETING FROM THE END
4 17
head 42
6
We need a pointer one node before the node to be deleted
p
Node * p;
Node * q;
p = head;
while (p -> next -> next != NULL)
{
p = p -> next;
}
q
q = p -> next;
delete (q);
P -> next = NULL
48. NODE DELETION FROM END
Node * delete_end(Node *head)
{
Node *p, *q;
if(head = = NULL)
{
cout<<“No node available to delete”;
return;
}
p = head;
while (p -> next -> next != NULL)
{
p = p -> next;
}
q = p -> next;
delete (q);
P -> next = NULL;
return (head);
}
49. DELETING IN-BETWEEN NODE
Steps:
Set previous Node pointer to next node
Break Node pointer connection
Delete the node
4 17 42
head
4 17
head 42
4
head 42
6
6
6
50. DELETING FROM IN-BETWEEN POSITION
4 17 42
head
We need a pointer on the node to be deleted (as well a pointer to one
node before the node to be deleted)
p q
Node * p;
Node * q;
p = head;
while(p -> next -> data != x)
{
p = p -> next;
}
q = p -> next;
p -> next = q -> next;
delete (q)
Given the value of the node to
be deleted, assume this to be
variable ‘x’
Keep moving a pointer until the
required node is reached
6
51. NODE DELETION FROM END
Node * delete_between(Node *head)
{
Node *p, *q;
if(head = = NULL)
{
cout<<“No node available to delete”;
return;
}
p = head;
while (p -> next -> data != x)
{
p = p -> next;
}
q = p -> next;
p -> next = q -> next;
delete (q);
return (head);
}
52. PROS AND CONS OF LINKED LISTS
• Access any item as long as external link to first item
maintained
• Insert new item without shifting
• Delete existing item without shifting
• Can expand/contract as necessary
• Overhead of links: used only internally, pure overhead
• No longer have direct access to each element of the
list
• We must go through first element, and then second,
and then third, etc. 57
54. 9 17 22 26 34
first
ptr
9 17 22 26 34
firs t
pt r
.
.
9 17 22 26 34
firs t
pt r
9 17 22 26 34
firs t
ptr
ptr = first;
while (ptr != null_value)
{
Process data part of
node pointed to by ptr;
ptr = next part of node
pointed to by ptr;
}
34
55. TRAVERSING THE LIST
35
Node * currNode;
currNode = head;
while (currNode != NULL)
{
cout<< currNode->data;
currNode = currNode->next;
}
56. Circular linked list
A circular linked list is one which
has
No ending.
The null pointer in the last node of a linked list is
replaced with the address of its first node .
58. Operations on linked list
The basic operations on
linked lists are :
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
59. The creation operation is used to create a
linked list.
There are two fields in singly circular inked
list.
Data - any type
Next – a pointer to the next node
C/C++ representation
struct node
{
int data;
struct node *next;
}
What Is creation
60. Allocate a new node
Insertnew element
Makenewnodepointto
null
Createheadtopointto
newnode
Algorithm
p = new node
p -> data = x
p -> next = NULL
Rear = p
Rear -> next = Rear
What Is creation of CLL
Rear
93
p
61. NODE INSERTION IN CLL
36
Insertion at the beginning of the list
Insertion at the end of the list
Insertion in the in between of the list
62. NODE INSERTION AT THE BEGINNING OR END
Steps:
Create a node
Set the node data values
Connect the pointers
48 17 142
Rear
Step 1
Step 2
Initial list
List after Step 3
Rear
93
63. NODE INSERTION AT THE BEGINNING OR END
Initial list
Node *p;
p = new Node;
p -> data = 93;
p -> next = NULL;
p ->next = Rear->next;
Rear -> next = p
93
p
p
48 17 142
Rear
93
p
Rear
64. NODE INSERTION AT THE BEGINNING OR END
Rearrange
Rear
93
Final List
Rear
93
Rear = p
p
65. NODE INSERTION AT THE BEGINNING OR END
Node * insert_beg_end(Node *Rear, int x)
{
Node *p;
p = new Node;
p -> data = X;
p -> next = NULL;
if(Rear = = NULL)
{
Rear = p;
p -> next = p
return (Rear);
}
else
{
p -> next = Rear -> next
Rear -> next = p;
Rear = p;
return (Rear)
}
}
66. NODE INSERTION IN BETWEEN
Steps:
Create a node
Set the node data values
Connect the pointers
48 17 142
Rear
Step 1
Step 2
Initial list
List after Step 3
Rear
93
48 17 142
67. NODE INSERTION IN BETWEEN
Initial list
Node *p;
p = new Node;
p -> data = 93;
p -> next = NULL;
q = Rear;
While(q->data != y)
q = q -> next;
p ->next = q ->next;
q -> next = p
93
p
p
48 17 142
Rear
93
p
Rear
q
68. NODE INSERTION IN BETWEEN
p
93
48 17 142
Rear
Re-arranging
Rear = p
p
93
48 17 142
Rear
69. NODE INSERTION IN-BETWEEN
Node * insert_between(Node *Rear, int x, int y)
{
Node *p;
p = new Node;
p -> data = x;
p -> next = NULL;
if(Rear = = NULL)
{
return (p);
}
q = Rear;
while(q -> data != y)
q = q -> next;
p ->next = q ->next;
q -> next = p;
return (Rear);
}
70. NODE DELETION FROM BEGINNING OR END
48 17 142
Rear
Initial list
List after
deletion
Rear
93
71. NODE DELETION FROM BEGINNING OR END
Initial list
Rear
93
Node *p;
p = Rear;
while(p -> next != Rear)
p = p -> next; Rear
93
p
72. NODE DELETION FROM BEGINNING OR END
48 17 142
Rear
Rear
93
Rear -> next = p -> next;
delete p;
p
p
93
Rear
Rear = p;
p = p -> next;
73. NODE DELETION FROM BEGINNING OR END
Node * delete_beg_end(Node *Rear)
{
Node *p;
if(Rear = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = Rear;
while(p -> next != Rear)
p = p -> next;
Rear = p;
p = p -> next;
Rear -> next = p -> next;
delete(p);
return (Rear)
}
74. NODE DELETION FROM IN BETWEEN
48
Rear
Initial list
List after
deletion In
Between node
Rear
93
142 93
75. NODE DELETION FROM IN BETWEEN
Initial list
Rear
93
p = Rear;
while(p -> next –> data != x)
p = p -> next;
Rear
93
p
76. NODE DELETION FROM IN BETWEEN
Rear
93
q = p -> next
p q
p -> next = q -> next;
delete (q);
48
Rear
List after
deletion In
Between node
142 93
77. NODE DELETION FROM IN-BETWEEN
Node * delete_between(Node *Rear, int x)
{
Node *p, *q;
if(Rear = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = Rear;
while(p -> next -> data != x)
p = p -> next;
q = p -> next;
p -> next = q -> next;
delete(q);
return (Rear);
}
78.
79. Doubly LinkedList
In doubly linked list each node contains two pointers.
Each pointer points to either next node or previous node
Doubly linked list is two-way list because one can move
either from left to right or from right to left.
Each node of linked list consist of three fields
a. data
b. Prev, next: Address of previous and next node
80. Examples of Doubly Linked Lists
5000
2000
5000
5000 3000
2000
2000
3000
head
1000
3000
1000
1000 2000
3000
3000
2000
head
5000
2000
5000
5000 3000
2000
2000
3000
head
81. Operations on a Doubly linked list (DLL)
1) Create list.
2) Insert element at beginning in list.
3) Insert element at end in list.
4) Insert element in-Between list.
5) Delete element from the beginning of list.
6) Delete element from the end of list.
7) Delete element from in-Between list.
8) Traversing
82. Creation of Doubly linked list (DLL)
struct dnode
{
int data;
struct dnode *prev, *next;
}
5000
2000
5000
5000 3000
2000
2000
3000
head
83. Pseudo code for creation of DLL
dnode * create()
{
dnode *head,*p, *q;
int i, n, x;
cout<<“Enter no. of elements”;
cin>>n;
for(i=0; i<n; i++)
{
cout<< “Enter next data”;
p = new dnode;
cin>> p -> data;
p -> prev = p -> next = NULL;
if(head = = NULL)
head = p;
else
{
q = head;
while(q -> next != NULL)
q = q -> next;
q -> next = p;
p -> prev = q;
}
}
return (head);
}
84. Insertion in Doubly linked list (DLL)
1) Insert element at beginning in list.
2) Insert element at end in list.
3) Insert element in-Between list.
85. Insertion at beginning of DLL
5000
2000
5000
5000
2000
head
Initial List
3000
dnode *p;
p = new dnode;
p -> data = 20;
p -> prev = NULL;
p -> next = NULL
p
3000
5000
3000
3000 2000
5000
5000
2000
head
86. Insertion at beginning of DLL
p -> next = head
p
5000
5000
3000
3000 2000
5000
5000
2000
head
5000
5000
3000
2000
5000
5000
2000
head
p
head -> prev = p
87. Insertion at beginning of DLL
head = p;
3000
5000
3000
3000 2000
5000
5000
2000
head
p
Re-arranging
3000
5000
3000
3000 2000
5000
5000
2000
head
88. NODE INSERTION AT BEGINNING OF DLL
dnode * insert_beg(dnode *head, int x)
{
dnode *p;
p = new dnode;
p -> data = x;
p -> prev = NULL;
p -> next = NULL
if(head = = NULL)
{
return (p);
}
p -> next = head;
head -> prev = p;
head = p;
return (head);
}
89. Insertion at end of DLL
5000
2000
5000
5000
2000
head
Initial List
3000
dnode *p;
p = new dnode;
p -> data = 20;
p -> prev = NULL;
p -> next = NULL
p
5000
2000
5000
5000 3000
2000
2000
3000
head
90. Insertion at end of DLL
q = head;
while(q -> next != NULL)
q = q -> next;
q -> next = p
5000
3000
2000
5000
5000 3000
2000
head
p
p -> prev = q
q
5000
2000
3000
2000
5000
5000 3000
2000
head
p
q
91. Insertion at end of DLL
Re-arranging
5000
2000
5000
5000 3000
2000
2000
3000
head
92. NODE INSERTION AT END OF DLL
dnode * insert_end(dnode *head, int x)
{
dnode *p, *q;
p = new dnode;
p -> data = x;
p -> prev = NULL;
p -> next = NULL
if(head = = NULL)
{
return (p);
}
q = head;
while(q -> next != NULL)
q = q -> next;
q -> next = p;
p -> prev = q;
return (head);
}
93. Insertion at In-between of DLL
5000
2000
5000
5000
2000
head
Initial List
3000
dnode *p;
p = new dnode;
p -> data = 20;
p -> prev = NULL;
p -> next = NULL
p
5000
3000
5000
5000 2000
3000
3000
2000
head
94. Insertion at In-Between of DLL
q = head;
while(q -> data != y)
q = q -> next;
p -> next = q ->next
5000
2000
3000
2000
5000
5000
2000
head
p
p -> prev = q;
q
5000
5000 2000
3000
2000
5000
5000
2000
head
p
q
95. Insertion In-betwenn of DLL
q -> next -> prev = p;
5000
5000 2000
3000
2000
5000
3000
2000
head
p
q -> next = p
q
5000
5000 2000
3000
3000
5000
5000
2000
head
p
q
97. NODE INSERTION IN-BETWEEN OF DLL
dnode * insert_end(dnode *head, int x, int y)
{
dnode *p, *q;
p = new dnode;
p -> data = x;
p -> prev = NULL;
p -> next = NULL
if(head = = NULL)
{
return (p);
}
q = head;
while(q -> data != y)
q = q -> next;
p -> next = q ->next;
p -> prev = q;
q -> next -> prev = p;
q -> next = p
return (head);
}
98. Deletion Operations on a Doubly linked
list (DLL)
1) Deletion of element from the beginning of list.
2) Deletion of element from the end of list.
3) Deletion of element from in-Between of list.
99. Deletion from Beginning of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
3000
5000
5000 2000
3000
3000
2000
head
3000
5000 2000
3000
3000
2000
head
After Deletion:
100. Deletion from Beginning of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
dnode *p;
p = head;
5000
3000
5000
5000 2000
3000
3000
2000
head 5000
p
101. Deletion from Beginning of DLL
head = p -> next;
3000
3000
5000
5000 2000
3000
3000
2000
head
5000
p
delete p;
head -> prev = NULL;
3000
2000
3000
3000
2000
head
102. NODE DELETION FROM BEGINNING
dnode * delete_beg(dnode *head)
{
dnode *p;
if(head = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = head;
head = p -> next;
delete p;
head -> prev = NULL;
return (head);
}
103. Deletion from End of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
3000
5000
5000 2000
3000
3000
2000
head
After Deletion:
5000
3000
5000
5000
3000
head
104. Deletion from End of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
dnode *p;
p = head;
While(p -> next != NULL)
P = p -> next;
5000
3000
5000
5000 2000
3000
3000
2000
head 5000
p
105. Deletion from End of DLL
P -> prev -> next = NULL;
5000
3000
5000
5000
3000
3000
2000
head 5000
p
delete p;
5000
3000
5000
5000
3000
head
106. NODE DELETION FROM END
dnode * delete_end(dnode *head)
{
dnode *p;
if(head = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = head;
while(p -> next !=NULL)
p = p -> next;
p -> prev -> next = NULL;
delete p;
return (head);
}
107. Deletion from In-Between of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
3000
5000
5000 2000
3000
3000
2000
head
After Deletion:
5000
2000
5000
5000
2000
head
108. Deletion from End of DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
dnode *p;
p = head;
While(p -> data != x)
p = p -> next;
5000
3000
5000
5000 2000
3000
3000
2000
head 3000
p
109. Deletion from End of DLL
P -> prev -> next = p -> next;
P -> next -> prev = p -> prev;
5000
2000
5000
5000 2000
3000
3000
2000
head 3000
p
5000
2000
5000
5000 2000
3000
5000
2000
head 3000
p
111. NODE DELETION FROM In-Between
dnode * delete_end(dnode *head, int x)
{
dnode *p;
if(head = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = head;
while(p -> data != x)
p = p -> next;
P -> prev -> next = p -> next;
P -> next -> prev = p -> prev;
delete p;
return (head);
}
113. Sorting DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
3000
5000
5000 2000
3000
3000
2000
head
dnode *current, *index;
current = head;
index = current -> next;
5000
current
3000
index
116. Sorting of DLL
void sortList() {
node *current = NULL, *index = NULL;
int temp;
if(head == NULL) { //Check whether list is empty
return;
}
else {
//Current will point to head
for(current = head; current->next != NULL; current = current->next) {
//Index will point to node next to current
for(index = current->next; index != NULL; index = index->next) {
//If current's data is greater than index's data, swap
if(current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
}
}
}
}
117. Operations on a Doubly Circular linked
list (DCLL)
1) Create list.
2) Insert element at beginning in list.
3) Insert element at end in list.
4) Insert element in-Between list.
5) Delete element from the beginning of list.
6) Delete element from the end of list.
7) Delete element from in-Between list.
8) Traversing
118. Creation of Doubly linked list (DCLL)
struct dnode
{
int data;
struct dnode *prev, *next;
}
3000
2000
5000
5000 3000
2000
2000
3000
rear
119. Pseudo code for creation of DCLL
dnode * create()
{
dnode *rear,*p, *q;
int i, n, x;
cout<<“Enter no. of elements”;
cin>>n;
for(i=0; i<n; i++)
{
cout<< “Enter next data”;
p = new dnode;
cin>> p -> data;
p -> prev = p -> next = NULL;
if(rear = = NULL)
{ rear = p; p -> next = p; p -> prev = p; }
else
{
p -> prev = rear;
p -> next = rear -> next;
rear -> next -> prev = p;
rear –> next = p
}
}
return (rear);
}
120. Insertion in Doubly Circular linked list
(DCLL)
1) Insert element at beginning in list.
2) Insert element at end in list.
3) Insert element in-Between list.
121. Insertion at beginning/end of DCLL
2000
dnode *p;
p = new dnode;
p -> data = 20;
p -> prev = NULL;
p -> next = NULL
p
3000
3000
5000
5000
3000
rear
Initial List
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
122. Insertion at beginning/end of DCLL
2000
p
3000
3000 3000
5000
5000 5000
3000
rear
p -> prev = rear;
p -> next = rear -> next
3000 5000
2000
p
3000
3000 3000
5000
5000 5000
3000
rear
123. Insertion at beginning/end of DCLL
rear -> next -> prev = p
3000 5000
2000
p
3000
2000 3000
5000
5000 5000
3000
rear
rear -> next = p
3000 5000
2000
p
3000
2000 3000
5000
5000 2000
3000
rear
rear = p
124. Insertion at beginning/end of DCLL
After Re-arrangement 2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
125. NODE INSERTION AT THE BEGINNING OR END
Node * insert_beg_end(Node *Rear, int x)
{
Node *p;
p = new Node;
p -> data = x;
p -> next = NULL;
p -> prev = NULL;
if(Rear = = NULL)
{
Rear = p;
p -> next = p;
p -> prev = p;
return (Rear);
}
else
{
p -> prev = rear;
p -> next = rear -> next;
rear -> next -> prev = p;
rear -> next = p
Rear = p;
return (Rear)
}
}
126. Insertion Between of DCLL
2000
dnode *p;
p = new dnode;
p -> data = 8;
p -> prev = NULL;
p -> next = NULL
p
3000
3000
5000
5000
3000
rear
Initial List
2000
3000 2000
5000
5000 3000
2000
2000 5000
3000
rear
127. Insertion In-Between of DCLL
2000
p
3000
3000 3000
5000
5000 5000
3000
rear
p -> prev = q;
p -> next = q -> next
q = rear;
while(q->data != y)
q = q->next;
q
5000 3000
2000
3000
3000 3000
5000
5000 5000
3000
rear
q
p
129. Insertion Between of DCLL
Rear = p;
2000
3000 2000
5000
5000 3000
2000
2000 5000
3000
rear
130. NODE INSERTION IN-BETWEEN OF DCLL
Node * insert_between(Node *Rear, int x, int y)
{
Node *p;
p = new Node;
p -> data = x;
p -> next = p -> prev = NULL;
if(Rear = = NULL) {
Rear = p;
p -> next = p;
p -> prev = p;
return (Rear);
}
else {
q = rear;
while(q -> data != y)
q = q -> next;
p -> prev = q;
p -> next = q -> next;
q -> next-> prev = p;
q -> next = p;
Rear = p;
return (Rear);
}
}
131. Deletion from Doubly Circular linked list
(DCLL)
1) Delete element at beginning of list.
2) Delete element at end of list.
3) Delete element in-Between of list.
132. Deletion from beginning/end of DCLL
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
Initial List
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
3000
2000 3000
5000
5000 2000
3000
rear
After Deletion
133. Deletion from beginning/end of DCLL
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
Initial List
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
p
dnode *p;
p = rear;
rear = rear -> prev
3000
rear
134. Deletion from beginning/end of DCLL
2000
3000 3000
5000
5000 2000
3000
3000 5000
2000
p
p -> next -> prev = rear
3000
rear
2000
3000 3000
5000
5000 5000
3000
3000 5000
2000
p
rear -> next = p -> next
3000
rear
135. Deletion from beginning/end of DCLL
delete p;
2000
3000 3000
5000
5000 5000
3000
3000 5000
2000
p
3000
rear
3000
2000 3000
5000
5000 2000
3000
rear
After Deletion
136. NODE DELETION FROM BEGINNING OR END
Node * delete_beg_end(Node *rear)
{
Node *p;
if(rear = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = rear;
rear = rear -> prev;
p -> next -> prev = rear;
rear -> next = p -> next;
delete(p);
return(rear);
}
137. Deletion from In-Between of DCLL
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
Initial List
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
2000
2000 2000
5000
5000 5000
2000
rear
After Deletion
138. Deletion from beginning/end of DCLL
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
Initial List
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
dnode *p;
p = rear;
while(p -> data != y)
p = p -> next;
3000
p
139. Deletion from beginning/end of DCLL
2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
P -> prev -> next = p -> next
P -> next -> prev = p -> prev
3000
p
delete p;
2000
2000 2000
5000
5000 5000
2000
rear
After Deletion
140. NODE DELETION IN-BETWEEN
dnode * delete_end(dnode *read, int y)
{
dnode *p;
if(read = = NULL)
{
cout<<“No element available to delete”;
return;
}
p = rear;
while(p -> data != y)
p = p -> next;
P -> prev -> next = p -> next;
P -> next -> prev = p -> prev;
delete p;
return (rear);
}
142. Representation of Polynomial
Each node for each term of polynomial consist of 3
fileds named coefficient, exponent and address of next
node
To represent 5x3 + 2x – 4 polynomial
To represent 98x78 + 2x5 – 4x2 + 3 polynomial
coeff expo next
5 3 2 1 -4 0
head
98 78 2 5 -4 2
head
3 0
152. Generalized Linked List
• Definition: A generalized linked list A, is defined as a finite
sequence n>=0 elements a1 , a2 , …....,an such that ai elements
are either atoms or list of atoms.
• Thus A = ( a1 , a2 , a3….........., an )
where n is total no .of nodes in the list
• Representation of node:
• Flag 0 means data (atom) exist
• Flag 1 means down pointer exist
• Down pointer exists when list of atoms exist.
• Next pointer points to next node (atom)
Flag Data/ Down Ponter Next pointer
155. Generalized Linked List
Example3: G = ((a, b, c), d, (e, f), g)
1
0 a 0 b 0 c
0 d 1
0 e 0 f
0 g
Example4 for Practice: (L,(M, (N, (O, P)), Q), R, (S, T), (A, (B, C)))
156. Representation of Polynomial using GLL
Example1: 4x6 + 8x2 + 3x + 7 single variable polynomial
x - 6 4 2 8 1 3
Polynomial of x
0 7
Example2: 3y9 - 6y7 + 2y3 - 4 single variable polynomial
y - 9 3 7 -6 3 2
Polynomial of y
0 -4
157. Representation of Polynomial using GLL
Example3: y3(5x2+ 9x) + y(5x + 6) + (9x3 + 7) Two variable polynomial
y - 3
Polynomial of y
(5x2+ 9x)
1
(5x + 6)
0
(9x3 + 7)
158. Representation of Polynomial using GLL
y - 3
Polynomial of y
(5x2+ 9x)
1
(5x + 6)
0
(9x3 + 7)
y - 3
x - 2 5 1 9
1
x - 1 5 0 6
0
x - 3 9 0 7
159. Representation of Polynomial using GLL
Example4: x10y3z2 + 2x8y3z2 + 3x8y2z2 +x4y4z + 6x3y4z + 2yz
= z2(x10y3+ 2x8y3 + 3x8y2 ) + z(x4y4 + 6x3y4 + 2y)
Re-writing the above polynomial we get
= z2[y3(x10+ 2x8 )+ 3x8y2)] + z[y4(x4 + 6x3) + 2y]
z - 2
y3(x10+ 2x8 )+ 3x8y2)
1
y4(x4 + 6x3) + 2y
160. Representation of Polynomial using GLL
z - 2
y3(x10+ 2x8 )+ 3x8y2)
1
y4(x4 + 6x3) + 2y
z - 2
y - 3
x10+ 2x8
2
3x8
1
y - 4
x4+ 6x3
1
2
165. Representation of Polynomial using GLL
Example5: 3x4y3 + 5x3y3 + 7xy3 +3x4y6 + 5x3y6+ 7xy6 + 6xy
= y6( 3x4 + 5x3 +7x) + y3( 3x4 + 5x3 + 7x ) + 6xy
Re-writing the above polynomial we get
y - 6
3x4 + 5x3 +7x
3
3x4 + 5x3 + 7x
1
6x
166. Representation of Polynomial using GLL
y - 6
3x4 + 5x3 +7x
3
3x4 + 5x3 + 7x
1
6x
y - 6
x - 4 3 3 5 1 7
3
x - 4 3 3 5 1 7
1
x - 1 6
167. Case study: Garbage collection
• If some object is created and which is not been in use since long
time, then such an object is called garbage
• The garbage collection is a technique in which all such garbage is
collected and removed.
• The garbage collector cleans up the heap memory so that the
memory occupied by unused objected can be freed and can
be allocated to new objects
• Garbage collection algo works in 2 steps:
1. Mark: All the unused objects are located and marked them for
deletion.
2. Sweep: All marked objects are swept, and memory get freed
• Advantages:
1. Manual memory management by programmer is time consuming
and error prone. So this automatic memory management is useful
2. Reusability