0% found this document useful (0 votes)
8 views122 pages

Unit 3

The document discusses the limitations of arrays and introduces linked lists as a dynamic alternative for data storage. It explains the structure of linked lists, including singly and doubly linked lists, and details various operations such as insertion, deletion, searching, and reversing. Additionally, it highlights the differences between arrays and linked lists in terms of memory allocation and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views122 pages

Unit 3

The document discusses the limitations of arrays and introduces linked lists as a dynamic alternative for data storage. It explains the structure of linked lists, including singly and doubly linked lists, and details various operations such as insertion, deletion, searching, and reversing. Additionally, it highlights the differences between arrays and linked lists in terms of memory allocation and efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 122

Linked Lists

LIMITATIONS OF ARRAYS

🡪 Arrays are simple to understand and


elements of an array are easily accessible
🡪 But arrays have some limitations.
🡪 Arrays have a fixed dimension.
🡪 Once the size of an array is decided it can
not be increased or decreased during
program execution.
LIMITATIONS OF ARRAYS

🡪 Array elements are always stored in


contiguous memory locations.
🡪 Operations like insertion or deletion
of the array are pretty tedious.
🡪 To over come this limitationswe use
linked list.
LINKED LISTS
🡪 Linked list is a linear data structure.
🡪 Linked listis a collection of elements
called nodes.
🡪 Each node contains two parts. they are data
part and link part.
🡪 Each node contains two parts, i.e. DATA part
and LINK part.
🡪 The data contains elements and
🡪 Link contains address of another node.
data link
Linked List

A B C ∅

He
ad
A linked list is a series of
connected nodes
Each node contains at
least
A piece of data (any type)
Pointer to the next node in the list
node
Head: pointer to the first
node A

The last node points to dat pointe


NULL a r
14 100 30 400 42 N

200 100 400

🡪 The above figure shows the example of marks


obtained by different students can be stored in
a linked list
🡪 Here N-stands for NULL.
🡪 Null indicates the end of the node.
Arrays Vs Linked Lists
Arrays Linked list

Fixed size: Resizing is expensive Dynamic size

Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting

Random access i.e., efficient indexing No random access


🡪 Not suitable for operations requiring
accessing elements by index such as sorting

No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.

Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Arrays vs Linked List
(Summary)
Types of lists

🡪 There are two basic types of linked list

▪ Singly Linked list

▪ Doubly linked list


Singly Linked List

🡪 Each node has only one link part

🡪 Each link part contains the address of the next node


in the list

🡪 Link part of the last node contains NULL value


which signifies the end of the node
Schematic Representation
🡪 Here, it is a singly-linked list (SLL):

head

a b c d

🡪 Each node contains a value(data) and a pointer, which


points to the next node of the list

🡪 head is the header pointer which points to the first node


of the list
Basic Operations on a list

🡪 Creating a List
🡪 Traversing in List
🡪 Inserting an element in a list
🡪 Deleting an element from a list
🡪 Searching a list
🡪 Reversing a list
Creating a node

struct node{
int data; // A simple node of a linked list
node*next;
}*start; //start points at the first node
start=NULL ; initialised to NULL at beginning
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST

Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT

void display(struct node *ptr)


{
while(ptr != NULL)
{
printf("%d ", ptr->data); // process
ptr = ptr->next;
}
}
Inserting the node in a SLL
🡪 There are 3 cases here:-
🡪 Insertion at the beginning
🡪 Insertion at the end
🡪 Insertion after a particular node
Insertion at the beginning
There are two steps to be
followed:-
a) Make the next pointer of the node point towards
the first node of the list

b) Make the start pointer point towards this new node

▪ If the list is empty simply make the start


pointer point towards the new node;
Insertion at the beginning
1 7 3 4 2 6 5 X

START

9 1 7 3 4 2 6 5 X

START

ALGORITHM: INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST


Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT
void insert_beg(node* p)
{
node* temp;
if(start==NULL) //if the list is empty
{
start=p;
Printf(”\nNode inserted successfully at the
beginning”);
}
else {
temp=start;
start=p;
p->next=temp;//making new node point at
} the first node of the list
}
Inserting at the end

🡪 Here we simply need to make the next pointer of


