Dsa File
Dsa File
6th semester
BACHELORS OF TECHNOLOGY
IN
Electronics and communication
Engineering
PROGRAM CODE:
#include <stdio.h>
#include <conio.h>
void insert_ele(int i, int arr[], int len)
{
int ele, loc;
printf("\n enter the element to be inserted \n");
scanf("%d", &ele);
printf("\n enter the location\n");
scanf("%d", &loc);
if (i == 19)
{
printf("\nOverflow\n");
}
else
{
while (i >= loc)
{
arr[i + 1] = arr[i];
i--;
}
}
arr[loc] = ele;
len++;
{
printf("\n Underflow");
}
else
{
for (i = loc; i < len; i++)
{
arr[i] = arr[i + 1];
}
len--;
printf("\n new array is\n");
for (i = 0; i < len; i++)
{
printf("\t%d ", arr[i]);
}
}
}
void main()
{
int arr[20], i = 0, len = 0, ch;
char choice;
printf("\nEnter number of elements you want to insert: ");
scanf("%d", &len);
for (i = 0; i < len; i++)
{
printf("enter the element %d ", i);
scanf("%d", &arr[i]);
}
i--;
label:
printf("\n 1.To insert element,2.To delete \n");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert_ele(i, arr, len);
break;
case 2:
delete_ele(i, arr, len);
break;
default:
printf("\n Invalid operation\n");
break;
}
printf("\n press y to continue or any other key to quit : ");
scanf("%s", &choice);
if (choice = 'y' || choice == 'Y')
{
goto label;
}
}
OUTPUT:
EXPERIMENT – 02
PROGRAM CODE:
#include <stdio.h>
void main()
{
int matrix[10][10];
int i, j, m, n;
int sparse_counter = 0;
printf("enter the orer of matrix : \n");
scanf("%d%d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &matrix[i][j]);
if (matrix[i][j] = 0)
{
++sparse_counter;
}
}
}
if (sparse_counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix!!!\n");
}
else
{
printf("The given matrix is not a sparse matrix\n");
}
PROGRAM CODE:
#include <stdio.h>
int main()
{
int arr[100], search, c, n;
printf("Enter no of elements\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &arr[c]);
}
printf("Enter the element to search for \n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (arr[c] == search)
{
PROGRAM CODE:
#include <stdio.h>
int main()
{
int c, first, l, last, middle, n, search, arr[100];
printf("Enter No of elements \n");
scanf("%d", &n);
printf("Enter %d Integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &arr[c]);
}
printf("Enter the value to find :\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first + last) / 2;
while (first <= last)
{
if (arr[middle < search])
{
first = middle + 1;
}
else if (arr[middle] == search)
{
return 0;
}
OUTPUT:
EXPERIMENT – 05
PROGRAM CODE:
#include <stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
int main()
{
top = -1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t- ---");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
printf("\n\t EXIT POINT");
break;
}
default:
{
printf("\n\t Please Enter a Valid
Choice(1/2/3/4)");
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= n - 1)
{
printf("\n\tSTACK is over flow");
}
else
{
OUTPUT:
EXPERIMENT – 06
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{
front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete()
{
struct node *ptr;
if (front == NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
if (front == NULL)
{
printf("\nEMPTY QUEUE\n");
}
else
{
printf("\n printing values...\n");
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}
OUTPUT:
EXPERIMENT – 07
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d", &item);
ptr->data = item;
if (front == NULL)
{
front = ptr;
rear = ptr;
front->next = NULL;
rear->next = NULL;
}
else
{
rear->next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete()
{
struct node *ptr;
if (front == NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr = front;
front = front->next;
free(ptr);
}
}
void display()
{
struct node *ptr;
if (front == NULL)
{
printf("\nEMPTY QUEUE\n");
}
else
{
printf("\n printing values...\n");
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
}
OUTPUT:
EXPERIMENT – 08
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main()
{
int choice = 0;
while (choice != 9)
{
printf("\n\n *****Main menu *****\n");
printf("\n choose one of the option\n");
printf("\n-------------------------\n");
printf("\n1.Insert in beginning\n2.insert at
last\n3.Insert in the
middle\n4.delete in the beginning\n5.delete in
the last n6.delete in
the middle\n7.display the list\n");
else
{
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
{
else
{
printf("\n Enter the value?\
n");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
head = ptr;
printf("\n Node
inserted");
}
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\n Node inserted");
}
}
}
}
void randominsert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the value\n");
scanf("%d", &item);
ptr->data = item;
printf("\n Enter the location of the
element to be inserted\n");
scanf("\n %d", &loc);
temp = head;
for (i = 0; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)
void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("\n Nothing to print ");
}
else
{
printf("\n printing values.........\
n");
while (ptr != NULL)
{
printf("%d", ptr->data);
ptr = ptr->next;
}
}
}
void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\n list is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the
beginning ..\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\n the list is empty\n");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list
deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("\n Deleted node from the
last..\n");
}
}
void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;
printf("\n Enter the location of the
node after which you want to delete\n");
scanf("%d", &loc);
ptr = head;
་ OUTPUT:
་
་
EXPERIMENT – 09
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void main()
{
int choice = 0;
else
{
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
{
else
{
printf("\n Enter the
value?\n");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
ptr->next = NULL;
head = ptr;
printf("\n Node
inserted");
}
}
else
{
temp = head;
while (temp->next !=
NULL)
{
temp = temp-
>next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\n Node
inserted");
}
}
}
}
void randominsert()
{
int i, loc, item;
struct node *ptr, *temp;
ptr = (struct node
*)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the value\
n");
scanf("%d", &item);
ptr->data = item;
printf("\n Enter the location
of the element to be inserted\n");
scanf("\n %d", &loc);
temp = head;
for (i = 0; i < loc; i++)
{
temp = temp->next;
if (temp == NULL)
{
printf("\n can't
insert");
return;
}
}
ptr->next = temp->next;
temp->next = ptr;
printf("\n Node insert");
}
}
void display()
{
struct node *ptr;
ptr = head;
if (ptr == NULL)
{
printf("\n Nothing to print
");
}
else
{
printf("\n printing
values.........\n");
while (ptr != NULL)
{
printf("%d", ptr->data);
ptr = ptr->next;
}
}
}
void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\n list is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from
the beginning ..\n");
}
}
void last_delete()
{
struct node *ptr, *ptrl;
if (head == NULL)
{
printf("\n the list is empty\
n");
}
else if (head->next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the
list deleted ..\n");
}
else
{
ptr = head;
while (ptr->next != NULL)
{
ptrl = ptr;
ptr = ptr->next;
}
ptrl->next = NULL;
free(ptr);
printf("\n Deleted node from
the last..\n");
}
}
void random_delete()
{
struct node *ptr, *ptrl;
int loc, i;
printf("\n Enter the location of
the node after which you want to delete\
n");
scanf("%d", &loc);
ptr = head;
OUTPUT:
EXPERIMENT – 10
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#define maxsize 5
void insert();
void delete();
void display();
int front = -1, rear = -1;
int queue[maxsize];
void main()
{
int choice;
while (choice != 4)
{
printf("\n********Main
menu********");
printf("\
n=======================\n");
printf("\n1.insert an element\
n2.delete an element\n3.displaylist\n.4
exit");
printf("\n enter your choice?");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete ();
break;
case 3:
exit(0);
break;
default:
printf("\n Enter valid
choice\n");
}
}
}
void insert()
{
int item;
printf("\n enter the elements\n");
scanf("\n %d", &item);
if (rear == maxsize - 1)
{
printf("\n OVERFLOW\n");
return;
}
if (front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear + 1;
}
queue[rear] = item;
printf("\n Value inserted\n");
}
void delete()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOEW\n");
return;
}
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
printf("\n value deleted\n");
}
}
}
OUTPUT:
EXPERIMENT – 11
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
int main()
{
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
return 0;
}
OUTPUT:
EXPERIMENT – 12
PROGRAM CODE:
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && a[left] > a[largest])
{
largest = left;
}
if (right < n && a[right] > a[largest])
{
largest = right;
}
if (largest != i)
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
void heapsort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(a, n, i);
}
for (int i = n - 1; i >= 0; i--)
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a, 1, 0);
}
}
void printarr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}
}
int main()
{
int a[] = {48, 10, 23, 43.8, 12, 36};
int n = sizeof(a) / sizeof(a[0]);
printarr(a, n);
heapsort(a, n);
printf("\n after sorting elements: ");
printarr(a, n);
return 0;
}
OUTPUT:
EXPERIMENT – 13
PROGRAM CODE:
#include <stdio.h>
int main()
{
int s, i, j, t, lst[50];
return 0;
}
OUTPUT:
EXPERIMENT – 14
PROGRAM CODE:
#include <stdio.h>
OUTPUT:
EXPERIMENT – 15
PROGRAM CODE:
#include <stdio.h>
int main()
{
int arr[] = {164, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionsort(arr, n);
printarray(arr, n);
return 0;
}
OUTPUT: