Data Structure Algorithms
Data Structure Algorithms
1. To Travels an
Array: -
LA = Linear Array.
LB = Lower Bound.
UB = Upper Bound.
Operation = Process.
Step 1 [Initialize Counter]
Set k = LB
Step 2 Repeat Step 3 & 4 while k UB
Step 3 (Visit Element) Apply Process to LA[k]
Step 4 [Increase Counter] Set k++ or k = k+1
[End of Step 2 Loop]
Step 5 Exit
2. To Insert
Value in Array: -
Insert (LA, N, K, ITEM) K N.
Step 1 [Initialize Counter]
Set J = N.
Step 2 Repeat Step 3 & 4 while j k
Step 3 (Moved Jth Element downward)
Set LA[J+1] = LA[j]
Step 4 [Decrease Counter] set J = J1
[End of Loop]
Step 5 [Insert Element]
LA[K] = ITEM
Step 6 [Increment N]
Set N = N+1
Step 7 Exit
3. To Delete
Value in Array: -
Delete (LA, N, K, Item)
Step 1 Set ITEM = LA[k]
Step 2 Repeat for J = K to N1
(Move J+1st Element upward)
Set LA[J] = LA[J+1]
End of Loop
Step 3 [Reset the Number N of Element LA]
Set N = N1
Step 4 Exit
5. Insertion
Sort: -
Insertion sort (A, n)
Step 1 Set A[0] = -
Step 2 Repeat Steps 3 to 5
For = 2,3N
Step 3 Set TEMP = A[K] & PTR = K1
Step 4 Repeat while TEMP < A[PTR]
a) Set A[PTR+1] = A[PTR]
(Moves Element Forward)
b) Set PTR = PTR1
[End of Loop]
Step 5 Set A[PTR+1] = TEMP
[Insert Element at Proper Place]
[End of Step 2 Loop]
Step 6 Return
6. Linear
Search: -
Linear Search (k, n, x)
Step 1 [Initialize Search]
I=1
K[n+1] = x
Step 2 [Search the Vector]
Repeat while k[t] x
I = I+1
Step 3 [Successful Search]
If I = n+1 then
Write (Unsuccessful Search)
Return 0
Else
Write (Successful Search)
Return I
Step 4 exit
7. Binary
Search: -
Binary Search (k, n, x)
Step 1 [Initialize] low = 1, high = n
Step 2 [Perform Search]
Repeat thru: step 4 while low high
Step 3 [Obtain Index of Midpoint of Interval]
Middle = [(low + high)/2]
Step 4 [Compare]
If x < k[middle]
High = middle-1
Else if x > k[middle]
Low = middle+1
Else
Write (Successful Search) return middle
Step 5 [Unsuccessful Search]
Write (Unsuccessful Search)
Return 0
8. PUSH: -
PUSH (s, top, x)
Step 1 [Check for Stack Overflow]
If top n then
Write (Stack Overflow)
Return
Step 2 [Increment top]
Top = top+1
Step 3 [Insert Element]
S[top] = x
Step 4 return
9. POP: -
POP (s, top)
Step 1 [Check for Underflow on Stack]
If top = 0 then
Write (Stack Underflow on POP)
Return
Step 2 [Decrement Pointer]
Top = top-1
Step 3 [Return top Element of Stack]
Return (s[top+1])
10. PEEP: -
PEEP (s, top, I)
Step 1 [Check for Stack Underflow]
If top I + 1 0 then
Write (Stack Underflow on PEEP)
Return
Step 2 [Return Ith value from top of stack]
Return (s[top I + 1])
11. CHANGE: -
CHANGE (s, top, x, I)
Step 1 [Check for Stack Underflow]
If top I + 1 0 then
Write (Stack Underflow on PEEP)
Return
Step 2 [Change Ith element from top of Stack]
S[top I + 1] = x
Step 3 return
12. Convert
Infix Expression into Suffix Expression: -
S = Stack
Q = Array Containing Expression
Step 1 Initialize an Operator Stack S
Step 2 PUSH ( onto stack S & add ) to the end of the expression Q
Step 3 Read the next symbol from Q
If symbol is
An operand: write the operand
(: PUSH ( on stack S
): POP and write all operator from stack S until
encountering ( , then POP (
^: POP and write all ^ operator from top and write them.
PUSH new symbol ^ to stack
* or /: POP and write all *, /, ^ operator from top and
write them. PUSH new symbol * or /
+ or -: POP and write all operator from the top until ( is
encountering. PUSH new symbol + or -
Step 4 at the end of the expression Q POP and write all operator
Step 5 Exit
13. Evaluation of
Suffix Expression: -
P = Postfix Expression
Step 1 Add a right parenthesis ) at the end of P
Step 2 Scan P from left to right and repeat step 3 & 4 for each
element of P until ) is encounter
Step 3 if operand is encountered PUSHES it in stack
Step 4 if operator is encountered then
a) Remove the two top elements of stack where A is the
top element and B is the next to top element
b) Evaluate B A
c) Place the result of b) back on Stack
[End of if Structure]
[End of Step 2 Loop]
Step 5 Set value equal to the top element on stack
Step 6 Exit
14. Convert
Infix Expression into Prefix Expression: -
P = Stack Which Contain Operators
S = Stack Which Contain Find Expression
Step 1Intialize Stack P and Stack S
Step 2 Start to scan expression from right to left. Read the next
symbol. In case symbol is
An Operand: PUSH it into Stack S
): PUSH into Stack P
(: POP all Operators from Stack P and PUSH into Stack S until
right parenthesis ) from P
Operators +, *, -, /, ^: PUSH operators into stack P
Step 3 at end POP and write all the symbols from S
Step 4 Exit
16. Simple
Queue Insert: -
SQInsert (Q, F, R, N, Y)
Step 1 [Overflow]
If R N-1 then
Write (Overflow)
Return
Step 2 [Increment Rear Pointer]
R = R+1
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Front Pointer Properly Set?]
If F = -1 then
F=0
Return
Step 5 Exit
17. Simple
Queue Delete: -
SQDelete (Q, E, R)
Step 1 [Underflow]
If F = -1 then
Write (Underflow)
Return 0
Step 2 [Delete Element]
Y = Q[F]
Step 3 [Queue Empty]
If F = R then
F = K = -1
Else
F = F+1
Step 4[Return Element]
Return y
18. Circular
Queue Insert: -
CQInsert (F, R, Q, N, Y)
Step 1 [Reset Rear Pointer]
If R = N-1 then
R=0
Else
R = R+1
Step 2 [Overflow]
If F = R then
Write (Overflow)
Return
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Front Pointer Properly Set]
If F = -1 then
F=0
Return
Step 5
19. Circular
Queue Delete: -
CQDelete (F, R, Q, N, Y)
Step 1 [Underflow]
If F = -1 then
Write (Underflow)
Return 0
Step 2 [Delete Element]
Y = Q[F]
Step 3 [Queue Empty]
If F = R then
F = R = -1
Return y
Step 4 [Increment Front Pointer]
If F = N-1 then
F = -1
Else
F = F+1
Return y
Step 5 Exit
20. Double
Ended Queue Insert: -
DEQInsert (L, R, Q, N, Y)
Step 1 [Reset Right Pointer]
If R = N-1 then
R=0
Else
R = R+1
Step 2 [Overflow]
If L = R then
Write (Overflow)
Return
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Left Pointer Properly Set?]
If L = -1 then
L=0
Return
Step 5
21. Double
Ended Queue Delete: -
DEQDelete (L, R, Q, N, Y)
Step 1 [Underflow]
If L = -1 then
Write (Underflow)
Return
Step 2 [Delete Element]
Y = Q[L]
Step 3 [Queue Empty]
If L = R then
L = R = -1
Return y
Step 4 [Increment Left Pointer]
If L = N-1 then
L = -1
Else
L = L+1
Return y
22. Priority
Queue Insert: -
PQInsert (M, Item)
Step 1 Find the Row M
Step2 [Reset the Rear Pointer]
If Rear[M] = N-1 then
Rear[M] = 0
Else
Rear[M] = Rear[M]+1
Step 3 [Overflow]
If Front[M] = Rear[M] then
Write (This Priority Queue is full)
Return
Step 4 0Insert Element]
Q[M] [Rear[M]] = then
Step 5 [Is Front Pointer Properly Set]
If Front[M] = -1 then
Front[m] = 0
Return
Step 6 Exit
23. Priority
Queue Delete: -
PQDelete (K, Item)
Step 1 Initialize K = 0
Step 2 while (Front[K] = -1)
K = K+1
[To find the first non empty queue]
Step 3 [Delete Element]
Item = Q[K] [Front[K]
Step 4 [Queue Empty]
If Front[K] = N-1 then
Front[K] = 0
Else
Front[K] = Front[K]+1
Return Item
Step 6 Exit
25. Simple
Merge: -
SIMPLE_MERGE (K, FIRST, SECOND, THIRD)
TEMP = Temporary Table.
I = Cursor associated with first table.
J = Cursor associated with second table.
L = Index table associated with vector temp.
Step 1 [Initialize]
I = FIRST, J = SECOND, L = 0
Step 2 [Compare corresponding element & output the smallest]
Repeat while I < SECOND & J THIRD
If K[I] K[J] then
L = L+1
TEMP[L] = K[I]
I = I+1
Else
L = L+1
TEMP[L] = K[J]
J = J+1
Step 3 [Copy the remaining unprocessed elements in output area]
If I SECOND
Repeat while J THIRD
L = L+1, TEMP[L] = K[J]
J = J+1
Else
Repeat while I < SECOND
L = L+1, TEMP[L] = K[I]
I = I+1
Step 4 [Copy elements from temporary vector into original area]
Repeat for I = 1, 2,, L
K[FIRST-1+I] = TEMP[I]
Step 5 [Finished]
Return
27. Creation: -
Step 1 Allocate Memory to Temp
Step 2 if(First==NULL)
First = Temp
Curr = Temp
Else
Curr->Next = Temp
Curr = Curr->Next
Curr = Temp
Step 3 Curr->Next = NULL
Step 4 end
40. Preorder
[Root Left Right]: -
Step 1 [initialize]
If T = NULL then
Write (Empty tree)
Return
Else
Top = 0
Call PUSH(S, Top, T)
Step 2 [process each sorted branch address]
Repeat step 3 while Top > 0
Step 3 [get stored address and branch left]
P = POP(S, Top)
Repeat while P NULL
Write P->data
If P->RPTR NULL then
Call PUSH(S, Top, P->RPTR)
P=P->LPTR(branch_left)
Step 4 (Finished) return
41. Preorder
Recursively: -
Step 1 [process root node]
If T NULL then
Write T->data
Else
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR != NULL then
42. Rinorder
[Left Root Right]: -
Step 1 [process root node]
If T = NULL then
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR NULL then
Call Rinorder(T->LPTR)
Step 3 [process root node]
Write T->data
Step 4 [process the right sub tree]
If T->RPTR NULL then
Call Rinorder(T->RPTR)
Step 5 [finished] return
43. Recursive
Post Order [Left Right Root]: -
Step 1 [check the empty tree]
If T=NULL then
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR NULL then
Call Rpostorder(T->LPTR)
Step 3 [process the right sub tree]
If T->RPTR NULL then
Call Rpostorder(T->PTR)
Step 4 [process root node]
Write T->data
Step 5 [finished] return
49. Tinorder: -
Step 1 [initialize]
P=HEAD
Step 2 [traverse thread successor in inorder]
Repeat while TRUE
P=INS(P)
If P=HEAD then
Exit
Else
Write (P->data)