DATA STRUCTURES AND ALGORITHMS LABORATORY
EE2209 - DATA STRUCTURES AND ALGORITHMS LABORATORY
LAB MANUAL
Department of Information Technology,
Rajalakshmi Engineering College,
Thandalam.
INDEX
I Lab Syllabus
II Lab Plan
III Pseudo Codes for Lab experiments
1. (a) Implementation of Linked List
(b) Implementation of Doubly Linked list
2. Represent a polynomial as a linked list and write functions
For polynomial addition.
3. Implementation of tree traversal
4. Implementation of stack ( infix to postfix conversion)
5. Implementation of Binary search Tree
6. Implementation of insertion in AVL trees
7. Implementation of hashing techniques
8. Implementation of backtracking algorithm for knapsack problem
9. Implementation of prim’s and kruskal’s algorithm
10. Implementation of dijktra’s algorithm using priority queues
11. Implementation of array based circular queue
12. Implementation of priority queues using heaps
13. Implementation of branch and bound algorithm
14. Implementation of Randomized algorithm
15. Implementation of Topological sort on a Directed graph to decide is it is cyclic
EE2209 DATA STRUCTURES AND ALGORITHMS LABORATORY 0 0 3 2
(Common to EEE, EIE& ICE)
Aim:
To develop skills in design and implementation of data structures and their applications.
1. Implement singly and doubly linked lists.
2. Represent a polynomial as a linked list and write functions for polynomial addition.
3. Implement stack and use it to convert infix to postfix expression
4. Implement array-based circular queue and use it to simulate a producer-consumer problem.
5. Implement an expression tree. Produce its pre-order, in-order, and post-order traversals.
6. Implement binary search tree.
7. Implement insertion in AVL trees.
8. Implement priority queue using heaps
9. Implement hashing techniques
10. Perform topological sort on a directed graph to decide if it is acyclic.
11. Implement Dijkstra's algorithm using priority queues
12. Implement Prim's and Kruskal's algorithms
13. Implement a backtracking algorithm for Knapsack problem
14. Implement a branch and bound algorithm for traveling salesperson problem
15. Implement any randomized algorithm.
TOTAL : 45 PERIODS
LAB PLAN
|SI.No |Name of the Experiment |Date |
|1 |Implementation of Linked List | |
| |Implementation of Doubly Linked List | |
|2 |Represent a polynomial as a linked list and| |
| |write functions for polynomial addition. | |
|3 |Implementation of tree traversal | |
|4 |Implementation of stack (converting infix | |
| |to postfix expression) | |
|5 |Implementation of Binary search tree | |
|6 |Implementation of hashing techniques | |
|7 |Implementation of insertion in AVL Tree | |
|8 |Implementation of Backtracking algorithm | |
| |for Knapsack problem | |
|9 |Implementation of Prim’s and Kruskal’s | |
| |algorithm | |
|10 |Implementation of Dijktra’s algorithm using| |
| |priority queues | |
|11 |Implementation of array based circular | |
| |queue | |
|12 |Implementation of priority queues using | |
| |heaps | |
|13 |Implementation of Branch and bound | |
| |algorithm | |
|14 |Implementation of Randomized algorithm | |
|15 |Implementation of Topological sort on a | |
| |Directed graph to decide is it is cyclic | |
IMPLEMENTATION OF A LINKED LIST
Ex. No : 1(a)
AIM:
To implement a linked list and do all operations on it.
ALGORITHM:
Step 1: Start the process.
Step 2: Initialize and declare variables.
Step 3: Enter the choice. INSERT / DELETE.
Step 4: If choice is INSERT then
a) Enter the element to be inserted.
b) Get a new node and set DATA[NEWNODE] = ITEM.
c) Find the node after which the new node is to be inserted.
d) Adjust the link fields.
e) Print the linked list after insertion.
Step 5: If choice is DELETE then
a) Enter the element to be deleted.
b) Find the node containing the element (LOC) and its preceding node (PAR).
c) Set ITEM = DATA [LOC] and delete the node LOC.
d) Adjust the link fields so that PAR points to the next element. ie
LINK [PAR] = LINK [LOC].
e) Print the linked list after deletion.
Step 6: Stop the process.
IMPLEMENTATION OF DOUBLY LINKED LIST
Ex. No : 1(b)
AIM:
To implement a doubly linked list and do all operations on it.
ALGORITHM:
Step 1: Data type declarations
record Node {
data
prev
}
record List {
Node firstNode // points to first node of list; null for empty list
Node lastNode // points to last node of list; null for empty list
}
Step 2: Iterating over the nodes
Iterating through a doubly linked list can be done in either direction. In fact, direction can change many times, if desired.
node := list.firstNode
while node ≠ null
node := node.next
node := list.lastNode
while node ≠ null
node := node.prev
Step 3:Inserting a node
function insertAfter(List list, Node node, Node newNode)
newNode.prev := node
newNode.next := node.next
if node.next == null
list.lastNode := newNode
else
node.next.prev := newNode
node.next := newNode
function insertBefore(List list, Node node, Node newNode)
newNode.prev := node.prev
newNode.next := node
if node.prev is null
list.firstNode := newNode
else
node.prev.next := newNode
node.prev := newNode
Function to insert a node at the beginning of a possibly-empty list:
function insertBeginning(List list, Node newNode)
if list.firstNode == null
list.firstNode := newNode
list.lastNode := newNode
newNode.prev := null
newNode.next := null
else
insertBefore(list, list.firstNode, newNode)
A symmetric function inserts at the end:
function insertEnd(List list, Node newNode)
if list.lastNode == null
insertBeginning(list, newNode)
else
insertAfter(list, list.lastNode, newNode)
Step 5:Deleting a node
Removing a node is easier, only requiring care with the firstNode and lastNode:
function remove(List list, Node node)
if node.prev == null
list.firstNode := node.next
else
node.prev.next := node.next
if node.next == null
list.lastNode := node.prev
else
node.next.prev := node.prev
destroy node
Viva Questions:
• A Linked list can grow and shrink in size dynamically at _______.
• Write an algorithm to detect loop in a linked list.
• Reverse a linked list.
• Delete an element from a doubly linked list.
• Implement an algorithm to reverse a singly linked list. (with and without recursion)
• Implement an algorithm to reverse a doubly linked list
• How would you find a cycle in a linked list? Try to do it in O(n) time. Try it using a constant amount of memory.
REPRESENT A POLYNOMIAL AS LINKED LIST AND IMPLEMENT
POLYNOMIAL ADDITION
Ex. No: 2
AIM:
To represent polynomial as linked list and perform polynomial addition.
ALGORITHM:
Step 1: Creating Node.
/*Inserting an element in a sorted linked list. Let the data be sorted and put in a singly linked linear list which is being pointed by address “head “.Let the new data to be entered be “d”.Use malloc get a node with address “pNew”. Suppose we want to write a code to enter data “d” into the node and insert it into its proper place in the list.*/
typedef struct node {
int data;
struct node *next;
};
struct node* pNew = (struct node*)
(malloc(sizeof(struct node)));
pNew -> data = d;
pNew ->next = NULL;
pCur = head ;
/* check if data is smaller than smallest item on the list*/
if (pNew -> data < pCur -> data )
{
pNew ->next = pCur ;
head = pNew;
}
/* now examine the remaining list */
p = pCur -> next ;
while(p!=NULL ||p->data < pNew->data )
{
pCur = pCur -> next ;
p = p -> next ;
}
pNew -> next = pCur -> next;
pCur -> next = pNew ;
A polynomial can be represented in an array or in a linked list by simply storing the coefficient and exponent of each term. However, for any polynomial operation, such as addition or multiplication of polynomials,
Step 2: Addition of two polynomials
Consider addition of the following polynomials
5 x12 + 2 x9 + 4x7 + 6x6 + x3
7 x8 + 2 x7 + 8x6 + 6x4 + 2x2 + 3 x + 40
The resulting polynomial is going to be
5 x12 + 2 x9 + 7 x8 + 6 x7 + 14x6 + 6x4 +x3
2x2 + 3 x + 40
Step 3: Result of addition is going to be stored in a third list.
Step 4: Started with the highest power in any polynomial.
Step 5: If there was no item having same exponent, simply appended the term to the new list, and continued with the process.
Step 6: Wherever the exponents were matching, simply added the coefficients and then stored the term in the new list.
Step 6: If one list gets exhausted earlier and the other list still contains some lower order terms, then simply append the remaining terms to the new list.
//Pseudo code
Let phead1 , phead2 and phead3 represent the pointers of
the three lists under consideration.
Let each node contain two integers exp and coff .
Let us assume that the two linked lists already contain
relevant data about the two polynomials.
Also assume that we have got a function append to insert a
new node at the end of the given list.
p1 = phead1;
p2 = phead2;
Let us call malloc to create a new node p3 to build the
third list
p3 = phead3;
/* now traverse the lists till one list gets exhausted */
while ((p1 != NULL) || (p2 != NULL))
{
/ * if the exponent of p1 is higher than that of p2 then
the next term in final list is going to be the node of p1* /
while (p1 ->exp > p2 -> exp )
{
p3 -> exp = p1 -> exp;
p3 -> coff = p1 -> coff ;
append (p3, phead3);
/* now move to the next term in list 1*/
p1 = p1 -> next;
}
/ * if p2 exponent turns out to be higher then make p3
same as p2 and append to final list * /
while (p1 ->exp < p2 -> exp )
{
p3 -> exp = p2 -> exp;
p3 -> coff = p2 -> coff ;
append (p3, phead3);
p2 = p2 -> next;
}
/* now consider the possibility that both exponents are
same , then we must add the coefficients to get the term for
the final list */
while (p1 ->exp = p2 -> exp )
{
p3-> exp = p1-> exp;
p3->coff = p1->coff + p2-> coff ;
append (p3, phead3) ;
p1 = p1->next ;
p2 = p2->next ;
}
}
/* now consider the possibility that list2 gets exhausted, and there are terms remaining only in list1. So all those terms have to be appended to end of list3. However, you do not have to do it term by term, as p1 is already pointing to remaining terms, so simply append the pointer p1 to phead3
*/
if ( p1 != NULL)
append (p1, phead3) ;
else
append (p2, phead3);
.
IMPLEMENTATION OF TREE TRAVERSALS
Ex. No. :3
Date :
AIM:
To implement tree traversals using linked list.
ALGORITHM:
Step 1: Start the process.
Step 2: Initialize and declare variables.
Step 3: Enter the choice. Inorder / Preorder / Postorder.
Step 4: If choice is Inorder then
a) Traverse the left subtree in inorder.
b) Process the root node.
c) Traverse the right subtree in inorder.
Step 5: If choice is Preorder then
a) Process the root node.
b) Traverse the left subtree in preorder.
c) Traverse the right subtree in preorder.
Step 6: If choice is postorder then
a) Traverse the left subtree in postorder.
b) Traverse the right subtree in postorder.
c) Process the root node.
Step7: Print the Inorder / Preorder / Postorder traversal.
Step 8: Stop the process.
Viva Questions:
• Write a function and the node data structure to visit all of the nodes in a binary tree.
• What is a balanced tree
• Do a breadth first traversal of a tree.
• How would you print out the data in a binary tree, level by level, starting at the top?
• Write a function to find the depth of a binary tree.
• How do you represent an n-ary tree? Write a program to print the nodes of such a tree in breadth first order.
• What is a spanning Tree?
STACK IMPLEMENTATION FOR CONVERTING INFIX TO POSTFIX EXPRESSION
Ex. No. :4
AIM:
To implement stack for converting infix to postfix expression
ALGORITHM:
1. Start by initializing an empty stack. This will be for holding our operators.
2. Read the string, getting the first operand and add it to the postfix string.
3. Read the next object in the string, normally a operator, and push the operator to the stack. Is the operator of greater, equal, or lesser precedence than the top most operator on the stack?
If the operator is greater than the top most operator, push the current operator and continue.
If the operator is lesser to the top most operator, pop the stack adding it to the postfix string, and then push the current operator to the stack. Test the next operator to see if it is also lesser and repeat step 2. If not continue.
If the operator is equal to the top most operator pop the top of the stack adding it to the postfix string and then push the current operator to the stack.
4. Repeat these steps until all of the operands and operators are taken care of.
5. Afterwards look at the stack one last time to see if any operators remain, pop them all and add them to the end of the postfix string.
6. Stop the process.
\
Viva questions:
• Given an expression tree with no parentheses in it, write the program to give equivalent infix expression with parentheses inserted where necessary.
• How would you implement a queue from a stack?
• What is stack?
• List the difference between queue and stack.
• How would you implement a queue from a stack?
IMPLEMENTATION OF BINARY SEARCH TREE
Ex. No. :5
AIM:
To implement binary search tree and to do BST operations
ALGORITHM:
find() Operation
//Purpose: find Item X in the Tree
//Inputs: data object X (object to be found), binary-search-tree node node
// Output: bst-node n containing X, if it exists; NULL otherwise.
find(X, node)f if(node = NULL)
return NULL
if(X = node:data)
return node
else if(X < node:data)
return find(X,node:leftChild)
else // X > node:data
return find(X,node:rightChild)
{
findMinimum() Operation
//Purpose: return least data object X in the Tree
//Inputs: binary-search-tree node node
// Output: bst-node n containing least data object X, if it exists; NULL otherwise.
findMin(node)f if(node = NULL) //empty tree
return NULL
if(node:leftChild = NULL)
return node
return findMin(node:leftChild)
}
insert() Operation
//Purpose: insert data object X into the Tree
//Inputs: data object X (to be inserted), binary-search-tree node node
//Effect: do nothing if tree already contains X;
// otherwise, update binary search tree by adding a new node containing data object X
insert(X, node)f if(node = NULL)f node = new binaryNode(X,NULL,NULL)
return
{if(X = node:data)
return
else if(X < node:data)
insert(X, node:leftChild)
else // X > node:data
insert(X, node:rightChild)
}
delete() Operation
//Purpose: delete data object X from the Tree
//Inputs: data object X (to be deleted), binary-search-tree node node
//Effect: do nothing if tree does not contain X;
// otherwise, update binary search tree by deleting the node containing data object X
delete(X, node)f if(node = NULL) //nothing to do
return
if(X < node:data)
delete(X, node:leftChild)
else if(X > node:data)
delete(X, node:rightChild)
else f // found the node to be deleted! Take action based on number of node children
if(node:leftChild = NULL and node:rightChild = NULL)f delete node
node = NULL
return
{else if(node:leftChild = NULL)f tempNode = node
node = node:rightChild
delete tempNode
{else if(node:rightChild = NULL)f (similar to the case when node:leftChild = NULL)
{else f //replace node:data with minimum data from right subtree
tempNode = findMin(node.rightChild)
node:data = tempNode:data
delete(node:data,node:rightChild)
}
}
}
Viva questions:
• What is tree?
• What are the advantages of BST?
• Write a function and the node data structure to visit all of the nodes in a binary tree.
IMPLEMENTATION OF INSERTION IN AVL TREE
Ex. No. :6
AIM:
To implement Insertion in AVL tree.
ALGORITHM:
int avl_insert(node *treep, value_t target)
{
/* insert the target into the tree, returning 1 on success or 0 if it
* already existed
*/
node tree = *treep;
node *path_top = treep;
while (tree && target != tree->value) {
direction next_step = (target > tree->value);
if (!Balanced(tree)) path_top = treep;
treep = &tree->next[next_step];
tree = *treep;
}
if (tree) return 0;
tree = malloc(sizeof(*tree));
tree->next[0] = tree->next[1] = NULL;
tree->longer = NEITHER;
tree->value = target;
*treep = tree;
avl_rebalance(path_top, target);
return 1;
}
void avl_rebalance_path(node path, value_t target)
{
/* Each node in path is currently balanced.
* Until we find target, mark each node as longer
* in the direction of target because we know we have
* inserted target there
*/
while (path && target != path->value) {
direction next_step = (target > path->value); path->longer
node avl_rotate_2(node *path_top, direction dir)
{
node B, C, D, E;
B = *path_top;
D = B->next[dir];
C = D->next[1-dir];
E = D->next[dir];
*path_top = D;
D->next[1-dir] = B;
B->next[dir] = C;
B->longer = NEITHER;
D->longer = NEITHER;
return E;
} node avl_rotate_3(node *path_top, direction dir, direction third)
{
node B, F, D, C, E;
B = *path_top;
F = B->next[dir];
D = F->next[1-dir];
/* node: C and E can be NULL */
C = D->next[1-dir];
E = D->next[dir]; *path_top = D; D->next[1-dir] = B; D->next[dir] = F; B->next[dir] = C; F->next[1-dir] = E; D->longer = NEITHER; /* assume both trees are balanced */ B->longer = F->longer = NEITHER; if (third == NEITHER) return NULL; if (third == dir) { /* E holds the insertion so B is unbalanced */ B->longer = 1-dir; return E; } else { /* C holds the insertion so F is unbalanced */ F->longer = dir; return C; } } and there you have an AVL insertion, in non-recursive style, using only constant space, but sometimes performing more comparisons that absolutely needed.
IMPLEMENTATION OF HASHING TECHNIQUES
Ex. No. :7
AIM:
To implement hashing techniques
ALGORITHM:
For insertion:
HASH-INSERT (T, k)
i = 0
Repeat j ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- american structures and design
- brain structures and their functions
- brain structures and functions worksheet
- subcortical structures and their functions
- us government structures and institutions
- academic essay structures and format
- structures and functions of the brain
- cerebral structures and function
- market structures and their characteristics
- body structures and functions answers
- supply chain structures and relationships
- qa and qc laboratory standards