Understanding Linked Lists and Memory Management
Understanding Linked Lists and Memory Management
LINKED LISTS
1
Objectives
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.
malloc: Allocating a block of memory
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 6
perform the task of allocating
Example int *p = new int[10]
calloc: Allocating multiple block of memory
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);
7
Example ptr = (int*) calloc(5, 5*sizeof(int))
ptr = (float*) calloc(25, sizeof(float));
realloc( ); Altering the size of a block
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);
Example
8
realloc( ); Altering the size of a block
9
free( ); Releasing the used space
“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++
1
delete ptr; // available in C++ only 0
free( ); Releasing the used space
1
1
WHAT IS A LINKEDLIST?
10 5010 1
8 2010 20 NULL 2
1005 5010 2010
LIST AS AN ADT
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
1
3
LINKED LIST TERMINOLOGIES
Traversal of List
Means to visit every element or node in the list
beginning from first to last.
Predecessor and Successor
1
5
Singly Linked List
16
Singly LINKED LISTS
A B C
Head
[Link] 17
LISTS – ANOTHER PERSPECTIVE
A list is a linear collection of varying length of
homogeneous components.
18
An Array A Linked List
19
AN INTEGER LINKED LIST
Head
10 13 5 2
20
THE NULL POINTER
int *p;
p = NULL;
21
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
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
Basic Concepts
p = new Node; p 10
p - > data = 10;
p - > next = NULL;
q = new Node;
q - > data = 6; q 6
q - > next = NULL;
p - > next = q;
p 10 6
20
q
Basic Concepts
p 10 6
Expression Value
p Pointer to first node (head)
p - > data 10
p - > next Pointer to next node
p - > next - > data 6
p - > next - > next NULL pointer
21
BASIC LINKED LIST OPERATIONS
31
NODE INSERTION
Insertion at the beginning of the list
Insertion at the end of the list
36
NODE INSERTION AT THE BEGINNING
Steps:
Create a node
Node *ptr;
ptr = new Node; ptr
ptr - > data = 93;
ptr - > next = head; ptr
head = ptr; 93
head
Rearrange:
head 93
NODE INSERTION AT THE BEGINNING
Node * ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
NODE INSERTION AT THE END
ptr
Node * p;
p = new Node;
p -> data = 150; 150 //
p -> next = NULL;
p
NODE INSERTION AT THE END
ptr
head 48 17 142
150 //
p
NODE INSERTION AT THE END
Step 3
Step 4
NODE INSERTION IN-BETWEEN
ptr
How to get pointer on desired node?
NODE INSERTION AT ARBITRARY POSITION
Suppose we want to insert a node after the node having
value ‘x’:
Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}
ptr
NODE INSERTION AT ARBITRARY POSITION
Node * ptr; Node * p;
ptr = head; p = new Node;
while (ptr->data != x) p -> data = 100;
{ p -> next = NULL;
ptr = ptr->next;
}
ptr
150 //
p
NODE INSERTION AT IN-BETWEEN / ARBITRARY
Node * ptr; Node * p;
ptr = head; p = new Node;
while (ptr->data != x) p -> data = 150;
{ p -> next = NULL;
ptr = ptr->next; p -> next = ptr -> next;
} ptr -> next = p;
ptr
150
p
NODE INSERTION IN-BETWEEN
head 6 4 17 42
head
6 4 17 42
head 4 17 42
DELETING FROM THE BEGINNING
head
ptr 6 4 17 42
head
head 6 4 17 42
ptr
head 4 17 42
Node * ptr;
ptr = head;
head = head ->next;
delete ptr;
NODE DELETION FROM BEGINNING
head 6 4 17 42
head 6 4 17
DELETING FROM THE END
We need a pointer one node before the node to be deleted
head 6 4 17 42
p q
Node * p;
Node * q;
p = head;
while (p -> next -> next != NULL)
{
p = p -> next;
}
q = p -> next;
delete (q);
P -> next = NULL
NODE DELETION FROM END
head 6 4 17 42
head 6 4 17 42
head 6 4 42
DELETING FROM IN-BETWEEN POSITION
We need a pointer on the node to be deleted (as well a pointer to one
node before the node to be deleted)
head 6 4 17 42
p q
Node * p;
Node * q;
p = head; Given the value of the node to
while(p -> next -> data != x) be deleted, assume this to be
{ variable ‘x’
p = p -> next; Keep moving a pointer until the
required node is reached
}
q = p -> next;
p -> next = q -> next;
delete (q)
NODE DELETION FROM END
32
fir st 9 17 22 26 34
ptr = first;
while (ptr != null_value)
{ ptr
Process data part of
node pointed to by ptr; 9 17 22 26 34
firs t
ptr = next part of node
pointed to by ptr;
} pt r
.
.
firs t 9 17 22 26 34
pt r
firs t 9 17 22 26 34
ptr
34
TRAVERSING THE LIST
Node * currNode;
currNode = head;
while (currNode != NULL)
{
cout<< currNode->data;
currNode = currNode->next;
}
35
Circular linked list
Rear
C/C++ representation
struct node
{
int data;
struct node *next;
}
What Is creation of CLL
Allocate a new node Algorithm
Insert new element p = new node
Make new node point to p -> data = x
null p -> next = NULL
Create head to point to Rear = p
new node
Rear -> next = Rear
p Rear
93
NODE INSERTION IN CLL
Insertion at the beginning of the list
Insertion at the end of the list
36
NODE INSERTION AT THE BEGINNING OR END
Steps:
Create a node
Step 1
Step 2
Rear
List after Step 3
93
NODE INSERTION AT THE BEGINNING OR END
Rear
Initial list
Node *p;
p = new Node; 48 17 142
p -> data = 93;
p -> next = NULL;
p ->next = Rear->next;
Rear -> next = p
p p 93
p
93
Rear
NODE INSERTION AT THE BEGINNING OR END
Rear p
Rearrange
93
Rear = p
Rear
Final List
93
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)
}
}
NODE INSERTION IN BETWEEN
Steps:
Create a node
Step 1
Step 2
Rear
List after Step 3
48 93 17 142
NODE INSERTION IN BETWEEN
Rear
Initial list
Node *p;
p = new Node; 48 17 142
p -> data = 93;
p -> next = NULL;
q = Rear;
While(q->data != y)
p p 93
q = q -> next;
p ->next = q ->next;
q -> next = p
p
93
q Rear
NODE INSERTION IN BETWEEN
Re-arranging
p Rear
48 93 17 142
Rear = p
p Rear
48 93 17 142
NODE INSERTION IN-BETWEEN
93
Rear
93
Node *p;
p = Rear;
while(p -> next != Rear)
p = p -> next; Rear
p
93
NODE DELETION FROM BEGINNING OR END
p Rear
93
Rear = p;
p = p -> next; Rear p
93
48 17 142
NODE DELETION FROM BEGINNING OR END
93
Rear
93
p = Rear;
while(p -> next –> data != x)
p = p -> next;
p Rear
93
NODE DELETION FROM IN BETWEEN
q = p -> next
p q Rear
93
head
5000
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
Creation of Doubly linked list (DLL)
struct dnode
{
int data;
struct dnode *prev, *next;
}
head
5000
2000 5000
5000 2000
dnode *p; p
p = new dnode;
p -> data = 20;
p -> prev = NULL;
3000
p -> next = NULL
head
3000
5000
head
5000 3000
2000 5000
5000 2000
5000
head
5000 3000
5000 2000
Insertion at beginning of DLL
head = p;
p
5000
head
3000 3000
5000 2000
Re-arranging
head
3000
2000 5000
5000 2000
dnode *p; p
p = new dnode;
p -> data = 20;
p -> prev = NULL;
3000
p -> next = NULL
head
5000
5000 2000 p
p -> prev = q
2000
3000
head q
5000
5000 2000
Insertion at end of DLL
Re-arranging
head
5000
2000 5000
5000 2000
dnode *p; p
p = new dnode;
p -> data = 20;
p -> prev = NULL;
3000
p -> next = NULL
head
5000
2000 5000
5000 2000
p
p -> prev = q;
5000 2000
3000
head q
5000
2000 5000
5000 2000
Insertion In-betwenn of DLL p
q -> next -> prev = p;
5000 2000
3000
head q
5000
2000 3000
5000 2000
p
q -> next = p
5000 2000
3000
head q
5000
3000 5000
5000 2000
Insertion In-Between of DLL
Re-arranging
head
5000
head
5000
After Deletion:
head
3000
3000 2000
Deletion from Beginning of DLL
Initial List
head
5000
p
head 5000
5000
p head
5000 3000
head
3000
2000 3000
3000 2000
NODE DELETION FROM BEGINNING
head
5000
After Deletion:
head
5000
3000 5000
5000 3000
Deletion from End of DLL
Initial List
head
5000
dnode *p;
p = head;
While(p -> next != NULL)
P = p -> next;
p
head 5000
5000
delete p;
head
5000
3000 5000
5000 3000
NODE DELETION FROM END
head
5000
After Deletion:
head
5000
2000 5000
5000 2000
Deletion from End of DLL
Initial List
head
5000
dnode *p;
p = head;
While(p -> data != x)
p = p -> next;
p
head 3000
5000
p
head 3000
5000
p
head 3000
5000
head
5000
2000 5000
5000 2000
NODE DELETION FROM In-Between
head
5000
After Sorting:
head
5000
head
5000
current index
5000 3000
head
5000
current index
5000 3000
head
5000
current index
3000
head 5000
5000
struct dnode
{
int data;
struct dnode *prev, *next;
}
rear
3000
Initial List
3000 5000
5000 3000
dnode *p; p
p = new dnode;
p -> data = 20;
p -> prev = NULL;
2000
p -> next = NULL
rear
2000
5000 5000
2000
3000 3000
5000 3000
rear
3000 p
3000 5000
3000 3000 5000 5000
2000
5000 3000
Insertion at beginning/end of DCLL
rear -> next -> prev = p
rear
3000 p
3000 5000
2000 3000 5000 5000
2000
5000 3000
3000 5000
2000 3000 5000 2000
2000
5000 3000
rear = p
Insertion at beginning/end of DCLL
rear
After Re-arrangement 2000
Initial List
3000 5000
5000 3000
dnode *p; p
p = new dnode;
p -> data = 8;
p -> prev = NULL;
2000
p -> next = NULL
rear
2000
5000 3000
p -> prev = q; p
p -> next = q -> next
5000 3000 rear
q 3000
2000
5000 3000
Insertion In-Between of DCLL
q -> next-> prev = p; p
5000 3000
q -> next = p; p
5000 3000
Insertion Between of DCLL
Rear = p;
rear
2000
rear
2000
rear 3000
dnode *p;
p = rear;
rear = rear -> prev
rear p
3000 2000
rear p
3000 2000
rear p
3000 2000
After Deletion
rear
3000
5000 3000
NODE DELETION FROM BEGINNING OR END
rear
2000
rear 2000
dnode *p;
p = rear;
while(p -> data != y)
p rear
p = p -> next; 2000
3000
delete p;
rear 2000
1) Representation of Polynomial
2) Polynomial Addition
3) Polynomial Multiplication
Representation of Polynomial
5 3 2 1 -4 0
98 78 2 5 -4 2 3 0
Representation of Polynomial
C / C++ structure:
5 3 2 1 -4 0
head2
98 4 2 3 -4 2 3 0
Pnode *p1, p2
head1 = p1;
head2 = p2;
Addition of Polynomial
5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3
head1 p1
5 3 2 1 -4 0
head2 p2
98 4 2 3 -4 2 3 0
5 3 2 1 -4 0
head2 p2
98 4 2 3 -4 2 3 0
5 3 2 1 -4 0
head2 p2
98 4 2 3 -4 2 3 0
98 4 7 3 -4 2 2 1
Addition of Polynomial
head3 p3
98 4 7 3 -4 2 2 1 -1 0
Pseudocode: Addition of Polynomial
pnode *addpoly(pnode *p1, pnode *p2)
{
pnode *p3, *head3;
p3 = NULL;
while(p1 != NULL && p2 != NULL)
{
if(p3 = = NULL)
{
head3= p3= new pnode;
p3 -> next = NULL;
}
else
{
p3 -> next = new pnode;
p3 = p3 -> next;
p3 -> next = NULL;
}
Pseudocode: Addition of Polynomial
if(p1 -> expo > p2 -> expo)
{
p3 -> coeff = p1 -> coeff;
p3 -> expo = p1 -> expo;
p1 = p1 -> next;
}
elseif( p2 -> expo > p1 -> expo)
{
p3 -> coeff = p2 -> coeff;
p3 -> expo = p2 -> expo;
p2 = p2 -> next;
}
else
{
p3 -> coeff = p1 -> coeff + p2 -> coeff;
p3 -> expo = p1 -> expo;
p1 = p1 -> next;
p2 = p2 -> next;
}
}
Pseudocode: Addition of Polynomial
while(p1 != NULL)
{
p3 -> next = new pnode;
p3 = p3 -> next;
P3 -> next = NULL;
p3 -> coeff = p1 -> coeff;
p3 -> expo = p1 -> expo;
p1 = p1 -> next;
}
while(p2 != NULL)
{
p3 -> next = new pnode;
p3 = p3 -> next;
P3 -> next = NULL;
p3 -> coeff = p2 -> coeff;
p3 -> expo = p2 -> expo;
p2 = p2 -> next;
}
return (head3);
}
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:
0 a 1 0 d
0 b 0 c
Generalized Linked List
Example2: G = (p, q, (r, s, (t, u, v), w), x, y)
0 p 0 q 1 0 x 0 y
0 r 0 s 1 0 w
0 t 0 u 0 v
Generalized Linked List
Example3: G = ((a, b, c), d, (e, f), g)
1 0 d 1 0 g
0 e 0 f
0 a 0 b 0 c
Example4 for Practice: (L,(M, (N, (O, P)), Q), R, (S, T), (A, (B, C)))
Representation of Polynomial using GLL
Example1: 4x6 + 8x2 + 3x + 7 single variable polynomial
x - 6 4 2 8 1 3 0 7
Polynomial of x
y - 9 3 7 -6 3 2 0 -4
Polynomial of y
Representation of Polynomial using GLL
Example3: y3(5x2+ 9x) + y(5x + 6) + (9x3 + 7) Two variable polynomial
y - 3 1 0
y - 3 1 0
x - 3 9 0 7
x - 1 5 0 6
x - 2 5 1 9
Representation of Polynomial using GLL
Example4: x10y3z2 + 2x8y3z2 + 3x8y2z2 +x4y4z + 6x3y4z + 2yz
Re-writing the above polynomial we get
= z2(x10y3+ 2x8y3 + 3x8y2 ) + z(x4y4 + 6x3y4 + 2y)
z - 2 1
z - 2 1
y - 4 1
x4+ 6x3 2
y - 3 2
z - 2 1
y - 4 1
x - 0 2
x - 4 1 3 6
y - 3 2
x - 8 3
x - 10 1 8 2
Representation of Polynomial using GLL
Example4: P(a,b,c)= a10b3c2 + 6a8b3c2 + 5a8b2c2 +2a4b4c + 2a3b4c + 8bc
c - 2 1
c - 2 1
b - 4 1
a4+2a3 8
b - 3 2
c - 2 1
b - 4 1
a - 0 8
a - 4 1 3 2
b - 3 2
a - 8 3
a - 10 1 8 6
Representation of Polynomial using GLL
Example5: 3x4y3 + 5x3y3 + 7xy3 +3x4y6 + 5x3y6+ 7xy6 + 6xy
y - 6 3 1
y - 6 3 1
x - 1 6
x - 4 3 3 5 1 7
x - 4 3 3 5 1 7
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
• Advantages:
1. Manual memory management by programmer is time consuming
and error prone. So this automatic memory management is useful
2. Reusability