EC8381 - Fundamentals of Data Structures in C Laboratory Manual - by LearnEngineering - in
EC8381 - Fundamentals of Data Structures in C Laboratory Manual - by LearnEngineering - in
in
COURSE OBJECTIVES
n
LIST OF EXPERIMENTS:
g.i
1. Basic C Programs – looping, data manipulations, arrays
rin
2. Programs using strings – string function implementation
ee
3. Programs using structures and pointers
gin
11. Implementation Insertion sort, Bubble sort, Quick sort and Merge Sort
COURSE OUTCOMES
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
CONTENTS
n
g.i
3 Programs using structures and pointers 15
rin
4 20
11 50
Implementation Hash functions, collision resolution technique
12 58
Le
w.
ww
Aim:
To write a C program to calculate the sum of first n natural numbers using for loop statements
Software requirements:
C compiler
Hardware requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program.
2. Read the variables.
ee
3. The initialization statement is executed.
gin
4. The test expression is evaluated, if it is false for loop is terminated, test expression is true (nonzero),
codes inside the body of loop is executed and the update expression is updated.
5. This process repeats until the test expression is false.
En
Output
Sum = 55
Result:
Thus the program to find the sum of n natural numbers was executed successfully.
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
Aim:
To write a C program for data manipulations using the bitwise operators
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
n
Algorithm :
g.i
1. Start the program.
rin
2. Declare the variables.
3. Use the required bitwise operators for the given input.
ee
4. Display the result.
gin
5. Stop the program.
En
arn
Le
w.
ww
Sample Output :
A = 10
B = 25
Bitwise AND operator & = 8
A = 10
B = 25
Bitwise OR operator | = 29
n
A= 10
g.i
B = 25
Bitwise XOR (exclusive OR) operator ^ = 21
rin
ee
complement = ~35
complement = ~-12
gin
complement = -36
Output = 11
En
Result :
ww
Thus the program for data manipulations using the bitwise operators is executed successfully.
10
Aim:
Software requirements:
C compiler
Hardware requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program. ee
2. Declare the array and the variables required for the program.
3. Get the values .
gin
11
Sample Output :
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Result :
n
g.i
Outcome :
Thus the basic c programs for looping, Data manipulations and arrays is attained.
VIVA - VOCE
arn
5. What is an operator ?
7. What is an array ?
12
AIM:
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
n
g.i
Algorithm :
1. Start the program.
13
Sample Output :
Result :
Thus the program for string manipulation is executed successfully.
Outcome :
Thus the programs using strings- string function implementation is attained.
n
Applications:
g.i
String are used in spell checker, spam filter, intrusion detection
VIVA - VOCE
rin
ee
gin
1. What is a function ?
2. Mention the types of function.
En
3. What is a character ?
4. What is a string ?
arn
14
Aim:
Software requirements:
C compiler
Hardware requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program.
4. If the content of the address stored in the pointer is required, the indirection operator * is used to
obtain the value.
En
15
Sample Output :
Before Swapping : A = 10 B = 20
After Swapping : A = 20 B = 10
Result :
Thus the program to swap two numbers using pointers is executed successfully.
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
16
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
17
Sample Output :
n
Enter Employee Name :Rajesh
Enter Basic Salary : 22000
g.i
XYZ & Co. Payroll
rin
********************************************************************************
EmpId Name Basic HRA DA IT Gross Net Pay
ee
********************************************************************************
436 Gopal 10000 200.00 100.00 500.00 10300.00 9800.00
gin
Result :
arn
Outcome :
Le
Applications:
ww
The C pointer are used to track read /write position in files in Linux/Unix OS.
VIVA - VOCE
18
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
19
Aim:
To write a c program to perform dynamic memory allocation.
Software requirements:
n
g.i
C compiler
Hardware requirements:
rin
Server with supporting 30 terminals
ee
Algorthim:
1. Start the program.
gin
2. Include the required header file for using the memory allocation function.
3. Declare the required variables for the program.
En
4. Cal the required Dynamic Memory Allocation function depending upon the program.
5. Display the result.
arn
20
Sample Output :
Enter integer value: 100
Enter character value: x
Enter float value: 123.45
Inputted value are: 100, x, 123.45
Result:
Thus the program to perform dynamic memory allocation is executed successfully.
Outcome:
Thus the programs involving dynamic memory allocations are attained.
n
Applications:
g.i
The memory allocation concepts are used in operating systems present in embedded
rin
software .
ee
1. What is memory allocation? VIVA - VOCE
gin
2. What is meant by dyanamic memory allocation?
3. Differentiate static memory allocation from dynamic memory allocation?
En
8. What is meant by free() and what is the purpose of using free() fuction?
9.What is the significance of calloc()?
w.
21
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
n
g.i
Algorithm :
2.
rin
Include all the header files which are used in the program and define a
ee
constant 'SIZE' with specific value.
gin
5. Define a integer variable 'top' and initialize with '-1'. (int top = -1)
6. In main method display menu with list of operations and make suitable function calls to
Le
PUSH OPERATION
ww
2. If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate
the function.
3. If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
POP OPERATION
22
3. If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--)
DISPLAY
n
1.Check whether stack is EMPTY. (top == -1)
g.i
2. If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
rin
3. If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value
ee
and decrement i value by one (i--).
gin
23
Sample Output :
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT Enter Choice : 1
Enter Stack element : 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT Enter Choice : 1
Enter Stack element : 23
STACK OPERATION
n
.PUSH 2.POP 3.VIEW 4.QUIT Enter Choice : 1
g.i
Enter Stack element : 34
STACK OPERATION
rin
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
ee
Enter Stack element : 45
gin
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
En
Top--> 45 34 23 12
STACK OPERATION
arn
Popped element is 45
STACK OPERATION
w.
Top--> 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 4
Result :
Thus the program for array implementation of stack is executed successfully.
24
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program.
ee
2. Include all the header files which are used in the program and define a constant 'SIZE' with specific
gin
value.
3. Declare all the user defined functions which are used in queue implementation.
En
4. Create a one dimensional array with above defined SIZE (int queue[SIZE])
arn
5. Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -1)
Le
6. Then implement main method by displaying menu of operations list and make suitable function calls
to perform operation selected by the user on queue.
w.
ww
ENQUEUE OPERATION
2. If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the
function.
3. If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear] = value.
25
DEQUEUE OPERATION
2. If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the
function.
3. If it is NOT EMPTY, then increment the front value by one (front ++). Then display queue[front] as
deleted element. Then check whether both front and rear are equal (front == rear), if it TRUE, then set
n
both front and rear to '-1' (front = rear = -1).
g.i
DISPLAY OPERATION
3. If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
En
4. Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value is equal
arn
26
Sample Output :
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Enter element to be inserted : 12
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
n
Enter element to be inserted : 23
g.i
QUEUE OPERATION
rin
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Enter element to be inserted : 34 ee
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
gin
QUEUE OPERATION
En
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Le
Queue Full
QUEUE OPERATION
w.
27
Result :
Outcome:
Thus the program for array implementation of stack and queue is attained.
VIVA - VOCE
1. What is meant by data structure?
n
2. What are the types of data structure?
g.i
3. What is meant by stack?
4. What is meant by queue?
5. State the working principle of stack and queue?
rin
ee
6. What is difference between stack and queue?
7. What are the function of stack and queue?
gin
28
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
rin
Algorithm :
functions.
En
5. Implement the main method by displaying Menu with list of operations and make suitable function
Le
PUSH OPERATION
ww
29
POP OPERATION
2. If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the
function
3. If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
n
g.i
5. Finally, delete 'temp' (free(temp)).
rin
DISPLAY OPERATION
3. If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
En
4. Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the
arn
30
Sample Output :
n
Stack using Linked List
g.i
1->Push 2->Pop 3->View 4->Exit Enter your choice : 3
HEAD -> 34 -> 23 -> NULL
rin
Result : ee
Thus the program for Linked List implementation of Stack is executed successfully.
gin
En
arn
Le
w.
ww
31
Aim:
Software Requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
n
g.i
Algorithm :
rin
1. Start the program.
ee
2. Include all the header files which are used in the program. And declare all the user defined functions.
gin
4. Define two Node pointers 'front' and 'rear' and set both to NULL.
arn
5. Implement the main method by displaying Menu of list of operations and make suitable function calls
in the main method to perform user selected operation.
Le
enQueue OPERATION
w.
1. Create a newNode with given value and set 'newNode → next' to NULL.
ww
4. If it is Not Empty then, set rear → next = newNode and rear = newNode.
32
dEQueue OPERATION
2. If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the
function
3. If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
n
g.i
DISPLAY OPERATION
3. If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
En
4. Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to
arn
33
Sample Output
n
g.i
rin
Result :
Thus the program for Linked List implementation of Queue is executed successfully.
ee
Outcome :
gin
Thus the program for Linked List implementation of stack and queue is attained.
En
Applications:
Thread scheduler which maintains process in the memory .Linked list implementation is used
arn
VIVA - VOCE
Le
34
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program.
2. Read all the symbols one by one from left to right in the given Infix Expression.
ee
3. If the reading symbol is operand, then directly print it to the result (Output).
gin
4. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
En
5. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left
parenthesis is poped and print each poped symbol to the result.
arn
6. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, first pop the
operators which are already on the stack that have higher or equal precedence than current operator
and print them to the result.
Le
w.
ww
35
Sample Output :
INFIX EXPRESSION : ( A + B ) * ( C - D )
POSTFIX EXPRESSION : A B + C D - *
Result :
Thus the program for performing infix expression to postfix expression is executed successfully.
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
36
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
g.i
Server with supporting 30 terminals
rin
Algorithm :
4. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop operations and store the
two popped oparands in two different variables (operand1 and operand2). Then perform reading
symbol operation using operand1 and operand2 and push result back on to the Stack.
arn
5. Finally! perform a pop operation and display the popped value as final result.
Le
w.
ww
37
Sample Output :
Result :
Thus the program for performing postfix expression to infix expression is executed successfully.
n
g.i
Outcome :
Applications:
rin
ee
In high level programming language, the arithmetic expressions are calculated. The stacks are
used to convert the expression which is understood by the computer.
gin
En
VIVA - VOCE
arn
38
Aim:
Software Requirements:
C compiler
n
g.i
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
rin
ee
1. Start the program.
gin
PREORDER TRAVERSAL
2. Process the root node (N).
En
INORDER TRAVERSAL
Le
POSTORDER TRAVERSAL
8. Traverse the left subtree of N (L).
9. Traverse the right subtree of N (R).
10. Process the root node (N).
39
Sample Output :
n
Post order traversal sequence : I - J - D - F - B - K - G - H - C - A
g.i
Pre order traversal sequence : A - B - D - I - J - F - C - G - K - H
Result :
rin
ee
Thus the program for performing binary tree traversal is executed successfully.
gin
Outcome :
Thus the program for implementation of Trees, Tree Traversals is attained.
En
arn
Applications:
Manipulate hierarchical data
Manipulate sorted list of data
Le
40
Aim:
Software Requirements:
n
C compiler
g.i
Hardware Requirements:
Server with supporting 30 terminals
rin
Algorithm :
ee
1. Start the program.
gin
3. Compare, the search element with the value of root node in the tree.
Le
4. If both are matching, then display "Given node found!!!" and terminate the function
w.
5. If both are not matching, then check whether search element is smaller or larger than that
node value.
ww
6. If search element is smaller, then continue the search process in left subtree.
7. If search element is larger, then continue the search process in right subtree.
8. Repeat the same until we found exact element or we completed with a leaf node
9. If we reach to the node with search value, then display "Element is found" and terminate
the function.
41
10. If we reach to a leaf node and it is also not matching, then display "Element not found"
and terminate the function.
1. Create a newNode with given value and set its left and right to NULL.
n
g.i
4. If the tree is Not Empty, then check whether value of newNode is smaller or larger than
the node (here it is root node).
rin
5. If newNode is smaller than or equal to the node, then move to its left child. If newNode
ee
is larger than the node, then move to its right child.
gin
6. Repeat the above step until we reach to a leaf node (e.i., reach to NULL).
En
7. After reaching a leaf node, then isert the newNode as left child if newNode is smaller or
equal to that leaf else insert it as right child.
arn
2. Delete the node using free function (If it is a leaf) and terminate the function.
42
2. If it has only one child, then create a link between its parent and child nodes.
3. Delete the node using free function and terminate the function.
n
Case 3: Deleting a node with two children
g.i
1. Find the node to be deleted using search operation
rin
2. If it has two children, then find the largest node in its left subtree (OR)
ee
the smallest node in its right subtree.
gin
3. Swap both deleting node and node which found in above step.
En
4. Then, check whether deleting node came to case 1 or case 2 else goto steps 2
7. Repeat the same process until node is deleted from the tree.
w.
ww
43
Sample Output :
Select an option
1- insert.
2- Search.
3- Delete.
4 – Display.
5 - Exit
n
Enter your choice : 1
g.i
Enter the node value : 6
rin
Enter the node value : 1
Select an option
Le
1- insert.
2- Search.
3- Delete.
w.
4 – Display.
5 - Exit
ww
44
Result :
Thus the program for performing Binary Search Tree Operations is executed successfully.
Outcome :
Applications:
n
Used in search application where data is constantly entered and stored
g.i
Binary search partition used in 3D video game.
Binary tries – Used in high bandwidth router for storing routing data.
Syntax tree- Used for construction of compiler and calculator to parse expression
rin
ee
gin
En
arn
Le
w.
ww
45
Aim:
Software Requirements:
C compiler
n
Hardware Requirements:
g.i
Server with supporting 30 terminals
rin
Algorithm :
3. Compare, the search element with the first element in the list.
En
4. If both are matching, then display "Given element found!!!" and terminate the function
arn
5. If both are not matching, then compare search element with the next element in the list.
Le
6. Repeat steps 3 and 4 until the search element is compared with the last element in the list.
w.
7. If the last element in the list is also doesn't match, then display "Element not found!!!" and terminate
the function.
ww
46
Sample Output :
n
g.i
Result :
rin
Thus the program to perform Linear search operation is executed successfully.
ee
gin
En
arn
Le
w.
ww
47
Aim:
Algorithm :
n
1. Start the program.
g.i
2. Read the search element from the user
rin
3. Find the middle element in the sorted list ee
4. Compare, the search element with the middle element in the sorted list.
gin
5. If both are matching, then display "Given element found!!!" and terminate the function
En
6. If both are not matching, then check whether the search element is smaller or larger than middle
element.
arn
7. If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the left
sublist of the middle element.
Le
8. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the right
w.
9. Repeat the same process until we find the search element in the list or until sublist contains only one
element.
10. If that element also doesn't match with the search element, then display "Element not found in the
list!!!" and terminate the function.
48
Sample Output :
n
g.i
Result :
rin
Thus the program to perform binary search operation is executed successfully.
ee
Outcome :
gin
Applications:
En
49
Aim:
Software Requirements:
C compiler
n
Hardware Requirements:
g.i
Server with supporting 30 terminals
rin
Algorithm : ee
1. Start the program.
gin
2. Asume that first element in the list is in sorted portion of the list and remaining all elements are in
unsorted portion.
En
3. Consider first element from the unsorted list and insert that element into the sorted list in order
arn
specified.
4. Repeat the above process until all the elements from the unsorted list are moved into the sorted list.
Le
w.
ww
50
Sample Output :
n
After Pass 3: 8 34 51 64 32 21
After Pass 4: 8 32 34 51 64 21
g.i
After Pass 5: 8 21 32 34 51 64
Sorted List : 8 21 32 34 51 64
Result :
rin
ee
Thus the program to perform insertion sort is executed successfully.
gin
En
arn
Le
w.
ww
51
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program.
4. set j=0.
En
7. Swap the elements stored at arr[j] and arr[j+1] and perform the operations.
Le
8. set j=j + 1.
9. set i= i -1.
w.
10. stop.
ww
52
Sample Output :
n
g.i
The sorted elements are :
rin
3
18
21
ee
33
gin
Result :
53
Aim:
Software Requirements:
C compiler
Hardware Requirements:
n
g.i
Server with supporting 30 terminals
rin
Algorithm :
3. Rearrange elements of the array by moving all elements xi > V right of V and all elements xi < = V
left of V. If the place of the V after re-arrangement is j, all elements with value less than V, appear in
a[0], a[1] . . . . a[j – 1] and all those with value greater than V appear in a[j + 1] . . . . a[n – 1].
arn
4. Apply quick sort recursively to a[0] . . . . a[j – 1] and to a[j + 1] . . . . a[n – 1].
Le
54
Sample Output :
n
57
g.i
40
rin
2
5
ee
34
40
gin
57
99
Result :
En
55
Aim:
Algorithm :
n
2. Divide the unsorted list into N sublists, each containing 1 element.
g.i
3. Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. N will
now convert into N/2 lists of size 2.
rin
4. Repeat the process till a single sorted list of obtained.
ee
gin
En
arn
Le
w.
ww
56
Sample Output :
Enter the number of elements in the array :
8
n
29
g.i
The sorted elements are :
rin
2
5
8
ee
29
34
gin
40
57
99
En
Result :
arn
Outcome :
Le
Thus the implementation of Insertion sort, Bubble sort, Quick sort, Merge sort is attained.
w.
Applications:
ww
57
Aim:
Software Requirements:
C compiler
n
Hardware Requirements:
g.i
Server with supporting 30 terminals
Algorithm :
3. An element is converted into an integer by using a hash function. This element can be used as an
index to store the original element, which falls into the hash table.
En
4. The element is stored in the hash table where it can be quickly retrieved using hashed key.
hash = hashfunc(key)
arn
58
Sample Output :
Result :
Thus the program to perform hash function is executed successfully.
n
g.i
rin
ee
gin
En
arn
Le
w.
ww
59
Aim:
To write a C program to perform Linear probing.
Software Requirements:
C compiler
Hardware Requirements:
n
Server with supporting 30 terminals
g.i
Algorithm :
rin
1. Start the program. ee
2. Create an array of structure (i.e a hash table).
gin
3. Take a key and a value to be stored in hash table as input.
4. Corresponding to the key, an index will be generated i.e every key is stored in a particular array
index.
En
5. Using the generated index, access the data located in that array index.
arn
6. In case of absence of data, create one and insert the data item (key and value) into it and increment
the size of hash table.
Le
7. In case the data exists, probe through the subsequent elements (looping back if necessary) for free
space to insert new data item.
Note: This probing will continue until we reach the same element again (from where we began probing)
w.
8. To display all the elements of hash table, element at each index is accessed (via for loop).
ww
9. To remove a key from hash table, we will first calculate its index and delete it if key matches, else
probe through elements until we find key or an empty space where not a single data has been entered
(means data does not exist in the hash table).
10. Exit
60
Sample Output :
MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
n
MENU-:
g.i
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
rin
4. Display Hashtable
61
n
Array[2] has elements-:
12 (key) and 10 (value)
g.i
Array[3] has elements-:
rin
122(key) and 5(value)
4. Display Hashtable
Result :
n
g.i
Thus the program to perform linear probing is executed successfully.
Outcome :
rin
Thus the implementation of Hash functions, collision resolution techniques is initiated.
ee
Applications:
gin
Time stamping
arn
Le
w.
ww
63