Abstract Data Types
Abstract Data Types
3.3.6 C
3.3.7
3.3.8 omplexity of Array operations
Time and space complexity of various array operations are described below.
Time Complexity
Operation Average Case Worst Case
Access O(1) O(1)
Search O(n) O(n)
Insertion O(n) O(n)
Deletion O(n) O(n)
Space Complexity
In array, space complexity for worst case is O(n).
2. Space Complexity
Operation Space complexity
Insertion O(n)
Deletion O(n)
Search O(n)
A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in Fig 3.6
Deletion at beginning
Deletion in douly linked list at the beginning is the simplest operation.
Just need to co the head pointer to pointer ptr and shift the head pointer to its next.
Make the prev of this new head node point to NULL.
Now free the pointer ptr by using the free function.
Algorithm 3.4
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
Fig. 3.13: Deletion of a specified node in Doubly Linked List at the end
a
entire list and make the head pointer free.
a
Scenario 3(the list contains more than one element) - If the list contains more
than one element, then in order to delete the last element, reach the last node.
in iy
Also keep track of the second last node of the list. For this purpose, the two
pointers ptr and preptr are defined.
n . or
Make just one more pointer adjustment. We need to make the next pointer of
.p
preptr point to the next of ptr (i.e. head) and then make pointer ptr free.
Algorithm 3.12
w
Step 1: IF HEAD = NULL
w
Write UNDERFLOW
w
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = HEAD
Step 7: FREE PTR
Step 8: EXIT
a a
in iy
Fig. 3.19: Deletion in a Circular Linked List at the end
n . or
3.6.2.5 Searching
Searching in circular singly linked list needs traversing across the list.
.p
The item which is to be searched in the list is matched with each node data of the
w
list once.
If the match found then the location of that item is returned otherwise -1 is
w
returned.
w
Algorithm 3.13
STEP 1: SET PTR = HEAD
STEP 2: Set I = 0
STEP 3: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 4: IF HEAD → DATA = ITEM
WRITE i+1 RETURN [END OF IF]
STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head
STEP 6: if ptr → data = item
write i+1
RETURN
End of IF
STEP 7: I = I + 1
STEP 8: PTR = PTR → NEXT
[END OF LOOP]
STEP 9: EXIT
3.6.2.5 Searching
a
Traversing in circular singly linked list can be done through a loop.
a
Initialize the temporary pointer variable temp to head pointer and run the while
in iy
loop until the next pointer of temp becomes head.
n . or
Algorithm 3.14
STEP 1: SET PTR = HEAD .p
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
w
GOTO STEP 8
w
END OF IF
w