DSA Lab File
DSA Lab File
LAB RECORD
DATA STRUCTURES AND ALGORITHMS LAB
199302163
Manan Singh
Code:
#include<stdio.h>
int main()
{
int a[20] , n , b , found;
printf("How many elements in the array? ");
scanf("%d",&n);
read(a , n);
if(found >= 0)
printf("Element found at index %d",found);
else
printf("Element not found");
return 0;
}
Execution:
#include<stdio.h>
int main()
{
int a[20] , n , b , found;
printf("How many elements in the array? ");
scanf("%d",&n);
read(a , n);
if(found >= 0)
printf("Element found at index %d",found);
else
printf("Element not found");
return 0;
}
Execution:
Code:
#include <stdio.h>
int main()
{
int n;
printf("Enter no. of array elements:");
scanf("%d",&n);
int arr[n];
int i;
for(i=0;i<n;i++)
{
printf("Enter element:");
scanf("%d",&arr[i]);
}
int pos;
printf("Enter pos to insert at:");
scanf("%d",&pos);
int ele;
printf("Enter element to insert:");
scanf("%d",&ele);
if(pos>n)
printf("Invalid Input");
else
{
for(i=n-1;i>=pos-1;i--)
arr[i+1]=arr[i];
arr[pos-1]=ele;
return 0;
}
Execution:
#include <stdio.h>
int main()
{
int array[100],position, c, n;
printf("Enter the number of elements of the array:");
scanf("%d", &n);
if(position>=n+1)
printf("\n Deletion not possible.\n");
else
{
for(c=position-1;c<n-1;c++)
array[c]=array[c+1];
Execution:
Program 5: Write a program to sort an array of N integers using selection sort
technique. Write separate functions for reading the array, sorting the array
and to display the elements of the array.
Code :
#include<stdio.h>
int main()
{
int a[20] , n , b , found;
printf("How many elements in the array? ");
scanf("%d",&n);
read(a , n);
selsort(a , n);
display(a , n);
}
Execution:
Program 6: Write a program to sort an array of N integers using bubble sort
technique. Write separate functions for reading the array, sorting the array
and to display the elements of the array.
Code:
#include<stdio.h>
int main()
{
int a[20] , n , b , found;
printf("How many elements in the array? ");
scanf("%d",&n);
read(a , n);
bubsort(a , n);
display(a , n);
}
Execution:
Lab 2 & 3. Two Dimensional Array
Code:
#include <stdio.h>
void read(int m1[][10], int m2[][10], int r1, int c1, int r2, int c2);
void multiply(int m1[][10], int m2[][10], int multResult[][10], int r1, int c1, int r2,
int c2);
void display(int mult[][10], int r1, int c2);
int main()
{
int m1[10][10], m2[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d", &r2, &c2);
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d", &r2, &c2);
}
void multiply(int m1[][10], int m2[][10], int mult[][10], int r1, int c1, int r2, int c2)
{
int i, j, k;
int main()
{
int m1[10][10], trans[10][10], r1, c1, i, j;
read(m1,r1, c1);
return 0;
}
Execution:
Program3: Write a program to find check whether the matrix is upper triangular
or not. Write separate functions for reading the matrix and to check whether the
matrix is upper triangular or not.
Code:
#include<stdio.h>
int main()
{
int m1[10][10],n, i, j,flag;
flag=find_upper(m1,n);
if(flag == 1)
printf(" upper triangular matrix");
else
printf("not upper triangular matrix");
return 0;
}
Execution:
}
return 1;
}
int main()
{
int m1[10][10],n, i, j,flag;
read(m1,n);
flag=find_lower(m1,n);
if(flag == 1)
printf(" lower triangular matrix");
else
printf("not lower triangular matrix");
return 0;
}
Execution:
Lab 4 & 5 : Linked List
Program 1: Write a program to perform following operations on the singly
linked list : Inserting a node (at the start, at the end, in between), deleting a
node (starting node, last node, in between node), displaying information
stored in the nodes. Write separate functions for each of the operations.
Code:
#include<stdio.h>
#include<stdlib.h>
//creating node
struct node {
int data;
struct node *link;
};
if(ptr == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter value\n");
scanf("%d",&data);
ptr->data = data;
ptr->link = head;
head = ptr;
printf("\nNode inserted");
}
}
//inserting a node at the end
void add_at_end() {
struct node *temp,*ptr;
int data;
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter value?\n");
scanf("%d",&data);
temp->data = data;
if(head == NULL) {
temp -> link = NULL;
head = temp;
printf("\nNode inserted");
}
else {
ptr = head;
//
while (ptr -> link != NULL) {
ptr = ptr -> link;
}
ptr->link = temp;
temp->link = NULL;
printf("\nNode inserted");
}
}
}
for(i=0;i<loc;i++) {
ptr = ptr->link;
if(ptr == NULL) {
printf("\ncan't insert\n");
return;
}
}
temp ->link = ptr ->link;
ptr ->link = temp;
printf("\nNode inserted");
}
}
void main () {
while(operation != 8) {
printf("\n\n====== Program 1: Operations on singly linked list ======\n");
printf("\nChoose an operation from the following list :-\n");
printf("\n===============================================\n");
printf("\n1.Insert at begining\n2.Insert at end\n3.Insert at a specific
location\n4.Delete from Beginning\n5.Delete from end\n6.Delete node at a
specific location\n7.Show\n8.Exit\n");
printf("\nEnter your operation\n");
scanf("\n%d",&operation);
switch(operation) {
case 1:
add_at_beg();
break;
case 2:
add_at_end();
break;
case 3:
add_at_pos();
break;
case 4:
del_from_beg();
break;
case 5:
del_from_end();
break;
case 6:
del_from_pos();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid operation..");
}
}
}
Execution:
Program 2: Write a program to perform following operations on the circular
linked list : Inserting a node (at the start, at the end), deleting a node (starting
node, last node, in), displaying information stored in the nodes. Write
separate functions for each of the operations.
Code:
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *prev;
struct node *next;
int data;
};
void insertion_beginning() {
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter Item value");
scanf("%d",&item);
ptr->data=item;
if(head==NULL) {
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else {
temp = head;
while(temp -> next != head) {
temp = temp -> next;
}
temp -> next = ptr;
ptr -> prev = temp;
head -> prev = ptr;
ptr -> next = head;
head = ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last() {
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL) {
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
else {
temp = head;
while(temp->next !=head) {
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
head -> prev = ptr;
ptr -> next = head;
}
}
printf("\nnode inserted\n");
}
void deletion_beginning() {
struct node *temp;
if(head == NULL) {
printf("\n UNDERFLOW");
}
else if(head->next == head) {
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else {
temp = head;
while(temp -> next != head) {
temp = temp -> next;
}
temp -> next = head -> next;
head -> next -> prev = temp;
free(head);
head = temp -> next;
}
}
void deletion_last() {
struct node *ptr;
if(head == NULL) {
printf("\n UNDERFLOW");
}
else if(head->next == head) {
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else {
ptr = head;
if(ptr->next != head) {
ptr = ptr -> next;
}
ptr -> prev -> next = head;
head -> prev = ptr -> prev;
free(ptr);
printf("\nnode deleted\n");
}
}
void display() {
struct node *ptr;
ptr=head;
if(head == NULL) {
printf("\nnothing to print");
}
else {
printf("\n printing values ... \n");
while(ptr -> next != head) {
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
void main () {
int operation =0;
while(operation != 6) {
printf("\n\n====== Program 2: Operations on circular linked list ======\
n");
printf("\nChoose an operation from the following list :-\n");
printf("\n===============================================\n");
printf("\n1.Insert in Beginning\n2.Insert at last\n3.Delete from Beginning\
n4.Delete from last\n5.Show\n6.Exit\n");
printf("\nEnter your operation\n");
scanf("\n%d",&operation);
switch(operation) {
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
deletion_beginning();
break;
case 4:
deletion_last();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("Please enter valid operation..");
}
}
}
Execution:
Program 3: Write a program to perform following operations on the doubly
linked list : Inserting a node (at the start, at the end, in between), deleting a
node (starting node, last node, in between node), displaying information
stored in the nodes. Write separate functions for each of the operations.
Code:
#include<stdio.h>
#include<stdlib.h>
struct node {
struct node *prev;
struct node *next;
int data;
};
void insertion_beginning() {
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL) {
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else {
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last() {
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL) {
printf("\nOVERFLOW");
}
else {
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL) {
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else {
temp = head;
while(temp->next!=NULL) {
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified() {
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL) {
printf("\n OVERFLOW");
}
else {
temp=head;
printf("Enter location after which we insert:");
scanf("%d",&loc);
for(i=0;i<loc;i++) {
temp = temp->next;
if(temp == NULL) {
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning() {
struct node *ptr;
if(head == NULL) {
printf("\n UNDERFLOW");
}
else if(head->next == NULL) {
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else {
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last() {
struct node *ptr;
if(head == NULL) {
printf("\n UNDERFLOW");
}
else if(head->next == NULL) {
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else {
ptr = head;
if(ptr->next != NULL) {
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified() {
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL) {
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL) {
ptr ->next = NULL;
}
else {
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display() {
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL) {
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void main () {
int operation =0;
while(operation != 6) {
printf("\n\n====== Program 3: Operations on doubly linked list ======\
n");
printf("\nChoose an operation from the following list :-\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n5.Delete from last\n6.Delete the node
after the given data\n7.Show\n8.Exit\n");
printf("\nEnter your operation?\n");
scanf("\n%d",&operation);
switch(operation) {
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("Please enter valid operation..");
}
}
}
Execution:
Lab 6 & 7 : Stack
Program 1: Write a program to implement stack using array. Write separate
functions for the following operations on stack: Push (inserting element), Pop
(deleting element).
Code:
#include<limits.h>
#include<stdio.h>
#include<stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
int main(){
struct Stack* stack = createStack(100);
push(stack, 45);
push(stack, 67);
printf("%d popped from stack\n", pop(stack));
return 0;
}
Execution:
Code:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct StackNode {
int data;
struct StackNode* next;
};
int main(){
struct StackNode* root = NULL;
push(&root, 73);
push(&root, 48);
printf("%d popped from stack\n", pop(&root));
return 0;
}
Execution:
Program 3: Write a program to convert an expression from infix notation to
postfix notation.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Stack{
int top;
unsigned capacity;
int* array;
};
int main(){
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
Execution:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// Stack type
struct Stack{
int top;
unsigned capacity;
int* array;
};
// Stack Operations
struct Stack* createStack( unsigned capacity ){
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
if (!stack) return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
if (!stack->array) return NULL;
return stack;
}
int main(){
char exp[] = "243*+31-";
printf ("postfix evaluation: %d", evaluatePostfix(exp));
return 0;
}
Execution:
Lab 8 : Queue
Program 1: Write a program to implement simple queue using array. Write
separate functions for the following operations on queue : Enqueue (inserting
element), Dequeue (deleting element).
Code:
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
void main () {
int operation;
while(operation != 4) {
printf("\n\n====== Program 1: Operations on singly linked list ======\n");
printf("\nChoose an operation from the following list :-\n");
printf("\n===============================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\
n4.Exit\n");
printf("\nEnter your operation\n");
scanf("%d",&operation);
switch(operation) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid operation??\n");
}
}
}
void insert() {
int data;
printf("\nEnter the element\n");
scanf("\n%d",&data);
if(rear == maxsize-1) {
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1) {
front = 0;
rear = 0;
}
else {
rear = rear+1;
}
queue[rear] = data;
printf("Value inserted ");
}
void delete() {
int data;
if (front == -1 || front > rear) {
printf("\nUNDERFLOW\n");
return;
}
else {
data = queue[front];
if(front == rear) {
front = -1;
rear = -1 ;
}
else {
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display() {
int i;
if(rear == -1) {
printf("\nEmpty queue\n");
}
else {
printf("\nprinting values:\n");
for(i=front;i<=rear;i++) {
printf("%d\n",queue[i]);
}
}
}
Execution:
Program 2: Write a program to implement circular queue using linked list.
Write separate functions for the following operations on queue : Enqueue
(inserting element), Dequeue (deleting element).
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void enqueue(int x) {
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
if(rear==-1) {
front=rear=newnode;
rear->next=front;
}
else {
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
void dequeue() {
struct node *temp;
temp=front;
if((front==-1)&&(rear==-1)) {
printf("\nQueue is empty");
}
else if(front==rear) {
front=rear=-1;
free(temp);
}
else {
front=front->next;
rear->next=front;
free(temp);
}
}
int peek() {
if((front==-1) &&(rear==-1)) {
printf("\nQueue is empty");
}
else {
printf("\nThe front element after dequeue operation is %d", front->data);
}
}
void display() {
struct node *temp;
temp=front;
printf("\nThe elements in a Queue are : ");
if((front==-1) && (rear==-1)) {
printf("Queue is empty");
}
else {
while(temp->next!=front) {
printf("%d,", temp->data);
temp=temp->next;
}
printf("%d", temp->data);
}
}
void main() {
enqueue(98);
enqueue(666);
enqueue(37);
display();
dequeue();
peek();
}
Execution:
Lab 9 & 10 : Trees
Program 1: Write a program to implement binary search tree. Write separate functions for
each of the following operations on binary search tree: Creating Binary Search
Tree, Inserting a node in the tree, Deleting a node from the tree.
Code:
#include <stdio.h>
#include <stdlib.h>
node;
typedef struct binary_search_tree {
node *root;
}
binary_search_tree;
binary_search_tree* new_binary_search_tree() {
binary_search_tree *t = malloc(sizeof(binary_search_tree));
t->root = NULL;
return t;
}
int main() {
binary_search_tree *t = new_binary_search_tree();
node *a, *b, *c, *d, *e, *f, *g, *h, *i, *j, *k, *l, *m;
a = new_node(15);
b = new_node(200);
c = new_node(30);
d = new_node(100);
e = new_node(90);
f = new_node(45);
g = new_node(500);
h = new_node(60);
i = new_node(1);
j = new_node(10);
k = new_node(150);
l = new_node(250);
m = new_node(120);
insert(t, a);
insert(t, b);
insert(t, c);
insert(t, d);
insert(t, e);
insert(t, f);
insert(t, g);
insert(t, h);
insert(t, i);
insert(t, j);
insert(t, k);
insert(t, l);
insert(t, m);
delete(t, e);
delete(t, f);
inorder(t, t->root);
return 0;
}
Execution:
Program 2: Write a program to traverse a binary search tree in pre-order, post-
order and in-order.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
Node* left, *right;
};
int main() {
Node* root = init_tree(10);
root->left = create_node(20);
root->right = create_node(30);
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");
print_tree(PREORDER, root);
printf("\n\n");
printf("----Inorder Traversal:----\n");
print_tree(INORDER, root);
printf("\n\n");
printf("----Postorder Traversal:----\n");
print_tree(POSTORDER, root);
printf("\n\n");
free_tree(root);
return 0;
}
Execution:
LAB 11 & 12 – GRAPHS
Program 1: Write a program to find the transitive closure of a directed graph.
Code:
#include <stdio.h>
#include <conio.h>
int n,a[10][10],p[10][10];
void path() {
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
p[i][j]=a[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(p[i][k]==1&&p[k][j]==1) p[i][j]=1;
}
void main() {
int i,j;
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
path();
printf("\nThe path matrix is showm below\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++)
printf("%d ",p[i][j]);
printf("\n");
}
getch();
}
Execution:
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main() {
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("Enter the adjacency matrix:");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) {
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("n");
while(ne<n) {
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0) {
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0) {
printf("Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("Minimun cost=%d",mincost);
getch();
}
Execution:
struct node {
int vertex;
struct node* next;
};
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;
}
Execution:
LAB 13 – SORTING
int main() {
int array[] = {29, 50, 19, 4, 68, 33};
int length = sizeof(array)/sizeof(array[0]);
printf("Before Sorting the array: ");
print_Array(array, length);
printf("\n");
printf("After Sorting the array: ");
quick_sort(array, 0, length-1);
print_Array(array, length);
return 0;
}
Execution:
int main() {
int arr[] = {53, 82, 15, 2, 46};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array before sorting:\n");
printArray(arr, n);
heapSort(arr, n);
printf("Array after sorting is:\n");
printArray(arr, n);
}
Execution: