LDS Lab Manual
LDS Lab Manual
(DEEMED TO BE UNIVERSITY)
COLLEGE OF ENGINEERING, PUNE
Lab Manual
LINEAR DATA STRUCTURES
3. To practice and promote professional ethics, transparency and accountability for social community,
economic and environmental conditions.
To be focused on innovative and quality education in computer science and engineering that
prepares professionals for development of society.
C Programming is an ANSI/ISO standard and powerful programming language for developing real-time
applications. C programming language was invented by Dennis Ritchie at the Bell Laboratories in 1972. It
was invented for implementing UNIX operating system. C is most widely used programming language
even today. All other programming languages were derived directly or indirectly from C programming
concepts.
Deletion
Traversal.
Merge Sort
Examination Scheme
Practical Exam is conducted at the end of semester.
Marking Scheme
Practical: 01 Credit
Laboratory Usage
Laboratory of data structures and algorithms consist of 20 machines with following configuration.
Software Requirements
System configuration:
2GB of RAM,
Practical Pre-requisite
Students should have knowledge of Fundamentals of data types and programming concepts.
The students of B.TECH. (Computer Science and Engineering), after graduating will able to,
CO-PO mapping
PO a b c d e f g h i j k l
CO1 2 2 1
CO2 1 2 2
CO3 2 1 2
CO4 2 2 1
CO5 2 1 2 1
CO6 2 2 1 2
Understand the
fundamentals and analysis
of algorithms
Problem definition
Implement Linear data
2,3,4 1,2,2,3 &Performing Practical and
structures
reporting results
The students learn both soft skills and technical skills while they are undergoing the practical sessions.
The soft skills gained in terms of communication, presentation and behavior. While technical skills they
gained in terms of C programming .
Deletion
Singly Linked Lists
Preorder
Inorder and
Algorithm:
Postorder
1. Traversal
The process of accessing and displaying all elements in the array.Algorithm: Traverse(array, n)
5 1. Start
Write a program for Implementation of following Graph Algorithms:
2. For i=0 to n−1 do: 6th Week
BFS Algorithms. 7th Week
Print array[i]
DFS Algorithms.
3. End
Experiment: Implement shortest path algorithms and measure execution
time.
2.6 Insertion
Write a program for Implementation of Array Operations:
8th Week
Searching (Linear Search). 9th Week 11
Lab Manual: Linear Data Structures
Searching ( Binary Search).
array[i+1]=array[i]
4. Set array[position]=valuearray[position]
5. Increment n.
6. End
3. Deletion
Deleting an element from a specified position involves shifting elements to the left to fill the gap.
Algorithm: Delete(array, n, position)
1. Start
2. If position<0 or position≥n:
array[i]=array[i+1]
4. Decrement n.
5. End
Time complexity-
Traversal: O(n)
Insertion: O(n)in the worst case.
// Initial array
arr[0] = 1; arr[1] = 2; arr[2] = 3; arr[3] = 4; arr[4] = 5;
// Traverse the array
printf("Original Array:\n");
traverse(arr, n);
// Insert an element at position 2
printf("Inserting 99 at position 2:\n");
insert(arr, &n, 2, 99);
traverse(arr, n);
// Delete an element at position 3
printf("Deleting element at position 3:\n");
deleteElement(arr, &n, 3);
traverse(arr, n);
return 0;
}
Questions:
When we want to work with an unknown number of data values, we use a linked list data structure to
organize that data. The linked list is a linear data structure that contains a sequence of elements such
that each element links to its next element in the sequence. Each element in a linked list is called
"Node". Linked List can be implemented as 1. Singly Linked List 2. Doubly Linked List 3. Circular
Linked List Single Linked List Simply a list is a sequence of data, and the linked list is a sequence of
data linked with each other. The formal definition of a single linked list is as follows... In any single
linked list, the individual element is called as "Node". Every "Node" contains two fields, data field, and
the next field. The data field is used to store actual value of the node and next field is used to store the
The following operations are performed on a Single Linked List ,Doubly Linked Lists,
1. Creation
2. Insertion
4. Display
1.Creation
Step 1 - Define a Node structure with two members data and next
2.Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
We can use the following steps to insert a new node at beginning of the single linked list...
Step 4 - If it is Not Empty then, set newNode→next = head and head = newNode.
We can use the following steps to insert a new node at end of the single linked list...
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
We can use the following steps to insert a new node after a node in the single linked list...
Step 3 - If it is Empty then, set newNode → next = NULL and head = newNode.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here
location is the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp is reached to last node or not. If it is reached to
last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
Step 7 - Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'
3. Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
We can use the following steps to delete a node from beginning of the single linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate
the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp → next == NULL)
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE then set head = temp → next, and delete temp.
We can use the following steps to delete a node from end of the single linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
Step 4 - Check whether list has only one Node (temp1 → next == NULL)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate the
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list.
We can use the following steps to delete a specific node from the single linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its
next node.
Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Step 6 - If it is reached to the exact node which we want to delete, then check whether list
Step 7 - If list has only one node and that is the node to be deleted, then
Step 8 - If list contains multiple nodes, then check whether temp1 is the first node in the
Step 9 - If temp1 is the first node then move the head to the next node (head = head →
Step 10 - If temp1 is not first node then check whether it is last node in the list
Step 11 - If temp1 is last node then set temp2 → next = NULL and
Step 12 - If temp1 is not first node and not last node then set temp2 → next = temp1 →
We can use the following steps to display the elements of a single linked list...
Step 2 - If it is Empty then, display 'List is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last
node
Step 5 - Finally display temp → data with arrow pointing to NULL (temp → data ---> NULL).
Double linked list is a sequence of elements in which every element has links to its previous
1.Creation
Step 1 - Define a Node structure with two members data and next
2.Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
We can use the following steps to insert a new node at beginning of the double linked list...
Step 1 - Create a newNode with given value and newNode → previous as NULL.
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.
We can use the following steps to insert a new node at end of the double linked list...
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
We can use the following steps to insert a new node after a node in the double linked list...
Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and
Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here
location is the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to
the last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp1 to next node.
3.Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
We can use the following steps to delete a node from beginning of the double linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp → previous is equal to
temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty
list conditions)
We can use the following steps to delete a node from end of the double linked list...
Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list has only one Node (temp → previous and
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in thelist. (until temp →
We can use the following steps to delete a specific node from the double linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the
last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in the list!
Step 6 - If it is reached to the exact node which we want to delete, then check whether list
Step 7 - If list has only one node and that is the node which is to be deleted then
Step 8 - If list contains multiple nodes, then check whether temp is the first node in the
Step 9 - If temp is the first node, then move the head to the next node (head = head →
delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in the list
Step 11 - If temp is the last node then set temp of previous of next to NULL
Step 12 - If temp is not the first node and not the last node, then set temp of previous
4.Displaying
We can use the following steps to display the elements of a double linked list...
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 5 - Keep displaying temp → data with an arrow (<===>) until temp reaches to the
last node
A circular linked list is a sequence of elements in which every element has a link to its next
element in the sequence and the last element has a link to the first element.
1.Creation
Step 1 - Define a Node structure with two members data and next
2.Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as
follows...
We can use the following steps to insert a new node at beginning of the circular linked list...
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until
Step 6 - Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
We can use the following steps to insert a new node at end of the circular linked list...
Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
We can use the following steps to insert a new node after a node in the circular linked list...
Step 3 - If it is Empty then, set head = newNode and newNode → next = head.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → data is equal to location, here
location is the node value after which we want to insert the newNode).
Step 6 - Every time check whether temp is reached to the last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
Step 7 - If temp is reached to the exact node after which we want to insert the newNode
Step 8 - If temp is last node then set temp → next = newNode and newNode →
next = head.
Step 9 - If temp is not last node then set newNode → next = temp → next and temp →
next = newNode.
In a circular linked list, the deletion operation can be performed in three ways those are as
follows...
We can use the following steps to delete a node from beginning of the circular linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
Step 4 - Check whether list is having only one node (temp1 → next == head)
Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list
conditions)
Step 6 - If it is FALSE move the temp1 until it reaches to the last node.
Step 7 - Then set head = temp2 → next, temp1 → next = head and delete temp2.
We can use the following steps to delete a node from end of the circular linked list...
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
Step 4 - Check whether list has only one Node (temp1 → next == head)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until temp1 reaches to the last node in the list. (until temp1 →
next == head)
We can use the following steps to delete a specific node from the circular linked list...
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the
last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its
next node.
Step 5 - If it is reached to the last node then display 'Given node not found in the list!
Step 6 - If it is reached to the exact node which we want to delete, then check whether list
Step 7 - If list has only one node and that is the node to be deleted then
Step 8 - If list contains multiple nodes then check whether temp1 is the first node in the
Step 9 - If temp1 is the first node then set temp2 = head and keep moving temp2 to its
next node until temp2 reaches to the last node. Then set head = head →
Step 10 - If temp1 is not first node then check whether it is last node in the list
Step 1 1- If temp1 is last node then set temp2 → next = head and
Step 12 - If temp1 is not first node and not last node then set
We can use the following steps to display the elements of a circular linked list...
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Keep displaying temp → data with an arrow (--->) until temp reaches to the last
Step 5 - Finally display temp → data with arrow pointing to head → data.
Program:
Write any applicable code.
Questions:
3. Can you explain how memory allocation works for linked lists?
6. How do you delete a node from a linked list (head, tail, or specific position)?
Experiment No: 3
Write a program for Stack and Queue Implementation:
Stack Implementation using arrays and linked lists.
Introduction:
Stack -
That means, a new element is added at top of the stack and an element is removed from the top of the
stack. In stack, the insertion and deletion operations are performed based on LIFO(Last In First Out)
principle.
"A Collection of similar data items in which both insertion and deletion operations are
performed based on LIFO principle".
Operations on a Stack -
Stack data structure can be implemented in two ways. They are as follows...
1. Using Arrays
A stack data structure can be implemented using a one-dimensional array. But stack implemented
using array stores only a fixed number of data values. This implementation is very simple. Just define
a one dimensional array of specific size and insert or delete the values into that array by using LIFO
principle with the help of a variable called 'top'. Initially, the top is set to -1. Whenever we want to
insert a value into the stack, increment the top value by one and then insert. Whenever we want to
delete a value from the stack, then delete the top value and decrement the top value by one.
Before implementing actual operations, first follow the below steps to create an empty stack.
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is
always inserted at top position. Push function takes one integer value as parameter and
inserts that value into the stack.
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
3.
In a stack,pop() is a function used to delete an element from the stack. In a stack, the
We can use the following steps to pop an element from the stack…
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top.
#include<iostream.h>
#include<conio.h>
#define SIZE 10
void push(int);
void display();
void main()
clrscr();
while(1){
cin>>choice;
switch(choice)
cin>>value;
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
if(top == SIZE-1)
else
top++;
stack[top] = value;
cout<<"\nInsertion success!!!";
void pop()
if(top == -1)
else
cout<<"\nDeleted :"<<stack[top]);
top--;
if(top == -1)
cout<<"\nStack is Empty!!!";
else
int i;
cout<<stack[i];
The major problem with the stack implemented using an arrays is, it works only for a fixed number of
data values. That means the amount of data must be specified at the beginning of the
implementation itself. Stack implemented using an array is not suitable, when we don't know the
size of data which we are going to use. A stack data structure can be implemented by using a linked
list data structure.
The stack implemented using linked list can work for an unlimited number of values. That means,
stack implemented using linked list works for the variable size of data. So, there is no need to fix the
size at the beginning of the implementation. The Stack implemented using linked list can organize
as many data values as we want. In linked list implementation of a stack, every new element is
inserted as 'top' element. That means every newly inserted element is pointed by 'top'. Whenever
we want to remove an element from the stack, simply remove the node which is pointed by 'top' by
moving 'top' to its previous node in the list. The next field of the first element must be always NULL
We can Perform the Following Operations on Stack Using Linked List (i.e)
1. Push()
2. Pop()
3. Display()
To implement a stack using a linked list, we need to set the following things before
implementing actual operations.
Step 1 - Include all the header files which are used in the program. And declare all
Step 2 - Define a 'Node' structure with two members data and next.
Step 4 - Implement the main method by displaying Menu with list of operations and make suitable
function calls in the main method.
We can use the following steps to insert a new node into the stack...
We can use the following steps to delete a node from the stack...
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the
function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
We can use the following steps to display the elements (nodes) of a stack...
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.Step 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).
Questions:
Experiment No: 4
Write a program for Stack and Queue Implementation:
Queue Implementation using arrays and linked lists.
1.enQueue()
2.deQueue()
3.Display()
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1 - Include all the header files which are used in the program and define a
Step 2 - Declare all the user defined functions which are used in queue
implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int
queue[SIZE]).
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'.
Step 5 - Then implement main method by displaying menu of operations list and make suitable
function calls to perform operation selected by the user on queue.
In a queue data structure, enQueue() is a function used to insert a new element into the queue. In a
queue, the new element is always inserted at rear position. The enQueue() function takes one integer
value as a parameter and inserts that value into the queue. We can use the following steps to insert an
Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and
In a queue data structure, deQueue() is a function used to delete an element from the queue. In a queue,
the element is always deleted from front position. The deQueue() function does not take any value as
parameter. We can use the following steps to delete an element from the queue…
Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++). Then
both front and rear are equal (front == rear), if it TRUE, then set
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value
reaches to rear (i <= rear).
To implement queue using linked list, we need to set the following things before implementing
actual operations.
Step 1 - Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2 - Define a 'Node' structure with two members data and next.
Step 3 - Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4 - 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.
We can use the following steps to insert a new node into the queue…
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode.
We can use the following steps to delete a node from the queue…
Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from
the function
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)).
We can use the following steps to display the elements (nodes) of a queue...
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until
#include<iostream.h>
#include<conio.h>
struct Node
int data;
void insert(int);
void delete();
void display();
void main()
clrscr();
while(1){
switch(choice){
cin>>value;
insert(value);
break;
case 4: exit(0);
newNode->data = value;
if(front == NULL)
else{
rear = newNode;
cout<<"\nInsertion is Success!!!\n";
void delete()
if(front == NULL)
cout<<"\nQueue is Empty!!!\n";
else{
free(temp);
void display()
if(front == NULL)
cout<<"\nQueue is Empty!!!\n";
else{
while(temp->next != NULL){
cout<<temp->data;
cout<<temp->data;
Introduction:
Searching is the fundamental process of locating a specific element or item within a collection of data.
This collection of data can take various forms, such as arrays, lists, trees, or other structured
representations.The primary objective of searching is to determine whether the desired element exists
within the data, and if so, to identify its precise location or retrieve it. It plays an important role in
various computational tasks and real-world applications, including information retrieval, data analysis,
decision-making processes, and more.
Linear Search: In this simple algorithm, each element in the collection is sequentially checked until the
desired item is found, or the entire list is traversed. It is suitable for small-sized or unsorted lists, but its
time complexity is O(n) in the worst case.
Binary Search: This algorithm is applicable only to sorted lists. It repeatedly compares the middle
element of the list with the target element and narrows down the search range by half based on the
comparison result. Binary search has a time complexity of O(log n), making it highly efficient for large
sorted lists.
Program:
(ii)Binary Search:
Before searching, the list of items should be sorted in ascending order. First compare the key value
with the item in the mid position of the array. If there is a match, we can return immediately the
position. if the value is less than the element in middle location of the array, the required value is lie in
the lower half of the array. If the value is greater than the element in middle location of the array, the
required value is lie in the upper half of the array. We repeat the above procedure on the lower half or
upper half of the array.
Algorithm:
Questions:
Experiment No:6
Write a program for Implementation of following Sorting Algorithms:
Bubble Sort
Merge Sort
Quick Sort
Introduction:
Sorting refers to rearrangement of a given array or list of elements according to a comparison operator
on the elements. The comparison operator is used to decide the new order of elements in the
respective data structure.
The bubble sort is an example of exchange sort.In this method,repetitive comparisonis performed
among elements and essential swapping of elements is done. Bubble sort is commonly used in sorting
algorithms. It is easy to understand but time consuming i.e. takes more number of comparisons to
sort a list. In this type, two successive elements are comparedand swapping is done.Thus,step-by-step
entire array elements are checked. It is different from the selection sort. Instead of searching the
minimum element and then applying swapping, two records are swapped instantly upon noticing that
they are not in order.
Algorithm:
Bubble_Sort ( A [ ] , N )
Step 1: Start
Step 2: Take an array of n elements
Step 3: for i=0,………….n-2
Step 4: for j=i+1,…….n-1
Step 5: if arr[j]>arr[j+1] then
Interchange arr[j] and arr[j+1]
End of if
Step 6: Print the sorted array arr
Step 7:Stop
Program:
#include <stdio.h>
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
bubbleSort(data, size);
Merge Sort is like a wise sage who knows that the best way to tackle a problem is by breaking it down.
It is a recursive method, which means it calls itself to solve smaller parts of a larger problem. Imagine
splitting a deck of cards into halves, over and over, until you have piles so small they're already sorted.
Here's a simple way to visualize Merge Sort:
Divide: Split your array of data in half, again and again, until each piece can't be divided further
(you'll end up with arrays of just one element).
Conquer: Now, with each piece being sorted (since it's just one element), you start combining them.
Merge: As you combine these pieces, you sort them along the way. Think of merging two small
stacks of cards where each stack is already in order.
This process continues until all the small, sorted pieces are merged back into a single, sorted array.
Algorithm
Program:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high){
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high){
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main(){
int i;
printf("Array before sorting");
for(i = 0; i <= max; i++)
Lab Manual: Linear Data Structures 55
printf("%d ", a[i]);
sort(0, max);
printf("Array after sorting");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
It is a divide and conquer algorithm. Quick sort first divides a large array into two smaller sub-arrays:
the low elements and the high elements. Quick sort can then recursively sort the sub-arrays.
ALGORITHM:
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int intArray[MAX] = { 4,6,3,2,1,9,7};
int main() {
printf("Input Array: ");
display();
Lab Manual: Linear Data Structures 57
printline(50);
quickSort(0, MAX - 1);
printf("Output Array: ");
display();
printline(50);
}
Questions:
Experiment No:7
Write a program for Implementation direct access file:
Introduction -
A Direct Access File allows random access to records using a hash function. Unlike sequential files,
where records are accessed one by one, direct access files enable efficient record management by
mapping keys to specific file locations.
Hash Function:
A mathematical function that computes the address of a record in the file based on its key.
Address=key%Total Records
Record Size:
Each record occupies a fixed amount of space in the file to ensure consistency during access.
Algorithm
1. Write Record
Input: Key, Data
Output: Record written to the file
3. Update Record
Input: Key, New Data
Output: Confirmation of update
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (strlen(record) > 0) {
printf("Record found: %s\n", record);
} else {
printf("Record not found.\n");
}
}
if (strlen(record) > 0) {
fseek(file, location, SEEK_SET);
strncpy(record, new_data, RECORD_SIZE - 1);
fwrite(record, RECORD_SIZE, 1, file);
printf("Record with key %d updated successfully.\n", key);
} else {
printf("Record not found for update.\n");
}
fclose(file);
}
while (1) {
printf("\nDirect Access File Operations:\n");
printf("1. Write Record\n");
printf("2. Read Record\n");
printf("3. Update Record\n");
printf("4. Delete Record\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter key (integer): ");
scanf("%d", &key);
printf("Enter data: ");
scanf("%s", data);
write_record(key, data);
break;
case 2:
printf("Enter key (integer): ");
Lab Manual: Linear Data Structures 62
scanf("%d", &key);
read_record(key);
break;
case 3:
printf("Enter key (integer): ");
scanf("%d", &key);
printf("Enter new data: ");
scanf("%s", data);
update_record(key, data);
break;
case 4:
printf("Enter key (integer): ");
scanf("%d", &key);
delete_record(key);
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
}
Questions: