array4
array4
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
7 5 10 12 9
5
Circular linked list(conti..)
6
Algorithm : Traversal in a circular linked list
The traversal of circular linked list having list pointer variable Begin
and a pointer variable Pointer to traverse the linked list from begin
to end.
7
Traversal in Circular Linked List
Step7: Exit
8
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
10
Insertion at the Beginning(cont…)
Step4: If Begin = Null Then
Set Begin = New
Set New → Next = Begin
Exit
[End If]
11
Insertion at the Beginning (cont…)
Step10: Exit
12
Insertion at the End of Circular Linked List
Begin
7 5 10 12 9
13
New
14
Insertion at the End (continued…)
Step4: If Begin = Null Then
Set Begin = New And New → Next = Begin
Exit
[End If]
Step8: Exit
15
Delete First Node in Circular Linked List
To delete the first node from the circular linked list,Begin will
point to info part of second node and Next part of last node will
point to info part of second node.
Begin
7 5 10 12 9
16
Delete Last Node in Circular Linked List
To delete the last node from the circular linked list the Next part of
Second last node will point to the Info part of first node and last
node will be deleted.
Begin
7 5 10 12 9
17
Applications of Circular Linked List
18
Two-Way Linked List(Doubly Linked List)
In Two-Way Linked List, we traverse the list in both the directions
• Forward direction (from beginning to end)
• Backward direction (from end to beginning).
The Two-Way Linked List is also known as Doubly Linked List. In
Two-Way Linked List, each node is divided into three parts: Pre,
Info, Next. The structure of a node used in Two-Way Linked List is
as shown below:
20
Two-Way Linked List (conti…)
Here in Two-Way Linked List, two list Pointer variables i.e. Begin and
End are used which contains the address of the first node and last
node of the Linked List respectively . Two-Way Linked List can be
shown diagrammatically as:
Begin End
Null 10 12 8 7 Null
•Traversing
•Searching
•Insertion
•Deletion
22
Traversing a Two-Way Linked List
A Two-Way Linked List can be traversed in both the
directions:
24
Searching in a Two-Way Linked List
•Traverse the list either from the end or from beginning of the
linked list.
25
Algorithm: Searching in Double Linked List
To find the position of a given element ‘data’ in a Two-Way
Linked List by traversing it from end to beginning.
26
Searching in Double Linked List
Step5: Exit
27
Insertion of an element in Two Way Linked List
Insertion can take place at various positions in a linked list
such as:
28
Insertion of node at Beginning of doubly linked list
Null 10 12 8 7 Null
Null Data
New
29
Algorithm: Insertion of a New node at Beginning
30
Insertion of a New node at Beginning
Step6: Exit
31
Insertion of New node after particular Data
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:
End
Begin
Null 10 12 8 7 Null
Successor=Pointer → Next
New → Next=Successor
Item
Pointer → Next=New
New → Pre=Pointer
Successor →Pre=New New
Insert a New node ‘Item’ after a given element ‘Data’ in the Two-
Way Linked List
33
Insertion after particular node(conti..)
34
Insertion after particular node(conti..)
35
Insertion after particular node(conti..)
Else
New → Next = Null
New → Pre = Pointer
Pointer → Next = New
End = New
[End If]
Step8: Exit
36
Deleting a node with given Item
Null 5 8 7 9 Null
37
Deleting 1st node with given Item
Begin
End
Successor
Null 5 Null 8 7 9 Null
Begin=Begin → Next
Begin → Pre=Null
Null 5 8 7 9 Null
Deleting a Node present between the first and the last node of a Two-
Way Linked List.
39
Deleting last node with given item
CASE3: Now we will delete an element 9 which is encountered
in the node which is last node of the list as shown:
End
Begin
Previous → Next=Null
End=Previous
40
Algorithm: Deleting a node with given Item(conti..)
41
Deleting a node with given Item(continue..)
//Deallocate memory held by Pos
Pos → Next = Free, Free = Pos
Exit
[End If]
42
Deleting a node with given Item (continue..)
Step5: If Pointer → Next = Null And Pointer → Info ≠ Item
Print: “ Item to be deleted not found”
Exit
[End If]
Step 8: Exit
44