0% found this document useful (0 votes)
10 views30 pages

Data Structure Practical

The document contains a series of practical programming exercises in C, covering various fundamental concepts such as calculating the sum of numbers, finding factorials, determining the greatest of three numbers, and implementing data structures like arrays, stacks, and queues. Each practical includes code snippets and expected outputs, demonstrating the implementation of algorithms for insertion, deletion, searching, and traversal. The exercises also cover recursive techniques and searching algorithms like linear and binary search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

Data Structure Practical

The document contains a series of practical programming exercises in C, covering various fundamental concepts such as calculating the sum of numbers, finding factorials, determining the greatest of three numbers, and implementing data structures like arrays, stacks, and queues. Each practical includes code snippets and expected outputs, demonstrating the implementation of algorithms for insertion, deletion, searching, and traversal. The exercises also cover recursive techniques and searching algorithms like linear and binary search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Practical No.

// Design a program to find sum of N number.

#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter any number\n");
scanf("%d", &n);

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


{
sum = sum + i;
}
printf("Sum :- %d", sum);

return 0;
}

Output :-
Practical No. 2

// Design a program for find factorial of N.

#include <stdio.h>
int main()
{
int n, fact = 1, i;

printf("Enter any number\n");


scanf("%d", &n);

for (i = 1; i <= n; i++)


{
fact = fact * i;
}

printf("The factorial of %d is :- %d", n, fact);

return 0;
}

Output:-
Practical No. 3

// Design a program to find greatest among 3 given number.

#include <stdio.h>
int main()
{
int a, b, c;

printf("Enter any three number respectively\n");


scanf("%d%d%d",&a,&b,&c);

if (a > b && a > c)


{
printf("%d is greater number", a);
}
else if (b > a && b > c)
{
printf("%d is greater number", b);
}
else if (c > a && c > b)
{
printf("%d is greater number", c);
}

return 0;
}

Output :-
Practical No. 4
// Implementation of traversing techniq in array.

#include <stdio.h>
int main()
{
int a[10], i;

printf("Input any 10 value\n");

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


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

printf("The array element are as follows :-\n");

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


{
printf("a[%d]=%d\n", i, a[i]);
}
return 0;
}
Output :-
Practical No. 5
// Implementation of insertion technique in array.
#include <stdio.h>
int main()
{
int a[100], i, pos, data, size;

printf(" Enter the size of array\n");


scanf("%d", &size);

printf("Enter any %d values\n", size);

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


{
scanf("%d", &a[i]);
}
printf("Enter data and its index no. for storing this element in array\n");
scanf("%d,%d", &data, &pos);

for (i = size; i >= pos; i--)


{
a[i + 1] = a[i];
}
a[pos] = data;
size = size + 1;

printf("The array after inserting a new element :-\n");

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


{
printf("a[%d]=%d\n", i, a[i]);
}

return 0;
}

Output :-
Practical No. 6
// Implementation of deletion technique in array.

#include <stdio.h>
int main()
{
int i, a[10], size, pos;

printf("Enter the size of array\n");


scanf("%d", &size);

printf("Enter any %d element :-\n", size);

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


{
scanf("%d", &a[i]);
}
printf("Enter the position for deleting an element\n");
scanf("%d", &pos);

for (i = pos; i <= size; i++)


{
a[i] = a[i + 1];
}
size = size - 1;
printf("The array after deletion an element is :-\n");
for (i = 0; i < size; i++)
{
printf("a[%d]=%d\n", i, a[i]);
}

return 0;
}

