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
LIST OF EXPERIMENTS:
11. Implementation Insertion sort, Bubble sort, Quick sort and Merge Sort
5
EC8381 - FUNDAMENTALS OF DATA STRUCTURES IN C
SYLLABUS
CONTENTS
6
EXPT. NO. 1a BASIC C PROGRAM – LOOPING
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:
Server with supporting 30 terminals
Algorithm :
7
Output
Sum = 55
Result:
Thus the program to find the sum of n natural numbers was executed successfully.
8
EXP 1 b. BASIC C PROGRAM – DATA MANIPULATIONS
Aim:
To write a C program for data manipulations using the bitwise operators
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
Algorithm :
9
Sample Output :
A = 10
B = 25
Bitwise AND operator & = 8
A = 10
B = 25
Bitwise OR operator | = 29
A= 10
B = 25
Bitwise XOR (exclusive OR) operator ^ = 21
complement = -36
Shift Operator ( >> and <<)
Num = 212
Result :
Thus the program for data manipulations using the bitwise operators is executed successfully.
10
EXP 1 c BASIC C PROGRAM – ARRAYS
Aim:
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
Algorithm :
11
Sample Output :
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Result :
Outcome :
Thus the basic c programs for looping, Data manipulations and arrays is attained.
VIVA - VOCE
5. What is an operator ?
7. What is an array ?
12
EXP NO 2 : PROGRAMS USING STRINGS FUNCTION
AIM:
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
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.
Applications:
VIVA - VOCE
1. What is a function ?
2. Mention the types of function.
3. What is a character ?
4. What is a string ?
5. Difference between character and string.
6. What are the different types of string handling functions are available.
7. Which header file is to be included while using the string functions.
8. How to declare a string and character.
14
EXP NO 3 a : PROGRAMS USING POINTERS
Aim:
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
Algorithm :
4. If the content of the address stored in the pointer is required, the indirection operator * is used to
obtain the value.
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.
16
EXP NO 3 b : PROGRAMS USING STRUCTURES
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Declare the structure and mention the structure variables inside the structure.
3. In the main program access the member of the structure using the dot operator.
17
Sample Output :
Result :
Outcome :
Applications:
The C pointer are used to track read /write position in files in Linux/Unix OS.
VIVA - VOCE
18
5. What are the usages of pointers?
6. Why you need pointers in C?
7. What is meant by referencing operator?
8. What are the ways to declare the structure?
9. What is meant by structure operator and arrow
operator? 10.How to pass function to pointers?
19
Expt. No. 4 PROGRAMS INVOLVING DYNAMIC MEMORY
ALLOCATION
Aim:
To write a c program to perform dynamic memory allocation.
Software requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
Algorthim:
1. Start the program.
2. Include the required header file for using the memory allocation function.
3. Declare the required variables for the program.
4. Cal the required Dynamic Memory Allocation function depending upon the program.
5. Display the result.
6. Stop the program.
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.
Applications:
The memory allocation concepts are used in operating systems present in embedded
software .
21
Expt. No. 5 a ARRAY IMPLEMENTATION OF STACK
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Include all the header files which are used in the program and define
a constant 'SIZE' with specific value.
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
perform operation selected by the user on the stack.
PUSH OPERATION
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
1. Check whether stack is EMPTY. (top == -1)
3. If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--)
DISPLAY
3. If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value
and decrement i value by one (i--).
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
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 1
Top--> 45 34 23 12
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 2
Popped element is 45
STACK OPERATION
1.PUSH 2.POP 3.VIEW 4.QUIT
Enter Choice : 3
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
Expt. No. 5 b ARRAY IMPLEMENTATION OF QUEUE
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Include all the header files which are used in the program and define a constant 'SIZE' with specific
value.
3. Declare all the user defined functions which are used in queue implementation.
4. Create a one dimensional array with above defined SIZE (int queue[SIZE])
5. Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -1)
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.
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
both front and rear to '-1' (front = rear = -1).
DISPLAY OPERATION
3. If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
4. Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value is equal
to rear (i <= rear)
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
Enter element to be inserted : 23
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Enter element to be inserted : 34
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Enter element to be inserted : 45
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Enter element to be inserted : 56
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 1
Queue Full
QUEUE OPERATION
1.INSERT 2.DELETE 3.VIEW 4.QUIT Enter Choice : 3
Front--> 12 23 34 45 56 <--Rear
27
Result :
Outcome:
Thus the program for array implementation of stack and queue is attained.
VIVA - VOCE
1. What is meant by data structure?
2. What are the types of data structure?
3. What is meant by stack?
4. What is meant by queue?
5. State the working principle of stack and queue?
6. What is difference between stack and queue?
7. What are the function of stack and queue?
8. What is the task of push and pop()?
9. Give some application of stack and queue?
10. What is stack overflow?
28
Expt. No. 6 a LINKED LIST IMPLEMENTATION OF
STACK
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Include all the header files which are used in the program. And declare all the user defined
functions.
5. Implement the main method by displaying Menu with list of operations and make suitable function
calls in the main method.
PUSH OPERATION
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'.
DISPLAY OPERATION
3. If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
4. Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the
first node in the stack (temp → next != NULL).
30
Sample Output :
Result :
Thus the program for Linked List implementation of Stack is executed successfully.
31
Expt. No. 6 b LINKED LIST IMPLEMENTATION OF QUEUE
Aim:
Software Requirements:
C compiler
Hardware requirements:
Server with supporting 30 terminals
Algorithm :
2. Include all the header files which are used in the program. And declare all the user defined functions.
4. Define two Node pointers 'front' and 'rear' and set both to NULL.
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.
enQueue OPERATION
1. Create a newNode with given value and set 'newNode → next' to NULL.
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'.
DISPLAY OPERATION
3. If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
4. Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to
'rear' (temp → next != NULL).
33
Sample Output
Result :
Thus the program for Linked List implementation of Queue is executed successfully.
Outcome :
Thus the program for Linked List implementation of stack and queue is attained.
Applications:
Thread scheduler which maintains process in the memory .Linked list implementation is used
move running process in queue which is used thread scheduler.
VIVA - VOCE
34
Expt. No. 7 a APPLICATION OF STACKS
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Read all the symbols one by one from left to right in the given Infix Expression.
3. If the reading symbol is operand, then directly print it to the result (Output).
4. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
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.
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.
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.
36
Expt. No. 7 b APPLICATION OF STACKS
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Read all the symbols one by one from left to right in the given Postfix Expression
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.
5. Finally! perform a pop operation and display the popped value as final result.
37
Sample Output :
Result :
Thus the program for performing postfix expression to infix expression is executed successfully.
Outcome :
Applications:
In high level programming language, the arithmetic expressions are calculated. The stacks are
used to convert the expression which is understood by the computer.
VIVA - VOCE
38
Expt. No. 8 IMPLEMENTATION OF TREES, TREE
TRAVERSALS
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
PREORDER TRAVERSAL
2. Process the root node (N).
3. Traverse the left subtree of N (L).
4. Traverse the right subtree of N (R).
INORDER TRAVERSAL
5. Traverse the left subtree of N (L).
6. Process the root node (N).
7. Traverse the right subtree of N (R).
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 :
Result :
Thus the program for performing binary tree traversal is executed successfully.
Outcome :
Thus the program for implementation of Trees, Tree Traversals is attained.
Applications:
Manipulate hierarchical data
Manipulate sorted list of data
As a workflow for computing digital image for visual effects
40
Expt. No. 9 IMPLEMENTATION OF BINARY SEARCH
TREES
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
3. Compare, the search element with the value of root node in the tree.
4. If both are matching, then display "Given node found!!!" and terminate the function
5. If both are not matching, then check whether search element is smaller or larger than that
node value.
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.
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).
5. If newNode is smaller than or equal to the node, then move to its left child. If newNode
is larger than the node, then move to its right child.
6. Repeat the above step until we reach to a leaf node (e.i., reach to NULL).
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.
2. Delete the node using free function (If it is a leaf) and terminate the function.
42
Case 2: Deleting a node with one child
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.
2. If it has two children, then find the largest node in its left subtree (OR)
the smallest node in its right subtree.
3. Swap both deleting node and node which found in above step.
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.
43
Sample Output :
Select an
option 1- insert.
2- Search.
3- Delete.
4 – Display.
5 - Exit
Select an
option 1- insert.
2- Search.
3- Delete.
4 – Display.
5 - Exit
44
Result :
Thus the program for performing Binary Search Tree Operations is executed successfully.
Outcome :
Applications:
Used in search application where data is constantly entered and stored
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
45
Expt. No. 10 a IMPLEMENTATION OF LINEAR SEARCH
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
3. Compare, the search element with the first element in the list.
4. If both are matching, then display "Given element found!!!" and terminate the function
5. If both are not matching, then compare search element with the next element in the list.
6. Repeat steps 3 and 4 until the search element is compared with the last element in the list.
7. If the last element in the list is also doesn't match, then display "Element not found!!!" and terminate
the function.
46
Sample Output :
Result :
47
Expt. No. 10 b IMPLEMENTATION OF BINARY SEARCH
Aim:
Algorithm :
4. Compare, the search element with the middle element in the sorted list.
5. If both are matching, then display "Given element found!!!" and terminate the function
6. If both are not matching, then check whether the search element is smaller or larger than middle
element.
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.
8. If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the right
sublist of the middle element.
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 :
Result :
Outcome :
Applications:
Searching used in information retrieval.
49
Expt. No. 11 a IMPLEMENTATION OF INSERTION SORT
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
2. Asume that first element in the list is in sorted portion of the list and remaining all elements are in
unsorted portion.
3. Consider first element from the unsorted list and insert that element into the sorted list in order
specified.
4. Repeat the above process until all the elements from the unsorted list are moved into the sorted list.
50
Sample Output :
Result :
51
Expt. No. 11 b IMPLEMENTATION OF BUBBLE SORT
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
4. set j=0.
7. Swap the elements stored at arr[j] and arr[j+1] and perform the operations.
8. set j=j + 1.
9. set i= i -1.
10. stop.
52
Sample Output :
Enter the number of elements in the array :
5
2
3
18
21
33
Result :
Thus the program to perform bubble sort is executed successfully.
53
Expt. No. 11 c IMPLEMENTATION OF QUICK SORT
Aim:
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
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].
4. Apply quick sort recursively to a[0] . . . . a[j – 1] and to a[j + 1].........a[n – 1].
54
Sample Output :
2
5
34
40
57
99
Result :
Thus the program to perform Quick sort is executed successfully.
55
Expt. No. 11 d IMPLEMENTATION OF MERGE SORT
Aim:
Algorithm :
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.
56
Sample Output :
Enter the number of elements in the array :
8
2
5
8
29
34
40
57
99
Result :
Outcome :
Thus the implementation of Insertion sort, Bubble sort, Quick sort, Merge sort is attained.
Applications:
Merge sort – Merge sort are used in external database.
Bubble Sort-Used in programming for TV remote application
Heap sort –Used in reading bar codes in plastic cards
Quick Sort –Sports scores are organized by quick sort.
57
Expt. No. 12 a IMPLEMENTATION OF HASH FUNCTION
Aim:
Software Requirements:
C compiler
Hardware Requirements:
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.
4. The element is stored in the hash table where it can be quickly retrieved using hashed
58
Sample Output :
Result :
Thus the program to perform hash function is executed successfully.
59
Expt. No. 12 a COLLISION RESOLUTION TECHNIQUES
Aim:
To write a C program to perform Linear probing.
Software Requirements:
C compiler
Hardware Requirements:
Server with supporting 30 terminals
Algorithm :
1. Start the program.
4. Corresponding to the key, an index will be generated i.e every key is stored in a particular
array index.
5. Using the generated index, access the data located in that array index.
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.
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)
8. To display all the elements of hash table, element at each index is accessed (via for loop).
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
61
Do you want to continue-:(press 1 for yes) 1
Implementation of Hash Table in C with Linear
Probing MENU-:
1. Inserting item in the Hashtable
2. Removing item from the Hashtable
3. Check the size of Hashtable
4. Display Hashtable
Result :
Outcome :
Applications:
Constructs a message Authentication code
Used to construct digital signature
Used in message digest as a cryptographic function
Time stamping
63