Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Table of Contents

1. Write a C program to illustrate the 3


concept of Sorting Algorithms-
Algorithms Non
Recursive
2. Write a C program to illustrate the 5
concept of Sorting Algorithms-
Algorithms
Recursive
3. Write a C program to illustrate the 7
concept of Searching Algorithms-
Algorithms
Binary Search
4. Write a C program to illustrate the 9
concept of Implementation of Stack
using Array
5. Write a C program to illustrate the 12
concept of Implementation of Queue
using Array
6. Write a C program to illustrate the 15
concept of Implementation of
Circular Queue using Array
7. Write a C program to illustrate the 19
concept of Implementation of Stack
using Linked List
8. Write a C program to illustrate the 22
concept of Implementation of Queue
using Linked List
3|Page

Lab Exercise 1
Program: Write a C program to illustrate the concept of Sorting Algorithms
Non Recursive

Code:

#include <stdio.h>

// Utility function to swap values at two indices in an array


void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// Function to print `n` elements of array `arr`


void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}

// Function to perform bubble sort on a given array `arr[]`


void bubbleSort(int arr[], int n)
{
// `n-1` passes
for (int k = 0; k < n - 1; k++)
{
// last `k` items are already sorted, so the inner loop can
// avoid looking at the last `k` items
for (int i = 0; i < n - 1 - k; i++)
{

Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
4|Page

if (arr[i] > arr[i + 1]) {


swap(arr, i, i + 1);
}
}

// 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>

// Utility function to swap values at two indices in an array


void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// Function to print `n` elements of array `arr`


void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}

// Recursive function to perform bubble sort on subarray `arr[i…n]`


void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1]) {
swap(arr, i, i + 1);
}
}

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

// C program to implement recursive Binary Search


#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]);

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:

Element is present at index 3

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;

// function for push the element into stack


void push()
{
int element ;
if(top==max-1)
printf("Overflow");
else
{
printf("Enter a number ");
scanf("%d",&element);
top=top+1;
stack[top]=element;
}
}

// function for pop the element from the stack

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

// function for display the element of the stack


void display()
{
int element;
if(top==-1)
printf("Underflow condition");
else
{
for(int i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
}
}

// function for reverse the stack


void reverse()
{
int tem;
for(int i=top;i>top/2;i--)
{
tem=stack[i];
stack[i]=stack[top-i];
stack[top-i]=tem;
}
}

//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

Enter your choice


1
Enter a number 12
Enter your choice
1
Enter a number 21
Enter your choice
2
pop element is 21
Enter your choice

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;

// function for insert element into queue

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;
}
}

// function for delete element from the queue

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);
}
}

// function for display all the element of the queue

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.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty

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

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be inserted");


scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();

}}
return 0;
}

Expected Output:

Press 1: Insert an element


Press 2: Delete an element
Press 3: Display the element
Enter your choice
1
Enter the element which is to be inserted
10
Press 1: Insert an element
Press 2: Delete an element

Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
18 | P a g e

Press 3: Display the element


Enter your choice
1
Enter the element which is to be inserted
20
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice
1
Enter the element which is to be inserted
30
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice
2
Elements in a Queue are : 10, 20, 30,
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice
The dequeued element is 10

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;
};

// function for the create list


struct node *createlist()
{
struct node *t;
t=(struct node*)malloc(sizeof(struct node));
return t;
}

// function for push the element in to stack


void push()
{
int element;
struct node *t;
t=createlist();
printf("Enter a element\n");
scanf("%d",&element);
t->data=element;
t->link=stack;
stack=t;
}

// function for pop the elelment from the stack


void pop()
{
struct node *t;
if(stack==NULL)

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;
}
}

// function for display the element of the stack


void display()
{
struct node *t;
if(stack==NULL)
printf("Stack is empty\n");
else
{
t=stack;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->link;
}
printf("\n");
}
}

//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:

1. push the element into stack


2. pop the element from the stack
3. Display the element of the stack
4. exit
enter your choice
1
Enter a element
12
enter your choice
1
Enter a element
23
enter your choice
2
23 is deleted from the stack
enter your choice
1
Enter a element
32
enter your choice
3
32 12
enter your choice
4

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;
};

// function for create node


struct node *createlinklist()
{
struct node *t;
t=(struct node *)malloc(sizeof(struct node));
return t;
}

// function for insert elements


void insert()
{
struct node *t,*tem;
tem=createlinklist();
printf("Enter a element\n");
scanf("%d",&tem->data);
tem->link=NULL;
t=queue;
if(queue==NULL)
queue=tem;
else
{
while(t->link!=NULL)
t=t->link;
t->link=tem;
}
}

Data Structure using C Lab (KCS 351) Manual (CS, III SEM)
23 | P a g e

// function for deletion


void delete()
{
struct node *t;
if(queue==NULL)
printf("Queue is underflow\n");
else
{
t=queue;
printf("%d is deleted\n",t->data);
queue=t->link;
}
}

// function for display element


void display()
{
struct node *t;
t=queue;
if(queue==NULL)
printf("Queue is underflow\n");
else
{
while(t!=NULL)
{
printf("%d ",t->data);
t=t->link;
}
}
}
int main()
{
int ch;
printf(" 1. Insert element into queue\n");
printf(" 2. Delete element from the queue\n");
printf(" 3. display the elements of the queue\n");
printf(" 4. Exit\n");
while(1)
{
printf("Enter your choice\n");

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:

1. Insert element into queue


2. Delete elemetn from the queue
3. display the element of the queue
4. Exit
Enter your choice
1
Enter a element
32
Enter your choice
1
Enter a element
21
Enter your choice
3
32 21
Enter your choice
2
32 is Deleted
Enter your choice
4

Data Structure using C Lab (KCS 351) Manual (CS, III SEM)

You might also like