Sei sulla pagina 1di 27

DATA STRUCTURES LAB

BICCIC307R02 / BITCIT307R02 / BCSCCS307R02

List of Exercises
1. 2. 3. 4. 5. 6. 7. 8. 9. Stack operations using arrays Queue operations using arrays Operations on SLL Infix to Postfix conversion Operations on DLL and Circular DLL Operations on BST Operations on Multi-way tree Operations on B-tree Operations on Trie Structure

10. Heap and Merge Sort 11. Searching Techniques 12. BFS and DFS 13. Finding Minimum Weight Spanning Tree

Ex. No: 01
1. Push:

Stack Operations

Algorithm PUSH(S[1N], TOP, X) If TOP>=N Then Write(STACK OVERFLOW) Return End If TOP = TOP + 1 S[TOP] = X Return End PUSH

2. Pop
Function POP(S[1N], TOP) If TOP = 0 Then Write(STACK UNDERFLOW ON POP) Return End If X = S[TOP] TOP = TOP-1 Return(X) End POP

3. Peep
Function PEEP(S[1N], TOP, I) If TOP-I+1 <= 0 Then Write (STACK UNDERFLOW ON PEEP) Return End If Return(S[TOP-I+1]) End PEEP

Pre Lab:

Implementation knowledge on Arrays and Functions.

Ex. No: 02
1. Enqueue

Queue Operations

Procedure ENQUEUE(Q[1N], F,R,N,Y) If R>=N Then Write(OVERFLOW) Return R=R+1 Q[R] = Y If F = 0 Then F=1 Return End ENQUEUE

2. Dequeue
Function DEQUEUE(Q[1N],F,R) If F=0 Then Write(UNDERFLOW) Return(0); Y = Q[F] If F = R Then F=R=0 Else F=F+1 Return(Y) End DEQUEUE

Pre Lab:

Implementation knowledge on Arrays and Functions.

Ex. No: 03

SLL Operations

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

3. Insert after Specified Location


Alogrithm insAfter(ref sList, val Loc, val dataIn) Temp = sList I=1 Loop(I < Loc) Temp = temp->link If(temp=null) then Write(Invalid Location) Return End if I= I+1 End loop Allocate(newNode) newNode->data = dataIn newNode->link = temp->link temp->link = newNode return End insAfter

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

