Practical File OF Data Structure and Algorithms

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

PRACTICAL FILE

OF
Data Structure and
Algorithms
Submitted in partial fulfillment of the requirement for the
Degree of Bachelor of Technology (B. tech)

In

Computer Science & Engineering

Submitted by: Ankush(8721103)cse 3rd sem

Submitted to: Mrs. Deeksha Kanwal

State Institute of Engineering & Technology, Nilokheri (Karnal)


(Affiliated to Kurukshetra University, Kurukshetra)

NAME OF PRACTICALS REMARKS


S.NO.
1 Write a program to Implement Linear Search

2 Write a program to Implement Binary Search

Write a program to Implement Insertion Sort.


3

Write a program to Implement Selection sort.


4

5
Write a program to Implement Bubble sort
6
Write a program to Implement Quick sort.

7 Write a program to Implement Merge sort

Write a program to Implement Stack and its


8 operations.

9
Write a program to Implement Queue and its
operations.
Write a program to Implement Circular Queue and
10 its operations.

Write a program to Implement Linked List and its


11 operation

Program: 1

Aim: Write a program to Implement Linear Search

Source Code:
#include <stdio.h>
int main()
{
int a [100], s, i, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (i = 0; i < n; i++) {


scanf("%d", &a[i]);
}

printf("Enter a number to search\n");


scanf("%d", &s );
for (i = 0; i < n; i++)
{
if (a[i] == s)
{
printf("%d is present at location %d.\n", s, i+1);
break;
} } if
(i == n)
printf("%d isn't present in the array.\n", s); }

Output:
Program: 2

Aim: Write a program to Implement Binary Search

Source Code:
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key,
array[100];
printf("Enter number of elements\n");
scanf("%d",&n); printf("Enter %d
integers\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find\n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{ if(array[mid] < key) low =
mid + 1; else if (array[mid]
== key)
{
printf("%d found at location %d", key, mid+1);
break;
} else high = mid - 1;
mid = (low + high)/2;
} if(low >
high)
printf("Not found! %d isn't present in the list\n", key); return
0;}
Output:
Program: 3
Aim: Write a program to Implement Insertion Sort.

Source Code:
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{ j
= i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}

Output:
Program: 4

Aim: Write a program to Implement Selection sort.

Source Code:
#include <stdio.h> int
main () {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10; int i, j, position, swap; for (i =
0; i < (n - 1); i++) { position = i; for (j
= i + 1; j < n; j++) { if (arr[position] >
arr[j])
position = j;
}
if (position != i) {
swap = arr[i]; arr[i] =
arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}

Output:
Program: 5

Aim: Write a program to Implement Bubble sort.

Source Code:
#include <stdio.h>

int main(){

int arr[50], num, x, y, temp;

printf("Please Enter the Number of Elements you want in the array:


");

scanf("%d", &num);

printf("Please Enter the Value of Elements: ");

for(x = 0; x < num; x++)

scanf("%d", &arr[x]);

for(x = 0; x < num - 1; x++){

for(y = 0; y < num - x - 1; y++){

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


temp = arr[y];

arr[y] = arr[y + 1];

arr[y + 1] = temp;

printf("Array after implementing bubble sort: ");

for(x = 0; x < num; x++){

printf("%d ", arr[x]);

return 0;

Output:
Program: 6

Aim: Write a program to Implement Quick sort.

Source Code:
#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;
while(i<j){

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])
j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

}
}

int main(){

int i, count, number[25];

printf("Enter some elements (Max. - 25): ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

for(i=0;i<count;i++) scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("The Sorted Order is: ");

for(i=0;i<count;i++)

printf(" %d",number[i]);

return 0;

Output:
Program: 7

Aim: Write a program to Implement Merge sort.

Source Code:
#include <stdio.h> #define
max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int
b[10];

void merging(int low, int mid, int high) {


int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2]) b[i] = a[l1++]; else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];

for(i = low; i <= high; i++)


a[i] = b[i];
}
void sort(int low, int high) {
int mid;

if(low < high) { mid


= (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;

printf("List before sorting\n");

for(i = 0; i <= max; i++)


printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

for(i = 0; i <= max; i++)


printf("%d ", a[i]);
}

Output:

Program: 8

Aim: Write a program to Implement Stack and its operations.

Source Code:
#include<stdio.h>

#include<stdlib.h>

#define Size 4

int Top=-1, inp_array[Size];


void Push(); void Pop();
void show();

int main()
{
int choice;

while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the
element\n3.Show\n4.End");
printf("\n\nEnter the choice:");
scanf("%d",&choice);

switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);

default: printf("\nInvalid choice!!");


}
}
}

void Push()
{
int x;

if(Top==Size-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the
stack:"); scanf("%d",&x); Top=Top+1;
inp_array[Top]=x;
}
}

void Pop()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",inp_array[Top]);
Top=Top-1;
}
}
void show()
{

if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}

Output:
Program: 9

Aim: Write a program to Implement Queue and its


operations.

Source Code:

#include <stdio.h>
#include<stdlib.h> #define MAX 50 void
insert(); void delete(); void display(); int
queue_array[MAX]; int rear = - 1; int front
= - 1; int main() { int choice; while (1) {
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n"); printf("Enter your
choice : "); scanf("%d", &choice);
switch(choice)
{ case
1:
insert
();
break;
case
2:
delete
();
break;
case
3:
displa
y();
break;
case
4:
exit(1
);
defaul
t:
printf
("Wron
g
choice
n");
}
} } void insert() { int
item; if(rear == MAX - 1)
printf("Queue Overflow n");
else { if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item); rear = rear + 1;
queue_array[rear] = item;
} } void
delete() {
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return; }
else {
printf("Element deleted from queue is : %dn",
queue_array[front]); front = front + 1;
} }
void display()
{ int i; if(front == - 1)
printf("Queue is empty n");
else { printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}

Output:
Program: 10

Aim: Write a program to Implement Circular Queue and its


operations.

Source Code:
#include<stdio.h> #
define MAX 5 int
cqueue_arr[MAX]; int
front = -1; int rear = -
1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0; else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0; else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n"); if(
front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item; do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n"); printf("Enter
your choice : \n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Input the element for insertion in
queue : \n"); scanf("%d", &item); insert(item);
break; case 2 : deletion();
break; case 3: display();
break; case 4: break;
default: printf("Wrong
choice\n");
}
} while (choice! = 4);
return 0;
}
Output:
Program: 11

Aim: Write a program to Implement Linked List and its


operation

Source Code:
#include <stdlib.h>
#include <stdio.h>

void display(); void


insert_end();
void delete_end();

struct node
{ int
info;
struct node *next;
};
struct node *start = NULL; int
main()
{ int
choice;
printf("\n MENU \n");
printf("\n 1.Insert at the end \n"); printf("\n
2.Delete from the end \n"); printf("\n
3.Display \n"); printf("\n 4.Exit \n");
while (1)
{
printf("\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 3:
display();
break; case 1:
insert_end();
break; case 2:
delete_end();
break; case 4:
exit(0);
break; default:
printf("\n Wrong Choice \n");
break;
}
}
return 0;
}

void insert_end()
{
struct node *temp, *ptr; temp = (struct node
*)malloc(sizeof(struct node)); printf("\nEnter the
data value for the node:\t"); scanf("%d", &temp-
>info); temp->next = NULL;
if (start == NULL)
{
start = temp;
}
else
{ ptr =
start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}

void delete_end()
{
struct node *temp, *ptr;
if (start == NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if (start->next == NULL)
{ ptr =
start; start =
NULL;
printf("\nThe deleted element is:%dt", ptr->info);
free(ptr);
}
else
{ ptr =
start;
while (ptr->next != NULL)
{
temp = ptr;
ptr = ptr->next;
}
temp->next = NULL;
printf("\nThe deleted element is:%d\t", ptr->info); free(ptr);
}
}

void display()
{
struct node *ptr;
if (start == NULL)
{
printf("\nList is empty\n");
return;
}
else
{ ptr =
start;
printf("\nThe List elements are:\t");
printf("[START] --> ");
while (ptr != NULL)
{
printf("%d --> ", ptr->info);
ptr = ptr->next;
}
printf("[END]");
}
}
Output:

You might also like