Unit 3 Notes
Unit 3 Notes
Abstract Data Types (ADTs) – List ADT – Array-Based Implementation – Linked List
– Doubly-Linked Lists – Circular Linked List – Stack ADT – Implementation of Stack –
Applications – Queue ADT – Priority Queues – Queue Implementation – Applications.
Illustration of an Array
As per the above illustration of array, there are some of the following important points -
Index starts with 0.
The array's length is 10, which means we can store 10 elements.
Each element in the array can be accessed via its index.
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)
[END OF LOOP]
[END OF LOOP]
Step 8: SET NEW_NODE -> NEXT = HEAD
Step 9: SET TEMP → NEXT = NEW_NODE
Step 10: SET HEAD = NEW_NODE
Step 11: EXIT
Insertion into a Circular Linked List at the beginning
3.6.2.5 Searching
Searching in circular singly linked list needs traversing across the list.
The item which is to be searched in the list is matched with each node data of
the list once.
If the match found then the location of that item is returned otherwise -1 is
returned.
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
Traversing in circular singly linked list can be done through a loop.
Initialize the temporary pointer variable temp to head pointer and run the
while loop until the next pointer of temp becomes head.
Algorithm 3.14
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR → NEXT != HEAD
STEP 5: PRINT PTR → DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]
STEP 7: PRINT PTR→ DATA
STEP 8: EXIT
3.7 STACK ADT
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out)
principle.
A Stack is an abstract data type with a pre-defined capacity, which means that it
can store the elements of a limited size.
Stack has one end, whereas the Queue has two ends (front and rear).
It contains only one pointer top pointer pointing to the topmost element of the
stack.
}
}
3.9.2 Backtracking
Backtracking is used in algorithms in which there are steps along some path
(state) from some starting point to some goal. It uses recursive calling to find the
solution by building a solution step by step increasing values with time. It
removes the solutions that doesn't give rise to the solution of the problem based
on the constraints given to solve the problem.
Let’s see how Stack is used in Backtracking in the N-Queens Problem
For the N-Queens problem, one way we can do this is given by the following:
For each row, place a queen in the first valid position (column), and then
move to the next row
If one can successfully place a queen in the last row, then a solution is found.
Now backtrack to find the next solution
Two examples of this are shown below:
Starting with a queen in the first row, first column (represented by a stack
containing just "0"), we search left to right for a valid position to place another
queen in the next available row.
If we find a valid position in this row, we push this position (i.e., the column
number) to the stack and start again on the next row.
Putting all this into pseudo-code form, we have the following algorithm.
Create empty stack and set current position to 0
Repeat {
loop from current position to the last position until valid position found //current
row
if there is a valid position {
push the position to stack, set current position to 0 // move to next row
}
if there is no valid position {
if stack is empty, break // stop search
else pop stack, set current position to next position // backtrack to previous
row
}
if stack has size N { // a solution is found
pop stack, set current position to next position // backtrack to find next solution
}
}
Types of Queue
Simple Queue or Linear Queue
Circular Queue
Priority Queue
Double Ended Queue (or Deque)