Data Structure Practical
Data Structure Practical
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter any number\n");
scanf("%d", &n);
return 0;
}
Output :-
Practical No. 2
#include <stdio.h>
int main()
{
int n, fact = 1, i;
return 0;
}
Output:-
Practical No. 3
#include <stdio.h>
int main()
{
int a, b, c;
return 0;
}
Output :-
Practical No. 4
// Implementation of traversing techniq in array.
#include <stdio.h>
int main()
{
int a[10], i;
return 0;
}
Output :-
Practical No. 6
// Implementation of deletion technique in array.
#include <stdio.h>
int main()
{
int i, a[10], size, pos;
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("\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
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
#include <stdio.h>
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()
{
Output :-
Practical No. 10
#include <stdio.h>
int main()
{
int n, factorial;
printf("Enter any number\n");
scanf("%d", &n);
factorial = fact(n);
return 0;
}
int fact(int n)
{
int val = 1;
if (n == 1)
{
return 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("\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
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
#include <stdio.h>
void linear_search(int a[], int data);
int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10, data, i;
return 0;
}
void linear_search(int a[], int data)
{
int val = -1;
if (a[i] == data)
{
val = i;
printf("Data is at index no.:%d\n", i);
}
}
if (val == -1)
{
printf("Data not found\n");
}
}
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;
printf("a[%d]=%d\n", i, a[i]);
}
while (l <= r)
{
mid = (l + r) / 2;
if (data == a[mid])
{
l = mid + 1;
}
r = mid - 1;
}
}
if (l > r)
{
return 0;
}
Output :-
Practical No. 18
#include <stdio.h>
int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;
Bubblesort(a, n);
printf("\n");
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
int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;
Array(a, n);
return 0;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
void Array(int a[], int n)
{
Output :-
Practical No. 20
int main()
{
int a[10] = {63, 93, 56, 23, 48, 36, 67, 52, 10, 72};
int n = 10;
Array(a, n);
InsertionSort(a, n);
printf("\n");
Array(a, n);
return 0;
}
val = a[i];
j = i - 1;
a[j + 1] = a[j];
j--;
}
a[j + 1] = val;
}
}
Output :-