array 2
array 2
A Simplified Approach
to
Data Structures
Prof.(Dr.)Vishal Goyal, Professor, Punjabi University Patiala
Dr. Lalit Goyal, Associate Professor, DAV College, Jalandhar
Mr. Pawan Kumar, Assistant Professor, DAV College, Bhatinda
Begin
Begin
7 5 10 12 9
Step7: Exit
3.4.2.Insertion at the Beginning of Circular
Linked List
In this case, the New node is inserted as the first node and the
next part of the last node is changed and now it points to the
newly inserted node as shown below in figure:
Begin
7 5 10 12 9
New
Step11: Exit
3.4.3. Insertion at the End of the Circular
Linked List
In the process of inserting an element at the end of the circular linked list,
the address stored in the Next part of the last node and next part of New node
need to be changed as shown below:
Begin
7 5 10 12 9
9
New
Step8: Exit
3.4.4. Applications of Circular Linked List
↑ ↑ ↑
Pre Info Next
Structure of a Node used in a Two-Way Linked List
Null 10 12 8 7 Null
↑ ↑ ↑
Pre Info Next
A Two-Way Linked List
The Pre part of the first node of a Two-Way Linked List will contain Null
as there is no node preceding the first node and the Next part of last node will
contain Null as there is no node following the last node.
3.5 Two-Way Linked List (Doubly Linked List)
(continued)
•Traversing
•Searching
•Insertion
•Deletion
3.5.1.Traversing a Two-Way Linked List
Step5: Exit
3.5.3. Insertion of an element in a Two-
Way Linked List
•at beginning
Null 10 12 8 7 Null
Null Data
New
Insertion of a node at the Beginning in a Doubly linked List
3.5.3.1. Inserting a New node at the Beginning
of a Two-Way Linked List(continued)
Algorithm: To insert a New node at the Beginning of
a Two-Way Linked List
Step6: Exit
3.5.3.2. Inserting a New node after a particular
node in a Two-Way Linked List
Insertion after a particular node requires finding the location of the
node after which new node is to be inserted. After finding the desired
node, the New node can be inserted easily by changing few pointers
as shown:
Begin End
Null 10 12 8 7 Null
Successor=Pointer → Next
New → Next=Successor
Pointer → Next=New
Item
New → Pre=Pointer
Successor →Pre=New New
Else
New → Next=Null
New → Pre=Pointer
Pointer → Next=New
End=New
[End If]
Step8: Exit
3.5.4. Deleting a node with given Item from
2-Way linked List
For deleting a particular node:
•if the desired node is found, it can be removed from the linked
list by changing few pointers as shown:
Begin End
Null 5 8 7 9 Null
Null 5 8 7 9 Null
Successor
Begin=Begin → Next
Begin → Pre=Null
Deleting the 1st node of a Two-Way linked List
3.5.4. Deleting a node with given Item from
2-Way linked List(continued)
CASE2: Suppose we want to delete an element 7, which is
contained in a node that lies between the first and last node of
the list then deletion will be performed as shown in the figure
below:
Begin End
Null 5 8 7 9 Null
Previous Pointer Successor
Previous →
Next=Successor
Successor→
Pre=Previous
Deleting a Node present between the first and the last node of a
Two-Way Linked List
3.5.4. Deleting a node with given Item from
2-Way linked List(continued)
Null 5 8 7 9 Null
Previous →
Next=Null
End=Previous
Deleting the last node from a Two-Way Linked List
3.5.4. Deleting a node with given Item from
2-Way linked List(continued)
Algorithm: Delete a node containing an element ‘Item’ from
a Two-Way Linked List