the last node point to the new node
Inserting at the end
ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
Inserting at the end
1 7 3 4 2 6 5 X

START, PTR

9 X
1 7 3 4 2 6 5

START
void insert_end(node* p)
{
node *q=start;
if(start==NULL)
{
start=p;
Printf(”\nNode inserted successfully at the
end…!!!\n”);
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
Inserting after an element
Here weagain need to do 2 steps :-

▪ Make the next pointer of the node to be inserted


point to the next node of the node after which
you want to insert the node

▪ Make the next pointer of the node after which


the node is to be inserted, point to the node to
be inserted
Inserting after an element
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE
NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
void insert_after(int c,node* p)
{
node*
q;
q=start;
for(int i=1;i<c;i++)
{
q=q->link;
if(q==NULL)
printf(”Less than and nodes in the list…!!!”);
}
p->link=q->link;
q->link=p;
printf(”\nNode inserted successfully”);
}
Deleting a node in SLL
Here also we have three cases:-

🡪 Deleting the first node

🡪 Deleting the last node

🡪 Deleting the intermediate node


Deleting the first node
Here we apply 2 steps:-

🡪 Making the start pointer point towards the


2nd node

🡪 Deleting the first node using delete keyword

start

one two three


void del_first()
{
if(start==NULL)
printf(”\nError……List is
empty\n”); else
{
node* temp=start;
start=temp->link;
delete temp;
Printf{”\nFirst node deleted
successfully….!!!”);
}
}
Deleting the first node
Algorithm to delete the first node from the linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
1 7 3 4 2 6 5 X
Step 5: EXIT

START

7 3 4 2 6 5 X

START
Deleting the last node
Here we apply 2 steps:-

🡪 Making the second last node’s next pointer point to


NULL

🡪 Deleting the last node via delete keyword

start

45 85 node3
Deleting the last node
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR 1 7 3 4 2 6 5 X
Step 8: EXIT
START, PREPTR, PTR

1 7 3 4 2 6 X 5 X

PREPTR PTR
START
void del_last()
{
if(start==NULL)
printf(”\nError….List is
empty”); else
{
node* q=start;
while(q->link->link!=NULL)
q=q->link;
node* temp=q->link;
q->link=NULL;
delete temp;
Printf(”\nDeleted successfully…”);
}
}
}
Deleting a particular node

🡪 Here we make the next pointer of the node previous


to the node being deleted ,point to the successor
node of the node to be deleted and then delete the
node using delete keyword

node1 node2 node3

To be deleted
Deleting a particular node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE
LINKED LIST

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
Deleting a particular node
1 7 3 4 2 6 5 X

START, PREPTR, PTR

1 7 3 4 2 6 5 X

START PREPTR PTR

1 7 3 4 2 6 5 X

START

1 7 3 4 6 5 X
void del(int c)
{
node* q=start;
for(int i=2;i<c;i++)
{
q=q->link;
if(q==NULL)
Printf(”\nNode not
found\n”);
}
if(i==c)
{ node* p=q->link; //node to be deleted
q->link=p->link; //disconnecting the node p
delete p;
Printf(“Deleted Successfully”);
}
}
Searching in a SLL
🡪 Searching involves finding the required element in
the given list
🡪 We can use various techniques of searching like
linear search or binary search where binary search
is more efficient in case of Arrays
🡪 But in case of linked list since random access is not
available it would become complex to do binary
search in it
🡪 We can perform simple linear search traversal
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS

struct node* search(struct node *ptr, int num)


{
while((ptr != NULL) && (ptr->data != num))
{
ptr = ptr->next;
}
return ptr;
}
// call example, search(START, 4)
In linear search each node is traversed till thedata
in
thenode matches with therequired value

void search(int x)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
Printf(“FOUND %d”,temp->data);
break;
}
temp=temp->next;
}
}
Reversing a linked list
🡪 We can reverse a linked list by reversing the
direction of the links between 2 nodes
🡪 We make use of 3 structure pointers say p,q,r

🡪 At any instant q will point to the node next to p and r


will point to the node next to q

Head P p q NULL
q r

NULL

•For next iteration p=q and q=r

•At the end we will change head to the last node


