Professional Documents
Culture Documents
Dsa Practical Merged
Dsa Practical Merged
Lab Exercise 1
Program: Write a C program to illustrate the concept of Sorting Algorithms
Non Recursive
Code:
#include <stdio.h>
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
4|Page
// the algorithm can be terminated if the inner loop didn't do any swap
}
}
int main(void)
{
int arr[] = { 3, 5, 8, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
Expected Output:
-2 1 3 4 5 8 9
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
5|Page
Lab Exercise 2
Program 2: Write a C program to illustrate the concept of Sorting Algorithms
Recursive
Code:
#include <stdio.h>
if (n - 1 > 1) {
bubbleSort(arr, n - 1);
}
}
int main(void)
{
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
6|Page
int arr[] = { 3, 5, 8, 4, 1, 9, -2 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
Expected Output:
-2 1 3 4 5 8 9
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
7|Page
Lab Experiment 3
Program 3: Write a C program to illustrate the concept of Searching
Algorithm- Binary Search
Code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
8|Page
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;
}
Expected Output:
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
9|Page
Lab Exercise 4
Program: Write a C program to illustrate the concept of Implementation of
Stack using Array.
Code:
#include<stdio.h>
#define max 5
int stack[max];
int top=-1;
void pop()
{
int element;
if(top==-1)
printf("Underflow condition");
else
{
element=stack[top];
printf("pop element is %d",element);
top=top-1;
}
}
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
10 | P a g e
//Driver function
void main()
{
int ch;
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.reverse\n");
printf("5. exit\n");
while(1)
{
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
11 | P a g e
{
case 1:
push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: reverse();
break;
case 5: exit(0);
default:
printf("Worng key");
}
}
}
Expected Output:
1.push
2.pop
3.display
4.reverse
exit
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
12 | P a g e
Lab Exercise 5
Program: Write a C program to illustrate the concept of Implementation of
Queue using Array.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int queue[max];
int rear,front;
rear=-1;
front=-1;
void insert()
{
int element;
if(rear==max-1)
printf("\nqueue is overflow");
else
{
if(front==-1)
front=0;
printf("\nEnter a value: ");
scanf("%d",&element);
rear+=1;
queue[rear]=element;
}
}
void delete()
{
int element;
if(front==-1||front>rear)
printf("\nEnter underflow condition");
else
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
13 | P a g e
{
element=queue[front];
front+=1;
printf("%d is deleted\n",element);
}
}
void display()
{
int i;
if(front==-1|| front>rear)
printf("Underflow condition\n");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}
// Driver function
void main()
{
int ch;
printf("1. insert element\n");
printf("2. delete element\n");
printf("3. display element\n");
printf("4. exit\n");
while(1)
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
14 | P a g e
break;
case 4: exit(0);
default: printf("\nWrong key");
}
}
}
Expected Output:
1. insert element
2. delete element
3. display element
4. exit
Enter your choice
1
Enter a value: 12
Enter your choice
2
12 is deleted
Enter your choice
1
Enter a value 23
Enter your choice
3
23
Enter your choice
4
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
15 | P a g e
Lab Exercise 6
Program: Write a C program to illustrate the concept of Implementation of
Circular Queue using Array.
Code:
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear positi
on.
}
}
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
16 | P a g e
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
17 | P a g e
switch(choice)
{
case 1:
}}
return 0;
}
Expected Output:
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
18 | P a g e
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
19 | P a g e
Lab Exercise 7
Program: Write a C program to illustrate the concept of Implementation of
Stack using Linked List.
Code:
#include<stdio.h>
#include<stdlib.h>
int *stack=NULL;
struct node
{
int data;
struct node *link;
};
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
20 | P a g e
printf("Stack is underfow\n");
else
{
t=stack;
printf("%d is deleted from the stack\n",t->data);
stack=t->link;
}
}
//Driver function
int main()
{
int ch;
printf("1. push the element in to the stack\n");
printf("2. pop the elelment fromt the stack\n");
printf("3.Display the element of the stack\n");
printf("4. exit\n");
while(1)
{
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
21 | P a g e
case 1: push();
break;
case 2: pop();
break;
case 3:display();
break;
case 4: exit(0);
default: printf("You entered the wrong key\n");
}
}
return 0;
}
Expected Output:
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
22 | P a g e
Lab Exercise 8
Program: Write a C program to illustrate the concept of Implementation of
Queue using Linked List.
Code:
#include<stdio.h>
#include<stdlib.h>
int *queue=NULL;
struct node
{
int data;
struct node *link;
};
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
23 | P a g e
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
24 | P a g e
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("Wrong key\n");
}
}
return 0;
}
Expected Output:
Data Structure using C Lab (KCS 351) Manual (CS, III SEM)