Output :-
Practical No. 7
// Implementation of push and pop operation on stack.
#include <stdio.h>
int main()
{
int stack[10];
int top = -1, size = 10, C;

printf("Hey, Hello Let's do some operation in stack\n");


printf("\n");
printf("-------------------------------------------------\n");
printf("So enter your choice from the following\n");

printf("\n");

while (1)
{
printf("PUSH--> Enter 1 \t Pop--> Enter 2 \t Dispay--> Enter 3 \t Exit--> Enter 0\n");

scanf("%d", &C);

if (C == 1)
{
int n;

if (top == 9)
{
printf("The stack is overflow\n");
}
else
{
printf("Enter the data that you want to be insert in stack\n");
scanf("%d", &n);
top++;
stack[top] = n;
}
}

else if (C == 2)
{
if (top == -1)
{
printf("The stack is underflow\n");
}
else
{
top--;
}
}
else if (C == 3)
{
if (top == -1)
{
printf("There is no data in stack\n");
}
else
{
for (int i = top; i >= 0; i--)
{
printf("a[%d]=%d\n", i, stack[i]);
}
}
}
else if (C == 0)
{
break;
}

else
{
printf("Enter valid choice\n");
}
}
return 0;
}

Output :-
Practical No. 8

// implementation of insertion and deletion techniq in queue.


#include <stdio.h>
#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = -1;
int front = -1;
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:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == -1)

front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}

void delete()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("Element deleted from queue is : %d\n", 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 :-
Practical No. 9

// Implementation of List Data structure using array

#include <stdio.h>

int list[10], listSize = 10;

void create();
void display();
void insert();
void delete();
// void search();

void main()
{
int ii;
startLabel:
printf("\n--------------------------");
printf("\nSelect an Option\n");
printf("1.insert 2.delete\n");
printf("3.display 4.exit\n");
printf("5.create 6.search\n");

scanf("%d", &ii);

switch (ii)
{
case 1:
insert();
goto startLabel;
break;
case 2:
delete ();
goto startLabel;
break;
case 3:
display();
goto startLabel;
break;
case 4:

break;
case 5:
create();
goto startLabel;
break;
case 6:

break;

default:
printf("Invalid Option selected \n Try Again \n");
goto startLabel;
break;
}
}
void create()
{
int n;
printf("Enter Number of Items \n");
scanf("%d", &n);
if (n <= 0 || n > listSize)
{
printf("Invalid Input Number must be From 1 to %d \n", listSize);
printf("Try again \n");
create();
return;
}
else
{
for (int i = 0; i < n; i++)
{
printf("Enter %d element of list\n", i + 1);
scanf("%d", &list[i]);
}
}
}
void display()
{
printf("List =");
for (int i = 0; i < listSize; i++)
{
printf(" %d ,", list[i]);
}
printf(" ;\n");
}
void insert()
{

int pos, data;


printf("Enter index Where data being inserted \n");
scanf("%d", &pos);
printf("Enter data Which being inserted \n");
scanf("%d", &data);
for (int i = listSize - 1; i > pos; i--)
{
list[i] = list[i - 1];
}
list[pos] = data;
}
void delete()
{

int pos, data;


printf("Enter index Where data being deleted \n");
scanf("%d", &pos);

for (int i = pos; i < listSize; i++)


{
list[i] = list[i + 1];
}
}

Output :-
Practical No. 10

// implementation of recursive technique for finding factorial of an integer.

#include <stdio.h>

int fact(int n);

int main()
{
int n, factorial;
printf("Enter any number\n");
scanf("%d", &n);

factorial = fact(n);

printf("FACTORIAL : %d\n", factorial);

return 0;
}

int fact(int n)
{

int val = 1;

if (n == 1)
{

return 1;
}

val = n * fact(n - 1);

return val;
}

Output :-
Practical No. 11
// Implement stack using array
#include <stdio.h>
int main()
{
int stack[10];
int top = -1, size = 10, C;

printf("Hey, Hello Let's do some operation in stack\n");


printf("\n");
printf("-------------------------------------------------\n");
printf("So enter your choice from the following\n");

printf("\n");

while (1)
{
printf("PUSH--> Enter 1 \t Pop--> Enter 2 \t Dispay--> Enter 3 \t Exit--> Enter 0\n");

scanf("%d", &C);

if (C == 1)
{
int n;

if (top == 9)
{
printf("The stack is overflow\n");
}
else
{
printf("Enter the data that you want to be insert in stack\n");
scanf("%d", &n);
top++;
stack[top] = n;
}
}

else if (C == 2)
{
if (top == -1)
{
printf("The stack is underflow\n");
}
else
{
top--;
}
}
else if (C == 3)
{
if (top == -1)
{
printf("There is no data in stack\n");
}
else
{
for (int i = top; i >= 0; i--)
{
printf("a[%d]=%d\n", i, stack[i]);
}
}
}
else if (C == 0)
{
break;
}

else
{
printf("Enter valid choice\n");
}
}
return 0;
}

Output :-
Practical No. 12

// implementation of queue using array


#include <stdio.h>
#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = -1;
int front = -1;
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:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == -1)

