You are on page 1of 8

LAB REPORT

DATA STRUCTURE
CSE-2101

Submitted by Name: Habibur Rahman Roll: 1740 Experiment no. 05 to 08 Date: 10/04/2012

Experiment no.5: Implementation of linked list using two parallel arrays. i.) ii.) Inserting elements into a linked list in sorted order. Print the linked list.

iii.) iv.)

Find out an element in a linked list. Deleting an element with given location and with given value.

Introduction: Linked list is a linear collection of data elements (linear data structure). Here each element is represented by a node. Each node is divided into two parts. One is information part which contains the information of element and another is link part contains the address of the next node in the link. We can implement linked list using two parallel arrays. Here one array is represents as info and another represents next node. Algorithm: i.) Insert_Sorted_List_(Info, Link, Start, Item, LOC) 1. 2. 3. 4. 5. 6. Set Info[New] := Item If Item < Info[Start] then Link[New] := Start and Start := New and Return. Set Save := Start and PTR := Link[Start]. While PTR NULL do steps 5 and 6 If Item > Info[PTR] then Save := PTR and PTR := Link[PTR]. If Item < = Info[PTR] then Link[New] := PTR and Link[Save] := New and Return If PTR = NULL then Link[New] := PTR and Link[Save] := New. Return

7. 8.

ii.) Print the linked list Print_List (List, Info, Link, Start) 1. Set PTR := Start 2. While PTR NULL repeat steps 3 and 4 3. 4. Print (Info[PTR]) PTR := Link[PTR]

5. Return. iii.) Search item Search_List (Info, Link, Start, Item, Loc) 1. Set PTR := Start and Loc := NULL 2. While PTR NULL do steps 3 to 5 3. 4. 5. 6. If Item = Info[PTR] then Loc := PTR , print: item found and return. /Successful Search PTR := Link[PTR] If Loc = NULL then Print: Item not in the list. /Unsuccessful Search

7. Return

IV.) Deletion of a Node with Given Value Delete_Node_Value(Info, Link, Start, Item) 1. If Item = Info[Start] then Start := Link[Start] and Return. 2. Set Save := Start and PTR := Link[Start] 3. While PTR!=NULL and Info[PTR] Item do step 4 4. Save := PTR and PTR := Link[PTR].

5. If PTR = NULL then Write: Item not in the List and Return. 6. Set Link[Save] := Link[PTR] 7. Return. Deletion of a Node with Given Location Delete_Node_LOC(Info, Link, Start, LOC) 1. If LOC = 1 then Start := Link[Start] and Return. 2. Set Save := Start and PTR := Link[Start] ans Count := 1 3. While PTR!=NULL and Count + 1 LOC do step 4

4.

Save := PTR and PTR := Link[PTR] and Count := Count + 1.

5. If PTR = NULL then Write: Location not found and Return. 6. Set Save := Link[PTR] 7. Return.

Discussion: From this lab we come to know about how linked list work really in memory. And we know the basic structure of linked list. Though we cannot be able to allocate memory dynamically as in linked list.

Experiment no 6: Find out the postfix expression from a given infix expression and evaluate it using linked list. Introduction: Postfix is a one kind of arithmetic expression. Here operators are followed by operands where as an infix expression operator placed between operands. Algorithm: Infix-to-Postfix (Q, P) Here Q is an arithmetic expression in infix notation and this algorithm generates the postfix expression P using stack. 1. Scan the infix expression Q from left to right. 2. Initialize an empty stack. 3. Repeat step 4 to 5 until all characters in Q are scanned. 4. 5. If the scanned character is an operand, add it to P. If the scanned character is an operator , then (a) If stack is empty, push to the stack.

(b) Otherwise repeatedly pop from stack and add to P each operator which has the same or higher precedence than . (c) Push to the stack. 6. If scanned character is a left parenthesis ( , then push it to stack. 7. If scanned character is a right parenthesis ), then (a) Repeatedly pop from stack and add to P each operator until ( is encountered. (b) Remove ( from stack. 8. If all the characters are scanned and stack is not empty, then (a) Repeatedly pop the stack and add to P each operator until the stack is empty. 9. Exit.

Algorithm: Postfix-Evaluation (P, Value) Here P is an arithmetic expression in postfix notation and this algorithm finds the value of this expression using stack. 1. Scan the postfix expression P from left to right. 2. Initialize an empty stack. 3. Repeat step 4 to 5 until all characters in P are scanned. 4. 5. If the scanned character is an operand, push it to the satck. If the scanned character is an operator , then (a) Remove two top elements of stack where A is the top element and B is the next-to-top element.

(b) Evaluate T = B A and push T to the stack. 6. Pop the stack and assign the top element of the stack to Value. 7. Exit

Discussion: Today we came to know about different arithmetic expression. We know how to evaluate postfix expression using stack easily. All through this lab was pleasure to us all.

Experiment no 7: Implementation of binary search tree using array. i.) ii.) iii.) Insertion Traverse them by inorder. Print.

Introduction: Binary search tree is a binary tree in which each internal node x stores an element such that the element stored in the left subtree of x are less than or equal to x and elements stored in the right subtree of x are greater than or equal to x.

Algorithm: Using only a single liner array Tree. (a) Tree[1] represents the Root of T. (b) If node N is in Tree[K], then its left child is in Tree[2K] and right child is in Tree[2K+1]. (c) Tree[1] = NULL indicates T is empty.

Inorder Traversal Using Stack Algorithm: Inorder_Traverse(Tree, Root, Stack) (1) Set Stack[0]=NULL and Top=1 and Ptr=Root (2) Repeat while Ptr NULL (a) Set Stack[Top]=Ptr and Top=Top+1 (b) Set PTR=Ptr->Left (3) Set Ptr=Stack[Top] and Top := Top -1 (4) Repeat steps 5 to 7 while Ptr NULL (5) (6) Process Ptr->Info If Ptr->Right NULL then set Ptr=Ptr->Right and go to step 2.

(7) Set Ptr=Stack[Top] and Top=Top-1 (8) Exit

Discussion: From this lab experiment we come to know about the advantages of binary search tree, how to construct a binary tree using array and so on.

Experiment no 8: i.) ii.) iii.) Insertion Delete in inorder way Print the tree.

Introduction: Deletion is an important property of a data structure. Binary search tree is required to delete its node according to its inorder traversal.

Algorithm: Using only a single liner array Tree. (d) Tree[1] represents the Root of T. (e) If node N is in Tree[K], then its left child is in Tree[2K] and right child is in Tree[2K+1]. (f) Tree[1] = NULL indicates T is empty.

Delete_B(Root, Left, Right, Suc, ParSuc) 1. If Par NULL then 2. 3. If Loc = Par.Left then Set Par.Left := Suc Else Set Par.Right := Suc.

4. Else Root := Suc 5. Set Suc.Left := Loc.Left and Suc.Right := Loc.Right 6. Return.

Discussion: From this lab we come to know how to delete a node a binary search tree inorder way. Actually inorder deletion paves the way for deleting nodes in increasing order. It teaches us well.

You might also like