SlideShare a Scribd company logo
CCC Digital India Pvt Ltd. 2nd Floor, Quantum Hub, Siripuram Jn, Visakapatnam-530003
UNIT-2
Linked lists: Introduction, linked lists and types, Representation of linked lists, Operations on linked list, Comparison of liked
lists with arrays and Dynamic arrays .
DATA STRUCTURES
•Data structure is a process of storing and organizing the data in different ways
•Data structures can be used to solve many real time problems
•Each data structure is useful in different situations
TYPES OF DATA STRUCTURES
DIFFERENCES BETWEEN LINEAR AND NON-LINEAR DATA STRUCTURES:
PRE REQUISITE FOR DATA STRUCTURES:
To do operations on data structures, everyone should have good skills over the following concepts
•Pointers
•Structures
•Dynamic memory allocation
•Recursion
LINKED LIST
•It is a linear data structure with collection of similar elements
•Each element in a linked list is known as node
•In this it consist set of nodes
•Each node contains a relation with next node using node’s address
•Compared to arrays, linked list are used when number of elements are unknown
•Compared to arrays memory is efficiently used in linked list because of dynamic nature
TYPES OF LINKED LIST
Types of linked list are
•Single linked list
•Double linked list
•Circular single linked list
•Circular double linked list
APPLICATIONS OF LINKED LIST
•To implement other data structures like stack, queue, trees and graphs
•To perform arithmetic operations on long integers
•To perform arithmetic operations on polynomials
•To maintain directory of names
•To represent sparse matrices etc.
SINGLE LINKED LIST
•In this each node consist two fields. They are data field and address field. They are
i) Data field: Consist value to be stored
ii) Address field: Consist address of the next node
•In this we can traverse only in forward direction
•A head/start pointer consist address of the first node
•Initially head==NULL
•Every node address field consist address of its next node
•Last node address is NULL to indicate end of the list
•NULL is a pre-defined macro. It’s value is 0
•printf(“null value=%d”,NULL); //null value=0
SINGLE LINKED LIST REPRESENTATION:
SINGLE LINKED LIST DECLARATION:
struct node
{
int data;
struct node *next;
};
struct node *newnode, *head=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
OPERATIONS ON LINKED LIST:
•Inserting at the beginning of the list
•Inserting at the end of the list
•Inserting at the particular position of the list
•Deleting from the beginning
•Deleting from the end
•Deleting from the specified location
•Displaying list of values
•Counting number of nodes
•Printing list in reverse order
•Finding cycle in the list
•Finding merge point of the two lists etc.
INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM:
INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM
STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
Else, first store address of newnode in the next field then make newnode as first node
by storing its address in head
else // case 2: there are more nodes in LL
{
newnode->next=head;
head=newnode;
}
INSERT AT END OF SINGLE LINKED LIST ALGORITHM:
STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
Else, create a pointer P, store head address in it.
struct node *p=head;
STEP-4
Now move P to the end of the linked list
while(p->next!=NULL)//while(p->next)
p=p->next;
STEP-5
Now store newnode address in P next field in place of NULL
p->next=newnode;
INSERTING A NODE AT SPECIFIC POSITION OF SINGLE LINKED LIST
STEP-1
Allocate memory to newnode, store x in data field, NULL in address field
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=NULL;
STEP-2
If there are no nodes in the linked list ,then assign newnode address to head
if(head==NULL) //case 1: there are no nodes in LL
head=newnode;
STEP-3
If position==1 insert node at the beginning
newnode->next=head;
head=newnode;
STEP-4 else,
STEP-5
Now store newnode address in P next field in place of NULL
p->next=newnode;
DISPLAYING SINGLE LINKED LIST ALGORITHM
STEP-1
Check whether list is EMPTY or not with condition
(head==NULL)
STEP-2
If list is EMPTY, then display "List is empty, cannot display element”, and terminate
the function
STEP-3
If list is NOT EMPTY, then define a pointer called temp, and initialize it with head i.e.,
temp=head
STEP-4 Display the node data with print(tempdata)
STEP-5
Now move temp to the next node using address
temp=tempnext, until it reaches end of list i.e., while(temp!=NULL)
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the single linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list is having only one node (temp → next == NULL)
•Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
•Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the single linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head.
•Step 4 - Check whether list has only one Node (temp1 → next == NULL)
•Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function.
•(Setting Empty list condition)
•Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
•Repeat the same until it reaches to the last node in the list. (until temp1 → next == NULL)
•Step 7 - Finally, Set temp2 → next = NULL and delete temp1.
Delete a Particular Node
The steps below can be used to remove a specific node from the single linked list:
Step 1: Check to see if the list is empty (head == NULL).
Step 2: If the list is empty, display the message ‘List is Empty!!!’ Deletion is not possible’, and the function is terminated.
Step 3: If it’s not empty, create two Node pointers, ‘temp1’ and ‘temp2,’ and set ‘temp1’ to head.
Step 4: Continue dragging the temp1 until it approaches the last node or the particular node to be deleted.
And before moving ‘temp1’ to the next node, always set ‘temp2 = temp1’.
Step 5: Once you’re at the last node, the message “Given node not found in the list! Deletion Not Possible” appears.
Finally, the function is terminated.
Step 6: If you’ve reached the particular node which you want to delete, check to see if the list has only one node.
Step 7: Set head = NULL and remove temp1 (free(temp1)) if list contains just one node and that is the node to be deleted.
Step 8: Check if temp1 is the first node in the list (temp1 == head) if the list has several nodes.
Step 9: If temp1 is the initial node, remove temp1 and shift the head to the next node (head = head→next).
Step 10: If temp1 isn’t the first node in the list (temp1→next == NULL), see if it’s the final one.
Step 11: Set temp2→next = NULL and remove temp1 (free(temp1)) if temp1 is the final node.
Step 12: Set temp2→next = temp1 →next and remove temp1 (free(temp1)) if temp1 is not the first or last node.
ARRAY Vs LINKED LIST
DYNAMIC ARRAY Vs LIKED LIST
Compared to linked lists, dynamic arrays have faster indexing (constant time versus linear time) and typically
faster iteration due to improved locality of reference; however, dynamic arrays require linear time to insert or delete
at an arbitrary location, since all following elements must be moved, while linked lists can do this in constant time.
Linked lists do not require the whole data structure to be stored contiguously whereas it may be expensive or
impossible to find contiguous space for a large dynamic array
Dynamic arrays are really contiguous memory slots allocated dynamically to a variable.
With a reference/pointer to the first memory address in the allocated contiguous memory blocks, you have direct
access to each slot - Array indexes are "added" to the address and the values stored at the resulting
address is returned, making it constant-time access. Nodes within a linked list is accessed sequentially.
To get to a node, you need to run through every node prior to it within the list.
CIRCULAR LINKED LIST
•In CLL the last node does not consist NULL pointer
•Last node consist address of the first node
CLL ADVANTAGES
•Any node can be a starting point
•We don’t need to maintain two pointers for front and rear if we use circular linked list.
We can maintain a pointer to the last inserted node and front can always be obtained as next of last
•Circular lists are useful in applications to repeatedly go around the list.
CLL REPRESENTATION
•In CLL take a pointer tail instead of head for easy access
struct Node
{
int data;
struct Node *next;
}*tail=NULL, *temp;
INSERT AT BEGINNING OF CIRCULAR LL ALGORITHM
STEP-1
Create a newnode, store value add NULL address
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=value;
newNode->next=NULL;
STEP-2
Check whether list is EMPTY or not with condition
(tail==NULL)
STEP-3
If list is EMPTY, then assign newnode address to tail and tail->next
tail=newnode
tail->next=newnode
STEP-4
If list is NOT EMPTY, then in the newnode->next store tail->next address and make
newnode as first node with tail
newNode->next=tail->next;
tail->next=newNode;
STEP-5 Display value is inserted in Circular LL
INSERT AT END OF CIRCULAR LL ALGORITHM
STEP-1
Create a newnode, store value add NULL address
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=value;
newNode->next=NULL;
STEP-2
Check whether list is EMPTY or not with condition
(tail==NULL)
STEP-3
If list is EMPTY, then assign newnode address to tail and tail->next
tail=newnode
tail->next=newnode
STEP-4
If list is NOT EMPTY, then in the newnode->next store tail->next address and make
newnode as last node with tail
newNode->next=tail->next;
tail->next=newNode;
tail=newNode;
STEP-5 Display value is inserted in Circular LL
INSERT AT SPECIFIC LOCATION OF CIRCULAR LL ALGORITHM
These methods are used to put data into a circular linked list at a specified point.
•Terminate the function if the circular linked list is empty.
•If not, create two node pointers.
•Create a new node to store the data. Now, navigate to the desired location and place this new node there.
•Connect the pointer of this new node to the node that was previously existing at this location.
•To observe the outcome, print the new circular linked list.
Deletion of the First Node
The instructions below can be used to
remove a node from the circular linked
list’s beginning:
Step 1: Check for Overflow
if start = Null then
print list is empty
Exit
End if
Step 2: set ptr = start
Step 3: set start = start -> next
Step 4: print Element deleted is ,ptr ->
info
Step 5: set last -> next = start
Step 6: free ptr
Step 7: EXIT
Deletion of the Last Node
The instructions below can be used to remove a node from the circular linked list’s end:
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 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
START is used to initialize the pointer variable PTR, i.e., PTR now points to the linked list’s first node.
We use another pointer variable PREPTR in the while loop so that PREPTR always refers to one node before PTR.
We set the next pointer of the second last node to START after we reach the last node and the second last node,
making it the (new) final node of the linked list.
Deletion from Specific Point
Deletion from the nth place in a circular linked list is one of the deletion operations that we commonly execute on this
type of list. We utilize pointers to traverse the circular linked list and conduct deletion from a specified position.
These instructions are used to delete a specified place in a circular linked list:
•Initialize the p and q node pointers.
•Create a variable called k that will serve as a counter variable.
•Set the value of del to pos-1.
•Repeat step 5 until k does not equal del.
•Make q=p and p=p->next in this loop.
•After each successful repetition, increase the value of k by one.
•Make the next of q equal to the next of p.
•Delete the node referred to by the node pointer p.
DOUBLE LINKED LIST
•In SLL it is possible to move only in the forward direction
•You cannot move back in SLL because every node consist only address of next node
•But in DLL we can move in the forward and backward directions
•In DLL every node consist 3 three fields. They are
i) Data field: stores value
ii) Previous pointer field: stores address of previous node
iii) Next pointer field: stores address of next node
•First node previous field consist NULL value
•Last node next field consist NULL value
SINGLE LL Vs DOUBLE LL
INSERT AT BEGINNING OF DOUBLE LL ALGORITHM
STEP-1
Create a newnode, store value in data field and NULL in previous field of newnode
newnode -> data = value;
newnode -> previous = NULL;
STEP-2
If head==NULL, make the newnode as first node with assigning NULL in next field, and
make head as newnode
newnode -> next = NULL;
head = newnode;
STEP-3
Else store head address in next field of newnode
And make it as first node
newnode -> next = head;
head->prev=newnode;
head = newnode;
STEP-4 Display node is inserted
INSERT AT END OF DOUBLE LL ALGORITHM
STEP-1
Create a newnode, store value in data field and NULL in next field of newnode
newNode -> data = value;
newNode ->next = NULL;
STEP-2
If head==NULL, make the newnode as last node with assigning NULL in previous field, and make head as newnode
newNode -> previous = NULL;
head = newNode;
STEP-3
Else create a temp node, and assign head address in it. Move temp node to the last node. Then store temp address in
previous and next fields
struct Node *temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newNode;
newNode -> previous = temp;
STEP-4 Display node is inserted
Deletion
In a double linked list, the deletion operation can be performed in three
ways as follows...
•Deleting from Beginning of the list
•Deleting from End of the list
•Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function
.
•Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next)
•Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
•Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate
the function.
•Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
•Step 4 - Check whether list has only one Node (temp → previous and temp → next both
are NULL)
•Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the
function. (Setting Empty list condition)
•Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list.
(until temp → next is equal to NULL)
•Step 7 - Assign NULL to temp → previous → next and delete temp.
Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
•Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
•Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
•Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not
possible!!!' and terminate the function.
•Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node
or not
•Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and
delete temp (free(temp)).
•Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
•Step 9 - If temp is the first node, then move the head to the next node (head = head → next),
set head of previous to NULL (head → previous = NULL) and delete temp.
•Step 10 - If temp is not the first node, then check whether it is the last node in the list
(temp → next == NULL).
•Step 11 - If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp (free(temp)).
•Step 12 - If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp
→ previous) and delete temp (free(temp)).
We can use the following steps to display the elements of a double linked list...
•Step 1 - Check whether list is Empty (head == NULL)
•Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the
function.
•Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize
with head.
•Step 4 - Display 'NULL <--- '.
•Step 5 - Keep displaying temp → data with an arrow (<===>)
until temp reaches to the last node
•Step 6 - Finally, display temp → data with arrow pointing to NULL (temp →
data ---> NULL).
Displaying a Double Linked List

More Related Content

Similar to VCE Unit 02 (1).pptx

DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
beshahashenafe20
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
ISHAN194169
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
SonaPathak5
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
msohail37
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
Dabbal Singh Mahara
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Linked list
Linked listLinked list
Linked list
RahulGandhi110
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
Ain-ul-Moiz Khawaja
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
AfriyieCharles
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
Mandeep Singh
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
Khuram Shahzad
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
ssuser7922b8
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
EmilyMengich
 
Data structures2
Data structures2Data structures2
Data structures2
Parthipan Parthi
 

Similar to VCE Unit 02 (1).pptx (20)

DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssssDSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
DSA chapter 4.pptxhdjaaaaaadjhsssssssssssssssssssssssssss
 
Linked list
Linked list Linked list
Linked list
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
single linked list
single linked listsingle linked list
single linked list
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
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
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Arrays and linked lists
Arrays and linked listsArrays and linked lists
Arrays and linked lists
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
5.Linked list
5.Linked list 5.Linked list
5.Linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
Data structures2
Data structures2Data structures2
Data structures2
 

More from skilljiolms

VCE - UNIT-II.pptx
VCE - UNIT-II.pptxVCE - UNIT-II.pptx
VCE - UNIT-II.pptx
skilljiolms
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
skilljiolms
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
skilljiolms
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
skilljiolms
 
PP DBMS - 2 (1) (1).pptx
PP DBMS - 2 (1) (1).pptxPP DBMS - 2 (1) (1).pptx
PP DBMS - 2 (1) (1).pptx
skilljiolms
 
PP DBMS - 1 (2).pptx
PP DBMS - 1 (2).pptxPP DBMS - 1 (2).pptx
PP DBMS - 1 (2).pptx
skilljiolms
 
PP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptxPP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptx
skilljiolms
 
PP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptxPP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptx
skilljiolms
 

More from skilljiolms (10)

VCE - UNIT-II.pptx
VCE - UNIT-II.pptxVCE - UNIT-II.pptx
VCE - UNIT-II.pptx
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
PP DBMS - 2 (1) (1).pptx
PP DBMS - 2 (1) (1).pptxPP DBMS - 2 (1) (1).pptx
PP DBMS - 2 (1) (1).pptx
 
PP DBMS - 1 (2).pptx
PP DBMS - 1 (2).pptxPP DBMS - 1 (2).pptx
PP DBMS - 1 (2).pptx
 
PP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptxPP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptx
 
PP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptxPP DBMS - 1 (1).pptx
PP DBMS - 1 (1).pptx
 

Recently uploaded

Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel ChartSatta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
DearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUniDearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUni
katiejasper96
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
Corey Perlman, Social Media Speaker and Consultant
 