front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}

void delete()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("Element deleted from queue is : %d\n", 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 :-
Practical No. 16

// implementation of linear search.

#include <stdio.h>
void linear_search(int a[], int data);

void Array(int a[], int n);

int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10, data, i;

printf("Your array is\n");


Array(a, n);
printf("Enter the data that you want to search\n");
scanf("%d", &data);
linear_search(a, data);

return 0;
}
void linear_search(int a[], int data)
{
int val = -1;

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


{

if (a[i] == data)
{
val = i;
printf("Data is at index no.:%d\n", i);
}
}
if (val == -1)
{
printf("Data not found\n");
}
}

void Array(int a[], int n)


{

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


{

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

Output:-
Practical No 17
// Implementation of binary search using array.

#include <stdio.h>

int main()
{
int a[10] = {5, 7, 15, 27, 36, 42, 58, 67, 81, 93};
int i, l = 0, r = 9, mid, data;

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


{

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

printf("Enter The data that you want to find\n");


scanf("%d", &data);

while (l <= r)
{

mid = (l + r) / 2;

if (data == a[mid])
{

printf("Data is at index No.=%d", mid);


break;
}

else if (data > a[mid])


{

l = mid + 1;
}

else if (data < a[mid])


{

r = mid - 1;
}
}

if (l > r)
{

printf("data not found");


}

return 0;
}

Output :-
Practical No. 18

// imlementation of bubble sort.

#include <stdio.h>

void Bubblesort(int a[], int n);

void printarray(int a[], int n);

int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;

printf("Array before sorting\n\n");


printarray(a, n);
printf("\n");

Bubblesort(a, n);
printf("\n");

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


printarray(a, n);
return 0;
}

void Bubblesort(int a[], int n)


{
int i, j, temp;

for (i = 0; i < n - 1; i++)


{

for (j = 0; j < n - 1 - i; j++)


{

if (a[j] > a[j + 1])


{

temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void printarray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}

Output :-
Practical No. 19

// Implementation of selection sort.


#include <stdio.h>

void SelectionSort(int a[], int n);

void Array(int a[], int n);

int main()
{

int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;

printf("Array before sorting\n");


Array(a, n);
printf("\n");
printf("Array after sorting\n");
SelectionSort(a, n);

Array(a, n);

return 0;
}

void SelectionSort(int a[], int n)


{

int i, j, min, temp;


for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{

if (a[j] < a[min])


{
min = j;
}
}

temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
void Array(int a[], int n)
{

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


{

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


}
}

Output :-
Practical No. 20

// implementation of insertion sort.


#include <stdio.h>

void InsertionSort(int a[], int n);

void Array(int a[], int n);

int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;

printf("Array before sorting\n");

Array(a, n);

InsertionSort(a, n);

printf("\n");

printf("Array after sorting\n");

Array(a, n);

return 0;
}

void Array(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
void InsertionSort(int a[], int n)
{
int i, j, val;
for (i = 1; i < n; i++)
{

val = a[i];
j = i - 1;

while (j >= 0 && a[j] > val)


{

a[j + 1] = a[j];
j--;
}
a[j + 1] = val;
}
}

Output :-

You might also like