DSA Lab File Done
DSA Lab File Done
DSA Lab File Done
Experiment No – 1
Code: -
#include <stdio.h>
int linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++)
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given
array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = linearSearch(a, n, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", res);
return 0;
}
2
3
Experiment No – 2
Object: - WAP to implement binary search.
Code: - #include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if (end >= beg)
{
mid = (beg + end) / 2;
/* if the item to be searched is present at middle */
if (a[mid] == val)
{
return mid + 1;
}
/* if the item to be searched is smaller than middle, then it can
only be in left subarray */
else if (a[mid] < val)
{
return binarySearch(a, mid + 1, end, val);
}
/* if the item to be searched is greater than middle, then it can
only be in right subarray */
else
{
return binarySearch(a, beg, mid - 1, val);
}
}
return -1;
}
int main()
{
int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; // given array
int val = 40; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int res = binarySearch(a, 0, n - 1, val); // Store result
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val);
if (res == -1)
printf("\nElement is not present in the array");
4
else
printf("\nElement is present at %d position of array", res);
return 0;
}
5
Experiment No – 3
Output:-
7
Experiment No – 4
Output :-
14
Experiment No – 7
Code :-
#include <stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1, n2, n3, num;
printf("Enter the expression :: ");
scanf("%s", exp);
e = exp;
while (*e != '\0')
{
if (isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
15
n2 = pop();
switch (*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n", exp, pop());
return 0;
}
Output :-
16
Experiment No – 8
Code :-
#include <stdio.h>
#define n 5
int main()
{
int queue[n], ch = 1, front = 0, rear = 0, i, j = 1,
x = n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display
\n4.Exit");
while (ch)
{
printf("\nEnter the Choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
if (rear == x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:", j++);
scanf("%d", &queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
17
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
18
Output :-