Best practices for project execution and delivery
Best practices for project execution and deliveryBest practices for project execution and delivery
Best practices for project execution and delivery
CLIVE MINCHIN
 
Top mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptxTop mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptx
JeremyPeirce1
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
taqyea
 
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & InnovationInnovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Operational Excellence Consulting
 
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Neil Horowitz
 
Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024
Adnet Communications
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
Top Forex Brokers Review
 
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
Lviv Startup Club
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431
ecamare2
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
timesbpobusiness
 
2024-6-01-IMPACTSilver-Corp-Presentation.pdf
2024-6-01-IMPACTSilver-Corp-Presentation.pdf2024-6-01-IMPACTSilver-Corp-Presentation.pdf
2024-6-01-IMPACTSilver-Corp-Presentation.pdf
hartfordclub1
 
GKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt PresentationGKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt Presentation
GraceKohler1
 
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
Lacey Max
 
Digital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital ExcellenceDigital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital Excellence
Operational Excellence Consulting
 
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
my Pandit
 
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
Stephen Cashman
 

Recently uploaded (20)

Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel ChartSatta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
 
DearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUniDearbornMusic-KatherineJasperFullSailUni
DearbornMusic-KatherineJasperFullSailUni
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
 
Best practices for project execution and delivery
Best practices for project execution and deliveryBest practices for project execution and delivery
Best practices for project execution and delivery
 
Top mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptxTop mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptx
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
 
Innovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & InnovationInnovation Management Frameworks: Your Guide to Creativity & Innovation
Innovation Management Frameworks: Your Guide to Creativity & Innovation
 
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
Brian Fitzsimmons on the Business Strategy and Content Flywheel of Barstool S...
 
Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024Lundin Gold Corporate Presentation - June 2024
Lundin Gold Corporate Presentation - June 2024
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
 
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
Maksym Vyshnivetskyi: PMO KPIs (UA) (#12)
 
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta MatkaDpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
Dpboss Matka Guessing Satta Matta Matka Kalyan Chart Satta Matka
 
Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431Observation Lab PowerPoint Assignment for TEM 431
Observation Lab PowerPoint Assignment for TEM 431
 
TIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup IndustryTIMES BPO: Business Plan For Startup Industry
TIMES BPO: Business Plan For Startup Industry
 
2024-6-01-IMPACTSilver-Corp-Presentation.pdf
2024-6-01-IMPACTSilver-Corp-Presentation.pdf2024-6-01-IMPACTSilver-Corp-Presentation.pdf
2024-6-01-IMPACTSilver-Corp-Presentation.pdf
 
GKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt PresentationGKohler - Retail Scavenger Hunt Presentation
GKohler - Retail Scavenger Hunt Presentation
 
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
How are Lilac French Bulldogs Beauty Charming the World and Capturing Hearts....
 
Digital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital ExcellenceDigital Transformation Frameworks: Driving Digital Excellence
Digital Transformation Frameworks: Driving Digital Excellence
 
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
Taurus Zodiac Sign: Unveiling the Traits, Dates, and Horoscope Insights of th...
 
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
The Heart of Leadership_ How Emotional Intelligence Drives Business Success B...
 

VCE Unit 02 (1).pptx

  • 1. CCC Digital India Pvt Ltd. 2nd Floor, Quantum Hub, Siripuram Jn, Visakapatnam-530003 UNIT-2 Linked lists: Introduction, linked lists and types, Representation of linked lists, Operations on linked list, Comparison of liked lists with arrays and Dynamic arrays . DATA STRUCTURES •Data structure is a process of storing and organizing the data in different ways •Data structures can be used to solve many real time problems •Each data structure is useful in different situations TYPES OF DATA STRUCTURES
  • 2.
  • 3. DIFFERENCES BETWEEN LINEAR AND NON-LINEAR DATA STRUCTURES:
  • 4. PRE REQUISITE FOR DATA STRUCTURES: To do operations on data structures, everyone should have good skills over the following concepts •Pointers •Structures •Dynamic memory allocation •Recursion LINKED LIST •It is a linear data structure with collection of similar elements •Each element in a linked list is known as node •In this it consist set of nodes •Each node contains a relation with next node using node’s address •Compared to arrays, linked list are used when number of elements are unknown •Compared to arrays memory is efficiently used in linked list because of dynamic nature
  • 5. TYPES OF LINKED LIST Types of linked list are •Single linked list •Double linked list •Circular single linked list •Circular double linked list APPLICATIONS OF LINKED LIST •To implement other data structures like stack, queue, trees and graphs •To perform arithmetic operations on long integers •To perform arithmetic operations on polynomials •To maintain directory of names •To represent sparse matrices etc.
  • 6. SINGLE LINKED LIST •In this each node consist two fields. They are data field and address field. They are i) Data field: Consist value to be stored ii) Address field: Consist address of the next node •In this we can traverse only in forward direction •A head/start pointer consist address of the first node •Initially head==NULL •Every node address field consist address of its next node •Last node address is NULL to indicate end of the list •NULL is a pre-defined macro. It’s value is 0 •printf(“null value=%d”,NULL); //null value=0
  • 7. SINGLE LINKED LIST REPRESENTATION:
  • 8. SINGLE LINKED LIST DECLARATION: struct node { int data; struct node *next; }; struct node *newnode, *head=NULL; newnode=(struct node*)malloc(sizeof(struct node)); OPERATIONS ON LINKED LIST: •Inserting at the beginning of the list •Inserting at the end of the list •Inserting at the particular position of the list •Deleting from the beginning •Deleting from the end •Deleting from the specified location •Displaying list of values •Counting number of nodes •Printing list in reverse order •Finding cycle in the list •Finding merge point of the two lists etc.
  • 9. INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM: INSERT AT BEGINNING OF SINGLE LINKED LIST ALGORITHM
  • 10. STEP-1 Allocate memory to newnode, store x in data field, NULL in address field struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=x; newnode->next=NULL; STEP-2 If there are no nodes in the linked list ,then assign newnode address to head if(head==NULL) //case 1: there are no nodes in LL head=newnode; STEP-3 Else, first store address of newnode in the next field then make newnode as first node by storing its address in head else // case 2: there are more nodes in LL { newnode->next=head; head=newnode; }
  • 11. INSERT AT END OF SINGLE LINKED LIST ALGORITHM:
  • 12. STEP-1 Allocate memory to newnode, store x in data field, NULL in address field struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=x; newnode->next=NULL; STEP-2 If there are no nodes in the linked list ,then assign newnode address to head if(head==NULL) //case 1: there are no nodes in LL head=newnode; STEP-3 Else, create a pointer P, store head address in it. struct node *p=head; STEP-4 Now move P to the end of the linked list while(p->next!=NULL)//while(p->next) p=p->next; STEP-5 Now store newnode address in P next field in place of NULL p->next=newnode;
  • 13. INSERTING A NODE AT SPECIFIC POSITION OF SINGLE LINKED LIST
  • 14. STEP-1 Allocate memory to newnode, store x in data field, NULL in address field struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=x; newnode->next=NULL; STEP-2 If there are no nodes in the linked list ,then assign newnode address to head if(head==NULL) //case 1: there are no nodes in LL head=newnode; STEP-3 If position==1 insert node at the beginning newnode->next=head; head=newnode; STEP-4 else, STEP-5 Now store newnode address in P next field in place of NULL p->next=newnode;
  • 15. DISPLAYING SINGLE LINKED LIST ALGORITHM STEP-1 Check whether list is EMPTY or not with condition (head==NULL) STEP-2 If list is EMPTY, then display "List is empty, cannot display element”, and terminate the function STEP-3 If list is NOT EMPTY, then define a pointer called temp, and initialize it with head i.e., temp=head STEP-4 Display the node data with print(tempdata) STEP-5 Now move temp to the next node using address temp=tempnext, until it reaches end of list i.e., while(temp!=NULL)
  • 16.
  • 17. Deleting from Beginning of the list
  • 18. We can use the following steps to delete a node from beginning of the single linked list... •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. •Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head. •Step 4 - Check whether list is having only one node (temp → next == NULL) •Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions) •Step 6 - If it is FALSE then set head = temp → next, and delete temp. Deleting from End of the list We can use the following steps to delete a node from end of the single linked list...
  • 19. •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. •Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head. •Step 4 - Check whether list has only one Node (temp1 → next == NULL) •Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function. •(Setting Empty list condition) •Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. •Repeat the same until it reaches to the last node in the list. (until temp1 → next == NULL) •Step 7 - Finally, Set temp2 → next = NULL and delete temp1. Delete a Particular Node
  • 20. The steps below can be used to remove a specific node from the single linked list: Step 1: Check to see if the list is empty (head == NULL). Step 2: If the list is empty, display the message ‘List is Empty!!!’ Deletion is not possible’, and the function is terminated. Step 3: If it’s not empty, create two Node pointers, ‘temp1’ and ‘temp2,’ and set ‘temp1’ to head. Step 4: Continue dragging the temp1 until it approaches the last node or the particular node to be deleted. And before moving ‘temp1’ to the next node, always set ‘temp2 = temp1’. Step 5: Once you’re at the last node, the message “Given node not found in the list! Deletion Not Possible” appears. Finally, the function is terminated. Step 6: If you’ve reached the particular node which you want to delete, check to see if the list has only one node. Step 7: Set head = NULL and remove temp1 (free(temp1)) if list contains just one node and that is the node to be deleted. Step 8: Check if temp1 is the first node in the list (temp1 == head) if the list has several nodes. Step 9: If temp1 is the initial node, remove temp1 and shift the head to the next node (head = head→next). Step 10: If temp1 isn’t the first node in the list (temp1→next == NULL), see if it’s the final one. Step 11: Set temp2→next = NULL and remove temp1 (free(temp1)) if temp1 is the final node. Step 12: Set temp2→next = temp1 →next and remove temp1 (free(temp1)) if temp1 is not the first or last node.
  • 22. DYNAMIC ARRAY Vs LIKED LIST Compared to linked lists, dynamic arrays have faster indexing (constant time versus linear time) and typically faster iteration due to improved locality of reference; however, dynamic arrays require linear time to insert or delete at an arbitrary location, since all following elements must be moved, while linked lists can do this in constant time. Linked lists do not require the whole data structure to be stored contiguously whereas it may be expensive or impossible to find contiguous space for a large dynamic array Dynamic arrays are really contiguous memory slots allocated dynamically to a variable. With a reference/pointer to the first memory address in the allocated contiguous memory blocks, you have direct access to each slot - Array indexes are "added" to the address and the values stored at the resulting address is returned, making it constant-time access. Nodes within a linked list is accessed sequentially. To get to a node, you need to run through every node prior to it within the list. CIRCULAR LINKED LIST •In CLL the last node does not consist NULL pointer •Last node consist address of the first node
  • 23. CLL ADVANTAGES •Any node can be a starting point •We don’t need to maintain two pointers for front and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can always be obtained as next of last •Circular lists are useful in applications to repeatedly go around the list. CLL REPRESENTATION
  • 24. •In CLL take a pointer tail instead of head for easy access struct Node { int data; struct Node *next; }*tail=NULL, *temp; INSERT AT BEGINNING OF CIRCULAR LL ALGORITHM
  • 25. STEP-1 Create a newnode, store value add NULL address newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data=value; newNode->next=NULL; STEP-2 Check whether list is EMPTY or not with condition (tail==NULL) STEP-3 If list is EMPTY, then assign newnode address to tail and tail->next tail=newnode tail->next=newnode STEP-4 If list is NOT EMPTY, then in the newnode->next store tail->next address and make newnode as first node with tail newNode->next=tail->next; tail->next=newNode; STEP-5 Display value is inserted in Circular LL
  • 26. INSERT AT END OF CIRCULAR LL ALGORITHM
  • 27. STEP-1 Create a newnode, store value add NULL address newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data=value; newNode->next=NULL; STEP-2 Check whether list is EMPTY or not with condition (tail==NULL) STEP-3 If list is EMPTY, then assign newnode address to tail and tail->next tail=newnode tail->next=newnode STEP-4 If list is NOT EMPTY, then in the newnode->next store tail->next address and make newnode as last node with tail newNode->next=tail->next; tail->next=newNode; tail=newNode; STEP-5 Display value is inserted in Circular LL
  • 28. INSERT AT SPECIFIC LOCATION OF CIRCULAR LL ALGORITHM These methods are used to put data into a circular linked list at a specified point. •Terminate the function if the circular linked list is empty. •If not, create two node pointers. •Create a new node to store the data. Now, navigate to the desired location and place this new node there. •Connect the pointer of this new node to the node that was previously existing at this location. •To observe the outcome, print the new circular linked list.
  • 29. Deletion of the First Node The instructions below can be used to remove a node from the circular linked list’s beginning: Step 1: Check for Overflow if start = Null then print list is empty Exit End if Step 2: set ptr = start Step 3: set start = start -> next Step 4: print Element deleted is ,ptr -> info Step 5: set last -> next = start Step 6: free ptr Step 7: EXIT
  • 30. Deletion of the Last Node
  • 31. The instructions below can be used to remove a node from the circular linked list’s end: Step 1: IF START = NULL Write UNDERFLOW Go to Step 8 [END OF IF] Step 2: SET PTR = START Step 3: Repeat Steps 4 and 5 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 START is used to initialize the pointer variable PTR, i.e., PTR now points to the linked list’s first node. We use another pointer variable PREPTR in the while loop so that PREPTR always refers to one node before PTR. We set the next pointer of the second last node to START after we reach the last node and the second last node, making it the (new) final node of the linked list.
  • 32. Deletion from Specific Point Deletion from the nth place in a circular linked list is one of the deletion operations that we commonly execute on this type of list. We utilize pointers to traverse the circular linked list and conduct deletion from a specified position. These instructions are used to delete a specified place in a circular linked list: •Initialize the p and q node pointers. •Create a variable called k that will serve as a counter variable. •Set the value of del to pos-1. •Repeat step 5 until k does not equal del. •Make q=p and p=p->next in this loop. •After each successful repetition, increase the value of k by one. •Make the next of q equal to the next of p. •Delete the node referred to by the node pointer p.
  • 33. DOUBLE LINKED LIST •In SLL it is possible to move only in the forward direction •You cannot move back in SLL because every node consist only address of next node •But in DLL we can move in the forward and backward directions •In DLL every node consist 3 three fields. They are i) Data field: stores value ii) Previous pointer field: stores address of previous node iii) Next pointer field: stores address of next node •First node previous field consist NULL value •Last node next field consist NULL value
  • 34. SINGLE LL Vs DOUBLE LL
  • 35. INSERT AT BEGINNING OF DOUBLE LL ALGORITHM
  • 36. STEP-1 Create a newnode, store value in data field and NULL in previous field of newnode newnode -> data = value; newnode -> previous = NULL; STEP-2 If head==NULL, make the newnode as first node with assigning NULL in next field, and make head as newnode newnode -> next = NULL; head = newnode; STEP-3 Else store head address in next field of newnode And make it as first node newnode -> next = head; head->prev=newnode; head = newnode; STEP-4 Display node is inserted
  • 37. INSERT AT END OF DOUBLE LL ALGORITHM
  • 38. STEP-1 Create a newnode, store value in data field and NULL in next field of newnode newNode -> data = value; newNode ->next = NULL; STEP-2 If head==NULL, make the newnode as last node with assigning NULL in previous field, and make head as newnode newNode -> previous = NULL; head = newNode; STEP-3 Else create a temp node, and assign head address in it. Move temp node to the last node. Then store temp address in previous and next fields struct Node *temp = head; while(temp -> next != NULL) temp = temp -> next; temp -> next = newNode; newNode -> previous = temp; STEP-4 Display node is inserted
  • 39. Deletion In a double linked list, the deletion operation can be performed in three ways as follows... •Deleting from Beginning of the list •Deleting from End of the list •Deleting a Specific Node Deleting from Beginning of the list
  • 40. We can use the following steps to delete a node from beginning of the double linked list... •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function . •Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head. •Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next) •Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions) •Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
  • 41. Deleting from End of the list
  • 42. We can use the following steps to delete a node from end of the double linked list... •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function. •Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head. •Step 4 - Check whether list has only one Node (temp → previous and temp → next both are NULL) •Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the function. (Setting Empty list condition) •Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list. (until temp → next is equal to NULL) •Step 7 - Assign NULL to temp → previous → next and delete temp.
  • 43. Deleting a Specific Node from the list
  • 44. We can use the following steps to delete a specific node from the double linked list... •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. •Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head. •Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node. •Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and terminate the function. •Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one node or not •Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete temp (free(temp)). •Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head). •Step 9 - If temp is the first node, then move the head to the next node (head = head → next), set head of previous to NULL (head → previous = NULL) and delete temp.
  • 45. •Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next == NULL). •Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next = NULL) and delete temp (free(temp)). •Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next (temp → previous → next = temp → next), temp of next of previous to temp of previous (temp → next → previous = temp → previous) and delete temp (free(temp)).
  • 46. We can use the following steps to display the elements of a double linked list... •Step 1 - Check whether list is Empty (head == NULL) •Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function. •Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head. •Step 4 - Display 'NULL <--- '. •Step 5 - Keep displaying temp → data with an arrow (<===>) until temp reaches to the last node •Step 6 - Finally, display temp → data with arrow pointing to NULL (temp → data ---> NULL). Displaying a Double Linked List