SlideShare uma empresa Scribd logo
1 de 167
LINKED LISTS
1
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
Memory Allocation
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
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.
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]
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));
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
realloc( ); Altering the size of a block
9
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
free( ); Releasing the used space
1
1
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
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
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
VARIATIONS OF LINKED LISTS
1
5
 Singly linked lists
 Circular linked lists
 Doubly linked lists
 Doubly Circular linked list
Singly Linked List
16
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
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
A Linked List
19
AnArray
AN INTEGER LINKED LIST
Head
10 13 5 2
First Node of List
data next NULL
Last Node of List
20
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;
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
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
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
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
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
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
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
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)
}
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
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
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
NODE INSERTION AT THE END
48 17 142
head
ptr
ptr -> next = p;
150 //
p
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)
}
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
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?
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;
}
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
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
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)
}
NODE DELETION
 Deleting from the beginning of the list
 Deleting from the end of the list
 Deleting from in-between of the list
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
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
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)
}
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
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
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);
}
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
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
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);
}
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
Traversing the list
32
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
TRAVERSING THE LIST
35
Node * currNode;
currNode = head;
while (currNode != NULL)
{
cout<< currNode->data;
currNode = currNode->next;
}
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 .
Structure of circular linked list
1000 2000
B 2000 C 4000
4000
A 1000
Rear
Operations on linked list
The basic operations on
linked lists are :
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
 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
 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
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
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
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
NODE INSERTION AT THE BEGINNING OR END
Rearrange
Rear
93
Final List
Rear
93
Rear = p
p
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
 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
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
NODE INSERTION IN BETWEEN
p
93
48 17 142
Rear
Re-arranging
Rear = p
p
93
48 17 142
Rear
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);
}
NODE DELETION FROM BEGINNING OR END
48 17 142
Rear
Initial list
List after
deletion
Rear
93
NODE DELETION FROM BEGINNING OR END
Initial list
Rear
93
Node *p;
p = Rear;
while(p -> next != Rear)
p = p -> next; Rear
93
p
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;
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)
}
NODE DELETION FROM IN BETWEEN
48
Rear
Initial list
List after
deletion In
Between node
Rear
93
142 93
NODE DELETION FROM IN BETWEEN
Initial list
Rear
93
p = Rear;
while(p -> next –> data != x)
p = p -> next;
Rear
93
p
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
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);
}
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
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
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
Creation of Doubly linked list (DLL)
struct dnode
{
int data;
struct dnode *prev, *next;
}
5000
2000
5000
5000 3000
2000
2000
3000
head
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);
}
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.
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
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
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
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);
}
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
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
Insertion at end of DLL
Re-arranging
5000
2000
5000
5000 3000
2000
2000
3000
head
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);
}
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
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
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
Insertion In-Between of DLL
Re-arranging
5000
3000
5000
5000 2000
3000
3000
2000
head
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);
}
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.
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:
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
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
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);
}
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
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
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
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);
}
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
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
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
Deletion from In-Between of DLL
delete p;
5000
2000
5000
5000
2000
head
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);
}
Sorting DLL
Initial List
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
3000
5000
5000 2000
3000
3000
2000
head
After Sorting:
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
Sorting DLL
5000
3000
5000
5000 2000
3000
3000
2000
head
if(current->data > index->data)
swap
5000
current
3000
index
5000
3000
5000
5000 2000
3000
3000
2000
head
5000
current
3000
index
index = index -> next;
Sorting DLL
5000
3000
5000
5000 2000
3000
3000
2000
head
if(current->data > index->data)
swap
5000
current
3000
index
5000
3000
5000
5000 2000
3000
3000
2000
head 5000
current
3000
index
current = current -> next;
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;
}
}
}
}
}
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
Creation of Doubly linked list (DCLL)
struct dnode
{
int data;
struct dnode *prev, *next;
}
3000
2000
5000
5000 3000
2000
2000
3000
rear
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);
}
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.
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
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
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
Insertion at beginning/end of DCLL
After Re-arrangement 2000
2000 3000
5000
5000 2000
3000
3000 5000
2000
rear
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)
}
}
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
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
Insertion In-Between of DCLL
q -> next-> prev = p;
5000 3000
2000
3000
3000 3000
5000
2000 5000
3000
rear
q
p
q -> next = p;
5000 3000
2000
3000
3000 2000
5000
2000 5000
3000
rear
q
p
Insertion Between of DCLL
Rear = p;
2000
3000 2000
5000
5000 3000
2000
2000 5000
3000
rear
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);
}
}
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.
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
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
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
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
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);
}
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
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
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
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);
}
Polynomial Manipulation
1) Representation of Polynomial
2) Polynomial Addition
3) Polynomial Multiplication
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
Representation of Polynomial
C / C++ structure:
tydef struct pnode
{
int coeff;
int expo;
struct pnode *next;
}
Addition of Polynomial
5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3
5 3 2 1 -4 0
head1
98 4 2 3 -4 2
head2
3 0
Pnode *p1, p2
head1 = p1;
head2 = p2;
Addition of Polynomial
5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3
5 3 2 1 -4 0
head1
98 4 2 3 -4 2
head2
3 0
head3= p3 = new pnode;
p3 -> next = NULL;
if(p2 -> expo > p1 -> expo)
{
P3 -> coeff = p2 -> coeff;
P3 -> expo = p2 -> expo;
p2 = p2 -> next;
}
p1
p2
98 4
head3 p3
Addition of Polynomial
5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3
5 3 2 1 -4 0
head1
98 4 2 3 -4 2
head2
3 0
P3 -> next = new pnode;
p3 = p3 -> next;
p3 -> next = NULL;
if(p1 -> expo = = p2 -> expo)
{
P3 -> coeff = p1 -> coeff + p2->coeff;
P3 -> expo = p1 -> expo;
p1 = p1 -> next;
p2 = p2 -> next;
}
p1
p2
98 4
head3
7 3
p3
Addition of Polynomial
5 3 2 1 -4 0
head1
98 4 2 3 -4 2
head2
3 0
P3 -> next = new pnode;
p3 = p3 -> next;
p3 -> next = NULL;
if(p1 -> expo > p2 -> expo)
{
P3 -> coeff = p1 -> coeff;
P3 -> expo = p1 -> expo;
p1 = p1 -> next;
}
p1
p2
98 4
head3
7 3
p3
-4 2 2 1
Addition of Polynomial
98 4
head3
7 3 -4 2 2 1
p3
-1 0
Final Linked list after addition
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:
• 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
Generalized Linked List
• Example1: (a, (b, c), d)
0 a 1
0 b 0 c
0 d
Generalized Linked List
Example2: G = (p, q, (r, s, (t, u, v), w), x, y)
0 p 0 q 1
0 r 0 s 1
0 t 0 u 0 v
0 w
0 x 0 y
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)))
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
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)
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
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
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
Representation of Polynomial using GLL
z - 2
y - 3 2
x - 10 1 8 2
x - 8 3
1
y - 4 1
x - 4 1 3 6
x - 0 2
Representation of Polynomial using GLL
Example4: P(a,b,c)= a10b3c2 + 6a8b3c2 + 5a8b2c2 +2a4b4c + 2a3b4c + 8bc
= c2(a10b3+ 6a8b3 + 5a8b2 ) + c(a4b4 + 2a3b4 + 8b)
Re-writing the above polynomial we get
= c2[b3(a10+ 6a8 )+ 3a8b2] + c[b4(a4 + 2a3) + 8b]
c - 2
b3(a10+ 6a8 )+ 3a8b2
1
b4(a4 + 2a3) + 8b
Representation of Polynomial using GLL
c - 2
b3(a10+ 6a8 )+ 3a8b2
1
b4(a4 + 2a3) + 8b
c - 2
b - 3
a10+ 6a8
2
3a8
1
b - 4
a4+2a3
1
8
Representation of Polynomial using GLL
c - 2
b - 3 2
a - 10 1 8 6
a - 8 3
1
b - 4 1
a - 4 1 3 2
a - 0 8
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
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
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

Mais conteúdo relacionado

Semelhante a Linked List.pptx

Semelhante a Linked List.pptx (20)

Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linked list
Linked listLinked list
Linked list
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
UNIT 3a.pptx
UNIT 3a.pptxUNIT 3a.pptx
UNIT 3a.pptx
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Savitch Ch 13
Savitch Ch 13Savitch Ch 13
Savitch Ch 13
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Team 10
Team 10Team 10
Team 10
 
Linked list
Linked listLinked list
Linked list
 

Último

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Último (20)

The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

Linked List.pptx

  • 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
  • 9. realloc( ); Altering the size of a block 9
  • 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
  • 11. free( ); Releasing the used space 1 1
  • 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
  • 15. VARIATIONS OF LINKED LISTS 1 5  Singly linked lists  Circular linked lists  Doubly linked lists  Doubly Circular linked list
  • 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 .
  • 57. Structure of circular linked list 1000 2000 B 2000 C 4000 4000 A 1000 Rear
  • 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
  • 96. Insertion In-Between of DLL Re-arranging 5000 3000 5000 5000 2000 3000 3000 2000 head
  • 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
  • 110. Deletion from In-Between of DLL delete p; 5000 2000 5000 5000 2000 head
  • 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); }
  • 112. Sorting DLL Initial List 5000 3000 5000 5000 2000 3000 3000 2000 head 5000 3000 5000 5000 2000 3000 3000 2000 head After Sorting:
  • 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
  • 114. Sorting DLL 5000 3000 5000 5000 2000 3000 3000 2000 head if(current->data > index->data) swap 5000 current 3000 index 5000 3000 5000 5000 2000 3000 3000 2000 head 5000 current 3000 index index = index -> next;
  • 115. Sorting DLL 5000 3000 5000 5000 2000 3000 3000 2000 head if(current->data > index->data) swap 5000 current 3000 index 5000 3000 5000 5000 2000 3000 3000 2000 head 5000 current 3000 index current = current -> next;
  • 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
  • 128. Insertion In-Between of DCLL q -> next-> prev = p; 5000 3000 2000 3000 3000 3000 5000 2000 5000 3000 rear q p q -> next = p; 5000 3000 2000 3000 3000 2000 5000 2000 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); }
  • 141. Polynomial Manipulation 1) Representation of Polynomial 2) Polynomial Addition 3) Polynomial Multiplication
  • 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
  • 143. Representation of Polynomial C / C++ structure: tydef struct pnode { int coeff; int expo; struct pnode *next; }
  • 144. Addition of Polynomial 5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3 5 3 2 1 -4 0 head1 98 4 2 3 -4 2 head2 3 0 Pnode *p1, p2 head1 = p1; head2 = p2;
  • 145. Addition of Polynomial 5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3 5 3 2 1 -4 0 head1 98 4 2 3 -4 2 head2 3 0 head3= p3 = new pnode; p3 -> next = NULL; if(p2 -> expo > p1 -> expo) { P3 -> coeff = p2 -> coeff; P3 -> expo = p2 -> expo; p2 = p2 -> next; } p1 p2 98 4 head3 p3
  • 146. Addition of Polynomial 5x3 + 2x – 4 + 98x4 + 2x3 – 4x2 + 3 5 3 2 1 -4 0 head1 98 4 2 3 -4 2 head2 3 0 P3 -> next = new pnode; p3 = p3 -> next; p3 -> next = NULL; if(p1 -> expo = = p2 -> expo) { P3 -> coeff = p1 -> coeff + p2->coeff; P3 -> expo = p1 -> expo; p1 = p1 -> next; p2 = p2 -> next; } p1 p2 98 4 head3 7 3 p3
  • 147. Addition of Polynomial 5 3 2 1 -4 0 head1 98 4 2 3 -4 2 head2 3 0 P3 -> next = new pnode; p3 = p3 -> next; p3 -> next = NULL; if(p1 -> expo > p2 -> expo) { P3 -> coeff = p1 -> coeff; P3 -> expo = p1 -> expo; p1 = p1 -> next; } p1 p2 98 4 head3 7 3 p3 -4 2 2 1
  • 148. Addition of Polynomial 98 4 head3 7 3 -4 2 2 1 p3 -1 0 Final Linked list after addition
  • 149. 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; }
  • 150. 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; } }
  • 151. 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); }
  • 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
  • 153. Generalized Linked List • Example1: (a, (b, c), d) 0 a 1 0 b 0 c 0 d
  • 154. Generalized Linked List Example2: G = (p, q, (r, s, (t, u, v), w), x, y) 0 p 0 q 1 0 r 0 s 1 0 t 0 u 0 v 0 w 0 x 0 y
  • 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
  • 161. Representation of Polynomial using GLL z - 2 y - 3 2 x - 10 1 8 2 x - 8 3 1 y - 4 1 x - 4 1 3 6 x - 0 2
  • 162. Representation of Polynomial using GLL Example4: P(a,b,c)= a10b3c2 + 6a8b3c2 + 5a8b2c2 +2a4b4c + 2a3b4c + 8bc = c2(a10b3+ 6a8b3 + 5a8b2 ) + c(a4b4 + 2a3b4 + 8b) Re-writing the above polynomial we get = c2[b3(a10+ 6a8 )+ 3a8b2] + c[b4(a4 + 2a3) + 8b] c - 2 b3(a10+ 6a8 )+ 3a8b2 1 b4(a4 + 2a3) + 8b
  • 163. Representation of Polynomial using GLL c - 2 b3(a10+ 6a8 )+ 3a8b2 1 b4(a4 + 2a3) + 8b c - 2 b - 3 a10+ 6a8 2 3a8 1 b - 4 a4+2a3 1 8
  • 164. Representation of Polynomial using GLL c - 2 b - 3 2 a - 10 1 8 6 a - 8 3 1 b - 4 1 a - 4 1 3 2 a - 0 8
  • 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