Ex. No: 04
Priority for Operators: Priority 3: ^ Priority 2: * and / Priority 1: + and Priority 0: ( Algorithm:

Infix to Postfix Conversion

Algorithm inToPostFix(val Exp) Stack = createStack Set postfix to null string Looper = 1 Loop(Looper <= sizeof Exp) Token = Exp[Looper] If Token is open parenthesis then pushStack(Stack, Token) Else If Token is close parenthesis then popStack(Stack, Token) loop(Token not open paranthesis) concatenate Token to postFix popStack(Stack, Token) End loop Else If Token is Operator then stack Top(Stack, topToken) loop( not emptyStack(Stack) AND Priority(token) <= Priority(topToken) ) popStack (Stack, tokenOut) concatenate tokenOut to postFix stackTop (Stack, topToken) End loop PushStack (Stack, Token) Else Concatenate Token to postfix End if Looper = Looper + 1 End loop Loop (not emptyStack(Stack)) popStack (Stack, Token) concatenate Token to postFix End loop DestroyStack (Stack) Return postFix End inToPostFix

Ex. No: 05

DLL and CDLL Operations


Doubly Linked List Operations

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

3. Insert after Specified Location


Alogrithm insAfter(ref sList, val Loc, val dataIn) Temp = sList I=1 Loop(I < Loc) Temp = temp->sLink

If(temp=null) then Write(Invalid Location) Return End if I= I+1 End loop Allocate(newNode) newNode->data = dataIn newNode->pLink = temp newNode->sLink = temp->sLink temp->sLink->pLink = newNode temp->sLink = newNode return End insAfter

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

Circular Doubly Linked List 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

3. Insert after Specified Location


Alogrithm insAfter(ref sList, val Loc, val dataIn) Temp = sList I=1 Loop(I < Loc) Temp = temp->sLink If(temp=sList) then Write(Invalid Location) Return End if I= I+1 End loop Allocate(newNode) newNode->data = dataIn newNode->pLink = temp newNode->sLink = temp->sLink temp->sLink->pLink = newNode temp->sLink = newNode return End insAfter

4. Delete Node
Algorithm delNode(ref sList, val dataOut) If sList->sLink = sList 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 = 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
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 If temp = sList then Break loop End if 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 If temp = sList then Break loop End if End loop Return(Cnt) End getData

Ex. No: 06
1. Insertion

BST Operations

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->ritht = 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 postOrder (val Root) If Root=null then postOrder(Root->leftSubtree) postOrder(Root->rightSubtree) Process(Root) End if Return End postOrder

Ex. No: 07 Multi-Way Tree Operations


Aim: To perform basic operations such as INSERTION and DELETION on Multi-Way Tree 1. Insertion Insert the node anywhere in the tree by getting the level number and the parent to which it has to be associated. 2. Deletion Remove a node by getting the data and accordingly rearrange the tree. Pre Lab: Prior knowledge on Tree operations

Ex. No: 08 B-Tree Operations


B-Tree Insert Algorithm BTreeInsert(tree,data) 1 if(tree null) 1 create new node 2 set left subtree of node to null 3 move data to first entry in new node 4 set subtree of first entry to null 5 set tree root to address of new node 6 set number of entries to 1 2 end if 3 insertNode(tree, data, upEntry) 4 if(tree higher) 1 create new node 2 move upEntry to first entry in new node 3 set left subtree of node to tree 4 set tree root to new node 5 set number of entries to 1 6 end if end BTreeInsert

B-Tree Delete Algorithm BTreeDelete(tree,dltKey) Pre:- tree is a reference to a B-tree dltkey is the key of the entry to be deleted. Post:-data deleted or false returned Ret:- success(found) or failure (not found) 1 if(tree empty) 1 return false 2 end if 3 delete(tree, dltKey,success) 4 if(success) 1 if(tree number of entries zero) 1 set tree to left subtree 2 end if 5 end if 6 return success end BTreeDelete

Ex. No: 09 Trie Structure Operations


Aim: To perform basic operations such as INSERTION, DELETION and SEARCHING on Trie Structure 1. Insertion Insert a data along the path rather than in a single node. For n-character words, utmost there should be n+1 levels in the tree structure. Each node contains a DATA field and n number of LINK fields.

2. Deletion Find the path of the word to be deleted, remove that node and make necessary changes along that path. 3. Searching By scanning character from left to right one at a time, traverse the trie structure following the corresponding pointers to that character.

Ex. No: 10
1. Heap Sort

Sorting Techniques

Procedure Heapify(ref K, val N) Heapsize[K] = N I=N/2 Loop(I >= 1) Call Adjust(K,I) End loop Return End Heapify Procedure Adjust(ref K, val I) Left = 2*I Right = 2*I+1 If Left<=Heapsize[K] AND K[Left]<K[I] then Largest = I Else Largest = Left End if If Right<=Heapsize[K] AND K[Largest]<K[Right] then Largest = Right End if If Largest <> I then Swap K[I] and K[Largest] Call Adjust(K,largest) End if Return End Adjust Procedure HeapSort(ref K, val N) Call Heapify(K,N) Q=N Loop(Q>=2) Swap K[1] and K[Q] Heapsize[K] = Heapsize[K] 1 Call Adjust(K,1) End loop Return End HeapSort

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 Procedure Merge(ref K, val low, val mid, val high) I = low J = mid + 1 H = low Loop(I<=mid AND J<=high) If K[I]<=K[J] then Temp[H] = K[I] I=I+1 Else Temp[H] = K[J] J=J+1 End if H=H+1 End loop If I>mid then Loop(J<=high) Temp[H] = K[J] J=J+1 H=H+1 End loop Else Loop(I<=mid) Temp[H] = K[I] I=I+1 H=H+1 End loop End if I = low Loop(I<=high) K[I] = Temp[I] End loop Return End Merge

Ex. No: 11
1. Linear Search

Searching Techniques

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 2.1 Binary Search Iterative


Procedure BinarySearch_I(val K, val N, val X) Low = 1 High = N Loop(Low<=High) Mid = (Low + High) / 2 If X<K[Mid] then High = Mid 1 Else If X >K[Mid] then Low = Mid + 1 Else Return(Mid) End if End if End loop Return(0) End BinarySearch_I

2.2. Binary Search Recursive


Procedure BinarySearch_R(val K, val P, val Q, val X) If P > Q then Loc = 0 Else Mid = (P + Q) / 2 If X < K[Mid] then Loc = BinarySearch_R(K, P, Mid-1, X) Else If X > K[Mid] then Loc = BinarySearch_R(K, Mid+1, Q, X) Else Loc = Mid End if End if End if Return(Loc) End BinarySearch_R

3. Hash Search
Algorithm hash (val key, val size, val maxAddr, ref addr) Looper = 1 Addr = 0 Loop (looper<=size) If(key[looper] not space) Addr = addr + key[looper] Rotate addr 12 bits right End if Looper = looper + 1 If (addr<0) Addr = absolute(addr) End if Addr = addr modulo maxAddr + 1 Return End hash

Ex. No: 12 BFS & DFS


Breadth-first Traversal
Algorithm breadthFirst(graph) 1 if(empty graph) 1 return 2 end if 3 createQueue(queue) 4 loop(through all vertices) 1 set vertex to not processed 5 end loop 6 loop(through all vertices) 1 if(vertex not processed) 1 if(vertex not in queue) 1 enqueue(queue,walkPtr) 2 set vertex to enqueued 2 end if 3 loop(not emptyQueue(queue)) 1 set vertex to dequeue(queue) 2 process(vertex) 3 set vertex to processed 4 loop(through adjacency list) 1 if (destination not enqueued or processed) 1 enqueue(queue,destination)

2 set destination to enqueued 2 end if 3 get next destination 5 end loop 4 end loop 2 end if 3 get next vertex 7 end loop 8 destroyQueue(queue) end breadthFirst

Depth-First Traversal
Algorithm depthFirst(graph) Pre:- graph is a pointer to a graph head structure Post:- vertices processed 1 if(empty graph) 1 return 2 set walkPtr to graph first 3 loop (through all vertices) 1 set processed to 0 4 end loop 5 createStack(stack) 6 loop(through vertex list)

1 if(vertex not processed and not in stack) 1 pushStack(stack,walkPtr) 2 set walkPtr processed to 1 3 end if 4 loop(not emptyStack(stack)) 1 set vertex to popStack(stack) 2 process(vertex) 3 set vertex to processed 4 loop(through arc list) 1 if(arc destination not in stack) 1 pushStack(stack,destination) 2 set destination to in stack 2 end if 3 get next destination 5 end loop 2 end if 3 get next vertex 7 end loop 8 destroyStack(stack) end depthFirst

Ex. No: 13 Minimum Weight Spanning Tree


Procedure Prim(ref Head) Check[Head->Start] = 1 Check[Head->Termin] = 1 Head->Flag = 1 P = Head Loop(P not null) If P->Flag = 0 then If Check[P->Start]=1 AND Check[P->Termin] = 1 then Flag = -1 Else If Check[P->Start]<>0 OR Check[P->Termin]<>0 then Check[P->Start] = 1 Check[P->Termin] = 1 P->Flag = 1 P = Head End if End if End if P = P->Link End loop Return End Prim

Potrebbero piacerti anche