Data Structures Lab: List of Exercises
Data Structures Lab: List of Exercises
List of Exercises
1. Stack operations using arrays
3. Operations on SLL
5. Operations on DLL
7. Operations on BST
9. Operations on B-tree
1. Push:
Algorithm PUSH(S[0…N-1], TOP, X)
If TOP>=N-1 Then
Write(„STACK OVERFLOW‟)
Return
End If
TOP = TOP + 1
S[TOP] = X
Return
End PUSH
2. Pop
3. Peek
4. Display
Algorithm DISPLAY(S[0…N-1], TOP)
If TOP = -1 Then
Write(„STACK IS EMPTY‟)
Return
End If
I = TOP
Loop (I >= 0)
Write S[I]
I=I-1
End loop
Return
End DISPLAY
1. Enqueue
2. Dequeue
Function DEQUEUE(Q[0…N-1],F,R)
If F=-1 Then
Write(„UNDERFLOW‟)
Return(0);
Y = Q[F]
If F = R Then
F = R = -1
Else
F=F+1
Return(Y)
End DEQUEUE
3. Search
4. Display
Algorithm DISPLAY(Q[0…N-1], F,R)
If F= -1 Then
Write(„QUEUE IS EMPTY‟)
Return
End If
I=F
Loop (I <= R)
Write Q[I]
I=I+1
End loop
Return
End DISPLAY
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
newNode->link = sList
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
newNode->link = null
sList = newNode
Else
temp = sList
loop(temp->link is not null)
temp = temp->link
End loop
allocate(newNode)
newNode->data = dataIn
newNode->link = null
temp->link = newNode
End if
End insEnd
4. Delete Node
Algorithm delNode(ref sList, val dataOut)
If sList->link = null AND sList->data = dataOut
sList = null
end if
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
sList = temp->link
else
P->link = temp->link
End if
Release(temp)
Return
Else
P = temp
Temp = temp->link
End if
End loop
Write(„Data not found‟)
Return
End delNode
5. Retrieve Data
Function getData(ref sList, val Loc)
Temp = sList
Cnt = 1
Loop(temp not null)
If Cnt = Loc then
Return(temp->data)
End if
Temp = temp->link
Cnt = Cnt + 1
End loop
Write(„Invalid Location‟)
Return(0)
End getData
6. Count Node
Function cntNode(ref sList)
Temp = sList
Cnt = 0
Loop(temp not null)
Temp = temp->link
Cnt = Cnt + 1
End loop
Return(Cnt)
End getData
Algorithm:
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
newNode->pLink = null
newNode->sLink = sList
sList->pLink = newNode
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
newNode->pLink = null
newNode->sLink = null
sList = newNode
Else
temp = sList
loop(temp->sLink is not null)
temp = temp->sLink
end loop
allocate(newNode)
newNode->data = dataIn
newNode->sLink = null
newNode->pLink = temp
temp->sLink = newNode
End if
End insEnd
4. Delete Node
Algorithm delNode(ref sList, val dataOut)
If sList->sLink = null AND sList->data = dataOut
sList = null
end if
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
sList = temp->sLink
sList->pLink = null
else
if temp->sLink = null then
temp->pLink->sLink = null
else
temp->pLink->sLink = temp->sLink
temp->sLink->plink = temp->pLink
end if
end if
release(temp)
return
end if
temp = temp->sLink
End loop
Write(„Data not found‟)
Return
End delNode
5. Retrieve Data
Function getData(ref sList, val Loc)
Temp = sList
Cnt = 1
Loop(temp not null)
If Cnt = Loc then
Return(temp->data)
End if
Temp = temp->sLink
Cnt = Cnt + 1
End loop
Write(„Invalid Location‟)
Return(0)
End getData
6. Count Node
Function cntNode(ref sList)
Temp = sList
Cnt = 0
Loop(temp not null)
Temp = temp->sLink
Cnt = Cnt + 1
End loop
Return(Cnt)
End getData
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
newNode->pLink = sList->pLink
newNode->sLink = sList
sList->pLink->sLink = newNode
sList->pLink = newNode
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
newNode->pLink = newNode
newNode->sLink = newNode
sList = newNode
else
temp = sList->pLink
allocate(newNode)
newNode->data = dataIn
newNode->pLink = temp
newNode->sLink = sList
sList->pLink = newNode
temp->sLink = newNode
end if
End insEnd
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
sList = temp->sLink
sList->pLink = temp->pLink
temp->pLink->sLink = sList
else
temp->pLink->sLink = temp->sLink
temp->sLink->pLink = temp->pLink
end if
release(temp)
return
end if
temp = temp->sLink
if temp=sList then
break loop
end if
End loop
Write(„Data not found‟)
Return
End delNode
5. Retrieve Data
6. Count Node
1. Insertion
Algorithm insertBST(ref Root, val newNode)
If Root is null
Root = newNode
Else
pWalk = Root
loop(pWalk not null)
parent = pWalk
If newNode->key < pWalk->key then
pWalk = pWalk->left
Else
pWalk = pWalk->right
End if
End loop
If newNode->key < parent->key)
parent->left = newNode
Else
parent->right = newNode
End if
End if
Return
End insertBST
2. Deletion
Algorithm deleteBST(ref Root, val dltKey)
If Root = null then
Return false
If dltKey < Root->data.key then
Return deleteBST(Root->left, dltKey)
Else If dltKey > Root->data.key then
Return deleteBST(Root->right,dltKey)
Else
If Root->left = null then
dltPtr = Root
Root = Root->right
Release(dltPtr)
Return true
Else If Root->right = null then
dltPtr = Root
Root = Root->left
Release(dltPtr)
Return true
Else
DltPtr = Root->left
loop(dltPtr->right not null)
DltPtr = dltPtr->right
End loop
Root->data = dltPtr->data
Return deleteBST(Root->left, dltPtr->data.key)
End if
End if
End deleteBST
3. Traversal
Algorithm preOrder (val Root)
If Root=null then
Process(Root)
preOrder(Root->leftSubtree)
preOrder(Root->rightSubtree)
End if
Return
End preOrder
Algorithm inOrder (val Root)
If Root=null then
inOrder(Root->leftSubtree)
Process(Root)
inOrder(Root->rightSubtree)
End if
Return
End inOrder
Algorithm CONVERT
1. HEAD = newNode
HEADleftSubTree = null
HEADrightSunTree = HEAD
TOP=0
PUSH(S[1..N],TOP,LEV,LOC)
2. Loop thru step 7 while there exists input
3. Read LEVEL and NAME
4. Allocate(newNode)
newNodeleftSubTree = newNoderightSubTree = null
newNodedata = NAME
5. PRED = PEEK(S[1..N], TOP)
6. If LEVEL > PRED_LEVEL Then
PRED_LOCleftSubTree = newNode
Else
Loop (PRED_LEVEL > LEVEL)
POP(S[1..N],TOP)
PRED = PEEK(S[1..N], TOP)
End loop
If PRED_LEVEL < LEVEL Then
Write („Error in input‟. Mixed Level Numbers‟)
Return
End if
PRED_LOCrightSubTree = newNode
POP(S[1..N], TOP)
7. PUSH(S[1..N], TOP, LEVEL, newNode)
8. Return
B C D
K H I J E F
G
The input for the above general tree should be given as follows:
1, A
2, B
3, K
2, C
3, H
3, I
3, J
2, D
3, E
3, F
4, G
K C
H D
I E
J F
G
Ex. No: 09 B-Tree Operations
Algorithm BTreeInsert(tree,data)
1. if(tree = null)
2. create new node
3. set left subtree of node to null
4. move data to first entry in new node
5. set subtree of first entry to null
6. set tree root to address of new node
7. set number of entries to 1
8. end if
9. insertNode(tree, data, upEntry)
10. if(tree higher)
11. create new node
12. move upEntry to first entry in new node
13. set left subtree of node to tree
14. set tree root to new node
15. set number of entries to 1
16. end if
17. Return
Algorithm BTreeDelete(tree,dltKey)
1. if(tree empty)
2. return false
3. end if
4. delete(tree, dltKey,success)
5. if(success)
6. if(tree number of entries zero)
7. set tree to left subtree
8. end if
9. end if
10. return success
Ex. No: 10 Trie Structure Operations
Structure of node:
1. Insertion
1. Temp = Head
2. I = 1
3. N = Length(Word)
4. Loop (I <=N)
5. idx = Index corresponding to Word[I]
6. If Temp->Link [idx] not Null Then
7. Temp = TempLink[idx]
8. Else
9. Allocate(newNode)
10. newNodeData = “-“
11. Initialize all Link fields of newNode to Null
12. TempLink[idx] = newNode
13. Temp = newNode
14. EndIf
15. I=I+1
16. End Loop
17. TempData = Word
18. Return
2. Deletion
1. Prev = Null
2. Temp = Head
3. I = 1
4. N = Length(Word)
5. Loop (I <=N)
6. idx = Index corresponding to Word[I]
7. If Temp->Link [idx] not Null Then
8. Prev = Temp
9. Temp = TempLink[idx]
10. Else
11. Write “Word is not available in the dictionary”
12. Return
13. EndIf
14. I=I+1
15. End Loop
16. If Temp not Null then
17. TempData = “-“
18. If all Link fields of Temp are Null Then
19. PrevLink[idx] = Null
20. EndIf
21. Else
22. Write “Word is not available in the dictionary”
23. Return
24. EndIf
25. Return
3. Searching
1. Temp = Head
2. I = 1
3. N = Length(Word)
4. Loop (I <=N)
5. idx = Index corresponding to Word[I]
6. If Temp->Link [idx] not Null Then
7. Prev = Temp
8. Temp = TempLink[idx]
9. Else
10. Write “Word is not available in the dictionary”
11. Return
12. EndIf
13. I=I+1
14. End Loop
15. If TempData is not “-“
16. Write “Word is present in the dictionary”
17. Return Word
18. Else
19. Write “Word is not available in the dictionary”
20. EndIf
21. Return
4. Display
Algorithm Display(Node)
1. Heap Sort
Procedure Heapify(ref K, val N)
Heapsize[K] = N
I=N/2
Loop(I >= 1)
Call Adjust(K,I)
I=I-1
End loop
Return
End Heapify
2. Merge Sort
Procedure MergeSort(ref K, val low, val high)
If low<high then
Mid = (low+high) / 2
Call MergeSort(K, low, mid)
Call MergeSort(K, mid+1, high)
Call Merge(K, low, mid, high)
End if
Return
End MergeSort
1. Linear Search
Function LinearSearch(val K, val N, val X)
I=1
Loop(I<=N)
If K[I]=X then
Return(I)
End if
I = I+1
End loop
Return(0)
End LinearSearch
2. Binary Search
3. Hash Search
Division method for h( k )
h(k) = k mod m
Example:
If m = 20 and k = 91
h(91) = 91 mod 20 = 11
1. h = key mod m
2. Allocate(newNode)
3. newNodeData = key
4. newNodeLink = Table[h]
5. Table[h] = newNode
6. Return
1. h = key mod m
2. If Table[h] is Null
3. Write “Element is not present in the Table”
4. Return
5. Else
6. Temp = Table[h]
7. loc = 1
8. Loop Temp is not Null
9. If TempData = key Then
10. Write “Element is present at position”, loc, “in list”, h
11. Return (loc)
12. Else
13. loc = loc + 1
14. Temp = TempLink
15. EndIf
16. End Loop
17. Write “Element is not present in the Table”
18. Return (0)
Algorithm Display(Table)
1. h = 0
2. Loop h <=size(Table)
3. Temp = Table[h]
4. Loop Temp is not Null
5. Write TempData
6. Temp = TempLink
7. End Loop
8. h = h+1
9. End Loop
10. Return
Algorithm breadthFirst(graph)
1. If(empty graph)
2. return
3. End if
4. createQueue(queue)
5. loop(through all vertices)
6. set vertex to not processed
7. end loop
8. loop(through all vertices)
9. if(vertex not processed)
10. if(vertex not in queue)
11. enqueue(queue,walkPtr)
12. set vertex to enqueued
13. end if
14. loop(not emptyQueue(queue))
15. set vertex to dequeue(queue)
16. process(vertex)
17. set vertex to processed
18. loop(through adjacency list)
19. if (destination not enqueued or processed)
20. enqueue(queue,destination)
21. set destination to enqueued
22. end if
23. get next destination
24. end loop
25. end loop
26. end if
27. get next vertex
28. end loop
29. destroyQueue(queue)
30. Return
Depth-First Traversal
Algorithm depthFirst(graph)
1. if(empty graph)
2. return
3. EndIf
4. set walkPtr to graph first
5. loop (through all vertices)
6. set processed to 0
7. End loop
8. createStack(stack)
9. Loop(through vertex list)
10. If(vertex not processed and not in stack)
11. pushStack(stack,walkPtr)
12. set walkPtr processed to 1
13. End if
14. Loop(not emptyStack(stack))
15. set vertex to popStack(stack)
16. process(vertex)
17. set vertex to processed
18. Loop(through arc list)
19. If(arc destination not in stack)
20. pushStack(stack,destination)
21. set destination to in stack
22. End if
23. get next destination
24. End loop
25. End loop
26. get next vertex
27. End loop
28. destroyStack(stack)
29. Return
Represent the graph as adjacency list or adjacency matrix. w(u,v) represents the weight
of the edge (u,v)
Algorithm Prim(G, w, r)
Additional Exercises
Operations on circular Queue.
Evaluation of postfix expression using stack.
Operations on circular singly linked list.
Ternary Search
MST using Kruskal‟s algorithm
Operations on AVL Tree.