Code
void reverse()
{
node*p,*q,*r;
if(start==NULL)
{
Printf("\nList is empty\n");
return;
}
p=start;
q=p->link;
p->link=NULL;
while(q!=NULL)
{
r=q->link;
q->link=p;
p=q;
q=r;
}
start=p;
Printf("\nReversed
successfully");
Circular linked list
• In a circular linked list, the last node contains a pointer to the
first node of the list.
• We can have a circular singly listed list as well as circular doubly
linked list.
• While traversing a circular linked list, we can begin at any node
and traverse the list in any direction forward or backward until
we reach the same node where we had started.
• Thus, a circular linked list has no beginning and no ending.
START

1 2 3 4 5 6 7
Inserting a node in CSLL
Here also we have three cases:-

🡪 Inserting the first node

🡪 Inserting the last node

🡪 Inserting the intermediate


node
Circular linked list
Algorithm to insert a new node in the
beginning of circular the linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR->NEXT != START
Step 7: PTR = PTR->NEXT
Step 8: SET New_Node->Next = START
Step 8: SET PTR->NEXT = New_Node
Step 6: SET START = New_Node
Step 7: EXIT
Circular linked list
1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5

PTR
START

9 1 7 3 4 2 6 5

START
Circular linked list
Algorithm to insert a new node at the
end of the circular linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != START
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR ->NEXT = New_Node
Step 10: EXIT
Circular linked list
1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5 9

PTR
START
Circular linked list
Algorithm to insert a new node after
a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Deleting a node in CSLL
Here also we have three cases:-

🡪 Deleting the first node

🡪 Deleting the last node

🡪 Deleting the intermediate node


Circular linked list
Algorithm to delete the first node from
the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PTR = PTR->NEXT
[END OF IF]
Step 5: SET PTR->NEXT = START->NEXT
Step 6: FREE START
Step 7: SET START = PTR->NEXT
Step 8: EXIT
Circular linked list
1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5

PTR
START

7 3 4 2 6 5

START
Circular linked list
Algorithm to delete the last node of the
circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = START
Step 7: FREE PTR
Step 8: EXIT
Circular linked list
1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5

PREPTR
PTR
START

1 7 3 4 2 6

START
Circular linked list
Algorithm to delete the node after a given
node from the circular linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT
Circular linked list
1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5

START PREPTR PTR

1 7 3 4 6 5

START
Doubly Circular Linked List
1. Doubly circular linked list is a linked data structure that
consists of a set of sequentially linked records called nodes.

2. Each node contains three fields ::


-: one is data part which contain data only.
-:two other field is links part that are point
or references to the previous or to the next
node in the sequence of nodes.

3. The beginning and ending nodes' previous and next


links, respectively, point to some kind of terminator,
typically a sentinel node or null to facilitate traversal
of the list.
NODE
previous data next

A B C
NULL 11 786 200 656 400 786 777 NULL

200 786 400

A doubly linked list contain three fields: an integer value, the


link to the next node, and the link to the previous node.
DLL’s compared to
SLL’s
⚫ Advantages: ⚫ Disadvantages:
⚫ Can be traversed in either ⚫ Requires more space
direction (may be ⚫ List manipulations are
essential for some slower (because more
programs) links must be
⚫ Some operations, such as changed)
deletion and inserting ⚫ Greater chance of having
before a node, become bugs (because more links
easier must be manipulated)
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};

previous. .Data .next


inf
Inserting a node in DLL
Here also we have three cases:-

🡪 Inserting the first node

🡪 Inserting the last node

🡪 Inserting the intermediate


node
Inserting at beginning
Inserting at beginning
Algorithm to insert a new node in the beginning of the
doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 8
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->PREV = NULL
Step 6: SET New_Node->Next = START
Step 7: SET START = New_Node
Step 8: EXIT
Inserting at beginning
X 1 7 3 4 2 X

START

2 X
X 9 1 7 3 4

START
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;
Printf("\nNode inserted successfully at the
beginning\m");
}
else
{
node* temp=start;
temp->previous=p //making 1st node’s previous point to the
start=p;
; new node
p->next=temp; //making next of the new node point to
1st node the
Printf("\nNode inserted successfully at the
beginning\n");
}
Inserting at the end
Inserting at the end
Algorithm to insert a new node at the end of the doubly linked
list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: New_Node->PREV = PTR
Step 11: EXIT
Inserting at the end
X 1 7 3 4 2 X

START, PTR

X 1 7 3 4 2 9 X

PTR
void insert_end(node* p)
{
if(start==NULL)
{
start=p;
Printf("\nNode inserted successfully at the end");
}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;
Printf("\nNode inserted successfully at the
end\n");
}
}
Inserting after a node

Making next and previous pointer of the node to be


inserted point accordingly

Adjusting the next and previous pointers of the nodes b/w


which the new node accordingly
Inserting after a node
Algorithm to insert a new node after a node that has
value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 10: SET PTR->NEXT->PREV = New_Node
Step 11: EXIT
Inserting after a node
X 1 7 3 4 2 X

START, PTR

X 1 7 3 4 2 X

START PTR

X 1 7 3 9 4 2 X

START
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp=temp->next;
}
p->next=temp->next;
temp->next->previous=p;
temp->next=p;
p->previous=temp;
printf("\nInserted
successfully");
}
Deleting a node in DLL
Here also we have three cases:-

🡪 Deleting the first node

🡪 Deleting the last node

🡪 Deleting the intermediate node


Deleting a node
• Node deletion from a DLL involves changing two links
• In this example,we will delete node b
myDLL

a b c

• We don’t have to do anything about the links in node b


• Garbage collection will take care of deleted nodes
• Deletion of the first node or the last node is a special
case
Deleting a node
Algorithm to delete the first node from the doubly linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
X 1 7 3 4 2 X
Step 6: EXIT

START, PTR

7 3 4 2 X
void del_at(int c)
{
node*s=start;
{
for(int i=1;i<c-1;i++)
{
s=s->next;
}
node* p=s->next;
s->next=p->next;
p->next->previous=s;
delete p;
printf("\nNode number and deleted successfully");
}
}
}
Deleting the first node
Algorithm to delete the first node from the doubly linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
X 1 7 3 4 2 X
Step 6: EXIT

START, PTR

7 3 4 2 X
Deleting the last node

Algorithm to delete the last node of the doubly linked list


Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
Deleting the last node
X 1 3 5 7 8 9 X
1

START, PTR

X 1 3 5 7 8 9 X
1

START PTR

X 1 3 5 7 8 X

START
Deleting the node after a specific
location
Algorithm to delete the node after a given node from the
doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Deleting the node after a specific
location
X 1 3 4 7 8 9 X
1

START, PTR

X 1 3 4 7 8 9 X
1

START
PTR

X 1 3 4 8 9 X

START
Circular Doubly Linked List
• A circular doubly linked list or a circular two way linked
list is a more complex type of linked list which contains a
pointer to the next as well as previous node in the
sequence.
• The difference between a doubly linked and a circular
doubly linked list is same as that exists between a singly
linked list and a circular linked list.
• The circular doubly linked list does not contain NULL in
the previous field of the first node and the next field of
the last node.
Circular Doubly Linked List
• Rather, the next field of the last node stores the address
of the first node of the list, i.e; START.
• Similarly, the previous field of the first field stores the
address of the last node.
Circular Doubly Linked List
• Since a circular doubly linked list contains three parts in
its structure, it calls for more space per node and for
more expensive basic operations.
• However, it provides the ease to manipulate the
elements of the list as it maintains pointers to nodes in
both the directions . The main advantage of using a
circular doubly linked list is that it makes searches twice
as efficient. START

2 3 4
1 1
Inserting a node in CDLL

Here also we have three cases:-

🡪 Inserting the first node

🡪 Inserting the last node

🡪 Inserting the intermediate


node
Circular Doubly Linked List
Algorithm to insert a new node in the beginning of the
circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 6: SET START->PREV->NEXT = new_node;
Step 7: SET New_Node->PREV = START->PREV;
Step 8: SET START->PREV= new_Node;
Step 9: SET new_node->next = START;
Step 10: SET START = New_Node
Step 11: EXIT
Circular Doubly Linked List
Insert a new node in the beginning

1 7 3 4 2

START

2
9 1 7 3 4

START
Insertion at the END in CDLL
🡪 Algorithm to insert a new node at the end
of the circular doubly linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 7: SET START->PRE->NEXT=New_Node
Step 8: SET START->PREV= New_Node
Step 7: EXIT 1 7 3 4 2
START

1 7 3 4 2 9

START
Circular Doubly Linked List
🡪 Algorithm to insert a new node after a
node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a node

1 7 3 4 2

START, PTR

1 7 3 4 2

PTR

9
START
1 7 3 9 4 2

START
Deleting a node in DLL
Here also we have three cases:-

🡪 Deleting the first node

🡪 Deleting the last node

🡪 Deleting the intermediate node


Circular Doubly Linked List
Algorithm to delete the first node from the circular doubly
linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PTR->PREV=>NEXT= PTR->NEXT
Step 4: SET PTR->NEXT->PREV = PTR->PREV
Step 5: SET START = START->NEXT
Step 6: FREE PTR
1 3 5 7 8 9
Step 7: EXIT 1

START, PTR

3 5 7 8 9
1

START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly
linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START->PREV
Step 5: SET PTR->PREV->NEXT = START
Step 6: SET START->PREV = PTR->PREV
Step 7: FREE PTR 1 3 5 7 8 9
Step 8: EXIT 1

START PTR

X 1 3 5 7 8

START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the
circular doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Circular Doubly Linked List
1 3 5 7 8 91

START

1 3 5 7 8 91

START PTR

1 3 4 8 9

START
Circular Doubly Linked List
🡪 A header linked list is a special type of linked list which
contains a header node at the beginning of the list.
🡪 So, in a header linked list START will not point to the first
node of the list but START will contain the address of the
header node.
🡪 There are basically two variants of a header linked list-
🡪 Grounded header linked list which stores NULL in the
next field of the last node
🡪 Circular header linked list which stores the address of
the header node in the next field of the last node.
Here, the header node will denote the end of the list.
Circular Doubly Linked List
Header Node

1 2 3 4 5 6 X

START

Header Node

1 2 3 4 5 6

START
Circular Doubly Linked List
Algorithm to traverse a Circular Header Linked List

Step 1: SET PTR = START->NEXT


Step 2: Repeat Steps 3 and 4 while PTR != START
Step 3: Apply PROCESS to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
APPLICATIONS OF LINKED LIST

🡪 Applications that have an MRU (Most Recently Used)


list (a linked list of file names)

🡪 The cache in your browser that allows you to hit the BACK
button (a linked list of URLs)

🡪 Undo functionality in Photoshop or Word (a linked list of


state)

🡪 A stack, hash table, and binary tree can be implemented


using a doubly linked list.

Polynomials
•Array Implementation:
•p1(x) = 8x3 + 3x2 + 2x + 6
•p2(x) = 23x4 + 18x - 3
p1(x) p2(x)

6 2 3 8 -3 18 0 0 23
0 2 0 2 4

Index
represents
exponents
•This is why arrays aren’t good to represent
polynomials:

•p3(x) = 16x21 - 3x5 + 2x + 6

6 2 0 0 -3 0 ………… 0 16

WASTE OF
SPACE!
•Advantages of using an Array:

• Only good for non-sparse polynomials.


• Ease of storage and retrieval.

•Disadvantages of using an Array:


• Have to allocate array size ahead of time.
•Huge array size required for sparse
polynomials.
• Waste of space and runtime.
• Polynomial Representation

•Linked list Implementation:

•p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3


•p2(x) = 4x6 + 10x4 + 12x + 8

P1 23 9 18 7 41 6 18 7 3 0

TAIL (contains pointer)

P2 4 6 10 4 12 1 8 0

NODE (contains coefficient & exponent)


•Advantages of using a Linked list:

🡪 save space (don’t have to worry about sparse


polynomials) and easy to maintain
🡪 don’t need to allocate list size and can declare
nodes (terms) only as needed

•Disadvantages of using a Linked list :


🡪 can’t go backwards through the list
🡪 can’t jump to the beginning of the list from the end.
Polynomials e e e
m1 m 2 0
A(x) am1 xam x ... a0 x
2
Representation

struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;

coef exp next


Adding polynomials using a Linked list
representation: (storing the result in p3)

To do this, we have to break the process down


to cases:

•Case 1: exponent of p1 > exponent of p2


• Copy node of p1 to end of p3. [go to
next node]
•Case 2: exponent of p1 < exponent of p2
• Copy node of p2 to end of p3. [go to
next node]
🡪 Case 3: exponent of p1 = exponent of p2

🡪 Create a new node in p3 with the same exponent


and with the sum of the coefficients of p1 and p2.
Example
a 14 2x8 1
3x
a
3 14 2 8 1 0 null

b 14 10 10x6
8x 3x
b
8 14 -3 10 10 6 null
Adding
Polynomials
3 14
a
2 8 1 0

8 14 -3 10 6
b 10
11 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 6
10 b
11 14 -3 a->expon < b->expon
10
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
d
a->expon > b->expon
Dynamic Memory Management
🡪 The Library makes use of a dynamic memory manager which handles allocation
and deallocation of dynamic memory.
🡪 The methods used for allocation and deallocation are wrapper functions around
the native malloc, calloc, realloc, and free methods.
🡪 Hence, the functionality of teh module is very similar to the native C interface
but it allows for structured error recovery and application termination in case of
failure. It covers especially the following three situations:
🡪 Handling of allocation and deallocation of dynamic memory
🡪 Recovering from temporary lack of available memory
🡪 Panic handling in case a new allocation fails
Standard library functions for dynamic
memory management
Sparse Matrix
🡪 In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with 'm' columns and 'n' rows represent a m X n matrix.
🡪 There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix.
🡪 Sparse matrix is a matrix which contains very few non-zero elements.
🡪 When a sparse matrix is represented with a 2-dimensional array, we waste
a lot of space to represent that matrix.
🡪 For example, consider a matrix of size 100 X 100 containing only 10
non-zero elements. In this matrix, only 10 spaces are filled with non-zero
values and remaining spaces of the matrix are filled with zero.
🡪 That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to
store this integer matrix. And to access these 10 non-zero elements we
have to make scanning for 10000 times. To make it simple we use the
following sparse matrix representation.
Sparse Matrix Representations
🡪 In computer programming, a matrix can be defined with a 2-dimensional array.
Any array with 'm' columns and 'n' rows represent a m X n matrix.
🡪 There may be a situation in which a matrix contains more number of ZERO
values than NON-ZERO values. Such matrix is known as sparse matrix.
🡪 Sparse matrix is a matrix which contains very few non-zero elements.
🡪 When a sparse matrix is represented with a 2-dimensional array, we waste a lot
of space to represent that matrix.
🡪 For example, consider a matrix of size 100 X 100 containing only 10 non-zero
elements. In this matrix, only 10 spaces are filled with non-zero values and
remaining spaces of the matrix are filled with zero.
🡪 That means, totally we allocate 100 X 100 X 2 = 20000 bytes of space to store
this integer matrix. And to access these 10 non-zero elements we have to make
scanning for 10000 times. To make it simple we use the following sparse matrix
representation.
Sparse Matrix Representations

🡪 A sparse matrix can be represented by using TWO representations, those are as


follows...
🡪 Triplet Representation (Array Representation)
🡪 Linked Representation
Sparse Matrix Representations
Triplet Representation (Array Representation)
🡪 In this representation, we consider only non-zero values along with their row
and column index values.
🡪 In this representation, the 0th row stores the total number of rows, total number
of columns and the total number of non-zero values in the sparse matrix.
🡪 For example, consider a matrix of size 5 X 6 containing 6 number of non-zero
values. This matrix can be represented as shown in the image
Sparse Matrix Representations
Triplet Representation (Array Representation)
🡪 In above example matrix, there are only 6 non-zero elements ( those are 9, 8,
4, 2, 5 & 2) and matrix size is 5 X 6.
🡪 We represent this matrix as shown in the above image. Here the first row in the
right side table is filled with values 5, 6 & 6 which indicates that it is a sparse
matrix with 5 rows, 6 columns & 6 non-zero values.
🡪 The second row is filled with 0, 4, & 9 which indicates the non-zero value 9 is
at the 0th-row 4th column in the Sparse matrix.
🡪 In the same way, the remaining non-zero values also follow a similar pattern.
Assignment -2

🡪 Write a C program to implement the concept of polynomial addition and


multiplication using the linked list.
🡪 Write a C program to implement the concept of sparse matrix to perform the
addition and transpose using the linked list.
THANK
YOU

You might also like