DS Practical File
DS Practical File
1. Functions •
WAP to create a function to swap two numbers using call by value.
•
WAP to implement call by reference for swapping of two numbers.
2. Arrays •
WAP to create an array.
•
WAP to insert a new number in an array at any location
•
WAP to delete any number from an array
3. Searching •
WAP to search an element into an array using linear search
•
WAP for finding the element in an array using binary search method
using iteration and recursion.
4. Sorting WAP that implements the following sorting: -
• Insertion Sort
• Selection Sort
• Bubble Sort
• Merge Sort
• Quick Sort
5. Stack and Queue • WAP to implement stack using array.
using Array • WAP to implement queue using array.
6. Linked List • WAP to create a linked list & perform operations such as insert, delete
and reverse in the linked list.
7. Stack and Queue • WAP to implement stack using linked list.
using Linked • WAP to implement queue using linked list
List
Aim- WAP to create a function to swap two numbers using call by value
#include <stdio.h>
int main()
swapx(a, b);
return 0;
int t;
t = x;
x = y;
y = t;
}
Practical 2
#include <stdio.h>
int main()
swapx(&a, &b);
return 0;
int t;
t = *x;
*x = *y;
*y = t;
}
Practical 3
#include <stdio.h>
int main () {
int i,j;
return 0;
}
Practical 4
#include <stdio.h>
int main()
int arr[100] = { 0 };
arr[i] = i + 1;
printf("\n");
// element to be inserted
x = 50;
pos = 5;
n++;
// insert x at pos
arr[pos - 1] = x;
// print the updated array
printf("\n");
return 0;
}
Program-5
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &position);
else
array[c] = array[c+1];
printf("%d\n", array[c]);
return 0;
}
Program-6
// C code to linearly search x in arr[]. If x is present then return its location, otherwise
return -1
#include <stdio.h>
int i;
if (arr[i] == x)
return i;
return -1;
int main(void)
int x = 10;
// Function call
(result == -1)
return 0;
}
Program-7
Aim- WAP for finding the element in an array using binary search method using iteration and
recursion
(A) Iteration
#include <stdio.h>
while (l <= r)
int m = l + (r-l)/2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
return -1;
}
int main(void)
int x = 10;
return 0;
(B) Recursion
#include <stdio.h>
// A recursive binary search function. It returns location of x in
// given array arr[l..r] is present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
// If the element is present at the middle itself
if (arr[mid] == x) return mid;
// If element is smaller than mid, then it can only be present
// in left subarray
if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
// Else the element can only be present in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not present in array
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Program-9
Aim- WAP to implement of insertion sort
// C program for insertion sort
#include <math.h>
#include <stdio.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
return 0;
}
Program-9
Aim- WAP to implement of Selection sort
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j, min_idx;
min_idx = i;
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
/* Function to print an array */
int i;
printf("\n");
int main()
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printArray(arr, n);
return 0;
}
Program-10
Aim- WAP to implement of Bubble sort
// C program for implementation of Bubble sort
#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
b[i++] = a[l1++];
b[i++] = a[l2++];
a[i] = b[i];
int mid;
sort(mid+1, high);
} else {
return;
int main() {
int i;
sort(0, max);
}
Program-12
Aim- WAP to implement of Quick sort
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&arr[i], &arr[j]);
}
return (i + 1);
quickSort(arr, pi + 1, high);
// Driver code
int main()
{
int arr[] = { 10, 7, 8, 9, 1, 5 };
// Function call
quickSort(arr, 0, N - 1);
return 0;
}
Program- 13
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
}
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
}
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
}
Program- 14
#include<stdio.h>
#define n 5
int main()
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
while(ch)
scanf("%d",&ch);
switch(ch)
case 1:
if(rear==x)
else
scanf("%d",&queue[rear++]);
break;
case 2:
if(front==rear)
else
x++;
break;
case 3:
if(front==rear)
else
printf("%d",queue[i]);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
return 0;
}
Program- 15
Aim- WAP to create a linked list & perform operations such as insert, delete and reverse in the
linked list.
(A) Creation
// Linked list implementation in C
#include <stdio.h>
#include <stdlib.h>
// Creating a node
struct node {
int value;
struct node *next;
};
int main() {
// Initialize nodes
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
// Allocate memory
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
// Assign value values
one->value = 1;
two->value = 2;
three->value = 3;
// Connect nodes
one->next = two;
two->next = three;
three->next = NULL;
// printing node-value
head = one;
printLinkedlist(head);
}
(B) Insert
// A complete working C program to demonstrate all insertion methods
// on Linked List
#include <stdio.h>
#include <stdlib.h>
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
return 0;
}
(C) Deletion
// C code to delete a node from linked list
#include <stdio.h>
#include <stdlib.h>
// Drivers code
int main()
{
Node* list = malloc(sizeof(Node));
list->next = NULL;
Push(&list, 1);
Push(&list, 2);
Push(&list, 3);
printList(list);
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
}*top,*top1,*temp;
int count = 0;
if (top == NULL)
top->ptr = NULL;
top->info = data;
else
temp->ptr = top;
temp->info = data;
top = temp;
count++;
printf("Node is Inserted\n\n");
int pop() {
top1 = top;
if (top1 == NULL)
printf("\nStack Underflow\n");
return -1;
else
top1 = top1->ptr;
free(top);
top = top1;
count--;
return popped;
void display() {
// Display the elements of the stack
top1 = top;
if (top1 == NULL)
printf("\nStack Underflow\n");
return;
printf("%d--->", top1->info);
top1 = top1->ptr;
printf("NULL\n\n");
int main() {
while (1) {
switch (choice) {
case 1:
scanf("%d", &value);
push(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
Program-17
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Queue {
unsigned capacity;
int* array;
};
// of given capacity.
sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (int*)malloc(
queue->capacity * sizeof(int));
return queue;
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)
% queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
if (isEmpty(queue))
return INT_MIN;
queue->front = (queue->front + 1)
% queue->capacity;
queue->size = queue->size - 1;
return item;
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}
// Function to get rear of queue
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
int main()
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
dequeue(queue));
return 0;
}
Program- 18
#include <stdio.h>
#include <stdlib.h>
struct node {
};
return temp;
// searching operation
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
else //x is smaller than the data, so we will search the left subtree
// insertion
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
return root;
if (root == NULL)
return NULL;
else if (root -> left_child != NULL) // node with minimum value will have no left child
return root;
// deletion
if (root == NULL)
return NULL;
else {
free(root);
return NULL;
else
free(root);
return temp;
//Two Children
else {
return root;
// Inorder Traversal
}
}
int main() {
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
inorder(root);
printf("\n");
return 0;
}
Program- 19
(A) BFS
// BFS algorithm in C
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
// BFS algorithm
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
// Creating a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Creating a graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}
(B) DFS
// DFS algorithm in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
int* visited;
// DFS algo
void DFS(struct Graph* graph, int vertex) {
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
if (graph->visited[connectedVertex] == 0) {
DFS(graph, connectedVertex);
}
temp = temp->next;
}
}
// Create a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Create graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
// Add edge
void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
int main() {
struct Graph* graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);
printGraph(graph);
DFS(graph, 2);
return 0;
}