Data Structure Practical
Data Structure Practical
Data Structure Practical
____________________
Signature of the Teacher Head of the Department
#include<stdio.h>
#include<conio.h>
void main()
scanf("%d", &n);
scanf("%d", &array[c]);
// return 0;
}
Output:
Enter 5 integers
54
55
Sum=115
2.Program in c to find factorial of N numbers
#include <stdio.h>
#include<conio.h>
int main() {
int n, i;
scanf("%d", &n);
if (n < 0)
else {
fact *= i;
} return 0;
OUTPUT:
3. Design :Program to find greatest among 3 given numbers using Array
#include <stdio.h>
int main()
{
int a[1000],i,n,min,max;
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
Output:
Enter size of the array : 5
Enter elements in array : 1 2 3 5 4
minimum of array is : 1
maximum of array is : 5
Practical No. 4
Aim: - Practical to demonstrate traversing of an Array.
# include<stdio.h>
# include<conio.h>
void main( )
{
int a[10], i, j, n;
clrscr();
printf(“Enter the range of array elements\n”);
scanf(“%d”,&n);
printf(“Enter the array elements\n”);
for(i=0; i<n; i++)
{
scanf(“%d\t”,&a[i]);
}
printf(“Array elements are-\n”);
for(i=0; i<n; i++)
{
printf(“%d\n”,a[i]);
}
getch();
}
OUTPUT
Enter the range of array elements
10
Enter the array elements
11
22
33
44
55
66
77
88
99
100
Array elements are –
11
22
33
44
55
66
77
88
99
100
Practical No.5
Aim: - Practical to demonstrate insertion operation of array.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],ub,k,n,item,i,max;
clrscr( );
printf(“Enter the range of an array\n”);
scanf(“%d”,&n);
printf(“Enter the array elements\t”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
max=10;
ub=n-1;
printf(“Enter the position of an element where you want to insert\n”);
scanf(“%d”,&k);
printf(“Enter the element you want to insert\n”);
scanf(“%d”,&item);
printf(“After inserting the new element into the array\t”);
if(ub==max-1)
{
printf(“Array Overflow\n”);
}
else
{
while(ub>=k)
{
a[ub+1]=a[ub];
ub--;
}
}
for(i=0;i<=n;i++)
{
if(i==k) {
a[i]=item;
}
printf(“%d\n”, a[i]);
}
getch();
}
OUTPUT
Enter the range of an array
5
Enter the array elements
11
22
33
44
55
Enter the position of an element where you want to insert
3
Enter the element you want to insert
100
After inserting the new element into the array
11
22
33
100 44
55
Practical No.6
Aim: - Practical to demonstrate deletion operation on array.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],lb,ub,k,n,i,max;
clrscr( );
printf(“Enter the range of an array\n”);
scanf(“%d”,&n);
printf(“Enter the array elements\t”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
max=10;
lb=0;
ub=n-1;
printf(“Enter the position of an element you want to delete\n”);
scanf(“%d”,&k);
if(ub==0)
{
printf(“Array Underflow\n”);
}
else
{
while(k<ub)
{
a[k]=a[k+1]; k++;
}
}
printf(“After deleting the element of an array\n”);
for(i=0;i<ub;i++)
{
printf(“%d\n”, a[i]);
}
getch();
}
OUTPUT
Enter the range of an array
5
Enter the array elements
11
22
33
44
55
Enter the position of an element you want to delete
1
After deleting the element into the array
11
33
44
55
Practical No.7(ii)
Aim: - Practical to demonstrate POP operation on Stack.
SOURCE CODE
# include<stdio.h>
# include<conio.h>
void main()
{
int n,top,i,max,Stack[10];
clrscr();
printf("Enter the range of stack elements\n");
scanf("%d",&n);
max=10;
top=n-1;
if(top!=-1)
{
printf("Enter the Stack Elements are:-\n");
for(i=0;i<n;i++)
{
scanf("%d",&Stack[i]);
}
printf("Stack Elements are:-\n");
for(i=top;i>=0;i--)
{
printf("TOP=%d\t%d\n",i,Stack[i]);
}
Stack[top]=NULL;
top=top-1;
printf("STACK after POP operation:-\n");
for(i=top;i>=0;i--)
{
printf("TOP=%d\t%d\n",i,Stack[i]);
}
}
else
{
printf("Stack Underflow\nTOP=-1");
}
getch();
}OUTPUT-1
Enter the range of stack elements
5
Enter the Stack Elements are:-
1
2
3
4
5
Stack Elements are:-
TOP=4 5
TOP=3 4
TOP=2 3
TOP=1 2
TOP=0 1
STACK after POP operation:-
TOP=3 4
TOP=2 3
TOP=1 2
TOP=0 1
OUTPUT-2
Enter the range of stack elements
0
Stack Underflow
TOP=-1
Practical No.8(i)
Aim: - Practical to demonstrate Enqueue operation on Queue.
SOURCE CODE
# include<stdio.h>
# include<conio.h>
void main()
{
int n,item,Front,Rear,max,i,q[10];
clrscr();
printf("Enter the range of Queue elements\n");
scanf("%d",&n);
Front=1;
Rear=n;
max=10;
printf("Enter the Queue Elements:-\n");
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]);
}
printf("Queue Elements are:-\n");
for(i=1;i<=n;i++)
{
printf("%d\n",q[i]);
}
printf("FRONT=%d\tREAR=%d\n",Front,Rear);
printf("Enter the number which you want to PUSH in the Stack\n");
scanf("%d",&item);
if(Rear==max)
{
printf("Queue Overflow");
} else
{
Rear=Rear+1;
q[Rear]=item;
printf("Queue after Enqueue operation:-\n");
for(i=1;i<=Rear;i++)
{
printf("%d\n",q[i]);
}
printf("FRONT=%d\tREAR=%d\n",Front,Rear);
}
getch();
}
OUTPUT-1
Enter the range of Queue elements
5
Enter the Queue Elements:-
11
22
33
44
55
66
Queue Elements are:-
11
22
33
44
55
66
FRONT=1 REAR=6
Enter the number which you want to PUSH in the Stack
77Queue after Enqueue operation:-
11
22
33
44
55
66
77
FRONT=1 REAR=7
OUTPUT-2
Enter the range of Queue elements
5
Enter the Queue Elements:-
1
2
3
4
5
6
7
8
9
10
Queue Elements are:-
1
2
3
4
5
6
7
8
9
10
FRONT=1 REAR=10
Enter the number which you want to PUSH in the Stack
11
Queue Overflow
Practical No.8(ii)
Aim: - Practical to demonstrate Dequeue operation on Queue.
SOURCE CODE
# include<stdio.h>
# include<conio.h>
void main()
{
int n,item,Front,Rear,i,q[10];
clrscr();
printf("Enter the range of Queue elements\n");
scanf("%d",&n);
if(n>0)
{
Front=1;
Rear=n;
printf("Enter the Queue Elements:-\n");
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]);
}
printf("Queue Elements are:-\n");
for(i=1;i<=n;i++)
{
printf("%d\n",q[i]);
}
printf("FRONT=%d\tREAR=%d\n",Front,Rear);
q[Front]=NULL;
Front=Front+1;
printf("Queue after Deque operation:-\n");
for(i=Front;i<=Rear;i++)
{
printf("%d\n",q[i]);
}
printf("FRONT=%d\tREAR=%d\n",Front,Rear);
}
else
{
Front=0;
Rear=0;
printf("Queue is Underflow\n"); printf("FRONT=%d\tREAR=%d\n",Front,Rear);
}
getch();
}
OUTPUT-1
Enter the range of Queue elements
5
Enter the Queue Elements:-
1
2
3
4
5
Queue Elements are:-
1
2
3
4
5
FRONT=1 REAR=5
Queue after Deque operation:-
2
3
4
5
FRONT=2 REAR=5
******OUTPUT-2******
Enter the range of Queue elements
0
Queue is Underflow
FRONT=0 REAR=0
PRACTICAL NO.9 i
Aim: Implementation of List data structure using singly linked list.
1. #include <stdio.h>
2. #include <stdlib.h>
3. //Represent a node of singly linked list
struct node{
4. int data;
5. struct node *next;
6. };
7.
8. //Represent the head and tail of the singly linked list
9. struct node *head, *tail = NULL;
10.
11. //addNode() will add a new node to the list
12. void addNode(int data) {
13. //Create a new node
14. struct node *newNode = (struct node*)malloc(sizeof(struct node));
15. newNode->data = data;
16. newNode->next = NULL;
17.
18. //Checks if the list is empty
19. if(head == NULL) {
20. //If list is empty, both head and tail will point to new node
21. head = newNode;
22. tail = newNode;
23. }
24. else {
25. //newNode will be added after tail such that tail's next will point to newNode
26. tail->next = newNode;
27. //newNode will become new tail of the list
28. tail = newNode;
29. }
30. }
31.
32. //display() will display all the nodes present in the list
33. void display() {
34. //Node current will point to head
35. struct node *current = head;
36.
37. if(head == NULL) {
38. printf("List is empty\n");
39. return;
40. }
41. printf("Nodes of singly linked list: \n");
42. while(current != NULL) {
43. //Prints each node by incrementing pointer
44. printf("%d ", current->data);
45. current = current->next;
46. }
47. printf("\n");
48. }
49.
50. int main()
51. {
52. //Add nodes to the list
53. addNode(1);
54. addNode(2);
55. addNode(3);
56. addNode(4);
57.
58. //Displays the nodes present in the list
59. display();
60.
61. return 0;
62. }
63. Output:
64. Nodes of singly linked list:
65. 1234
Practical no.9 ii
Aim: Implement Stack using Array
#include <stdio.h>
int stack[MAX_SIZE];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack is empty!\n");
return -1;
}
int data = stack[top];
top--;
return data;
}
int main()
{
push(1);
push(2);
push(3);
push(4);
push(5);
push(3);
Output:
Elements in the stack are: 3 5 4 3 2 1
Practical No.10
Aim: - Practical to demonstrate Factorial of a number using Recursion.
SOURCE CODE
# include<stdio.h>
# include<conio.h>
int factorial(int);
void main( )
{
int n,f;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of the number %d is %d",n,f);
getch();
}
int factorial(int n)
{ int f;
if(n==1)
return(1);
else
f=n*factorial(n-1);
return(f);
}
OUTPUT
Enter the number: 6
Factorial of the number 6 is 720
Practical No.11
Aim: -Implenentation of Stack
#include<stdio.h>
#include<stdlib.h>
int pop(){
if(top==-1) return -1;
return stack[top--];
}
int peek(){
if(top==-1) return -1;
return stack[top];
}
void display(){
for(int i=top ; i>-1 ; i--) printf("%d ",stack[i]);
printf("\n\n");
}
int main(){
n = 10;
stack = (int*)malloc(n*sizeof(int));
push(1);
push(2);
push(3);
display();
display();
return 0;
}
OUTPUT:
Initializing the stack with size 10
Pushing elements into the stack
1
2
3
Practical no.12
Aim:Implementation of Queue using Array
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
Enter no 1:10
Enter no 2:54
Enter no 3:98
Enter no 4:234
Deleted Element is 10
Enter the Choice:3
struct node {
int data; //node will store some data
struct node *right_child; // right child
struct node *left_child; // left child
};
return temp;
}
// searching operation
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
return search(root -> right_child, x);
else //x is smaller than the data, so we will search the left subtree
return search(root -> left_child, x);
}
// insertion
struct node* insert(struct node * root, int x) {
//searching for the place to insert
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
root -> right_child = insert(root -> right_child, x);
else // x is smaller and should be inserted to left
root -> left_child = insert(root -> left_child, x);
return root;
}
// deletion
struct node* delete(struct node * root, int x) {
//searching for the item to be deleted
if (root == NULL)
return NULL;
if (x > root -> data)
root -> right_child = delete(root -> right_child, x);
else if (x < root -> data)
root -> left_child = delete(root -> left_child, x);
else {
//No Child node
if (root -> left_child == NULL && root -> right_child == NULL) {
free(root);
return NULL;
}
//Two Children
else {
struct node *temp = find_minimum(root -> right_child);
root -> data = temp -> data;
root -> right_child = delete(root -> right_child, temp -> data);
}
}
return root;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) // checking if the root is not null
{
inorder(root -> left_child); // traversing left child
printf(" %d ", root -> data); // printing data at root
inorder(root -> right_child); // traversing right child
}
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
inorder(root);
printf("\n");
return 0;
}
OUTPUT:
1 5 7 9 12 15 20 25 30 40 42 45
5 7 12 15 20 25 30 42
Practical 16 and17
Aim: Implenentation of Binary search and Linear search
#include <stdio.h>
#include <conio.h>
# include <stdlib.h>
void main()
{
Int a[10],n,flag=0,i,lb,ub,key,mid,ch;
clrscr();
printf("enter the size of the elements\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter any key element to search\n");
scanf("%d",&key);
printf("menu\n");
printf("\n1.linear search\n2.binary search \n");
printf("enter your choice:\n"); scanf("%d",&ch);
switch(ch)
{ case 1:for(i=0;i<n;i++) if(a[i]==key)
{
flag=1;
break;}
case 2:for(lb=0,ub=n-1;lb<=ub;)
{
mid=(lb+ub)/2;
if(key==a[mid])
{
flag=1;
break;
}
else if(key<a[mid]) ub=mid-1;
else lb=mid+1;
}
84break;
default:exit(0);
}
if(flag==1)
printf("seach is successful");
else
printf("search is not successful \n");
getch();
}
OUTPUT:
Enter the size of the elements 5
Enter the elements 32 6 3 9 5
Enter any element to search 3
Menu
1. Linear search
2. Binary search Enter your choice: 2
Search is successful
Practical no. 18
Program: Implementation of Bubble Sort
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
void bubblesort(int x[],int n); void main()
{
intnum[10],i,n;
clrscr();
printf("Enter the no of elements\n"); scanf("%d",&n);
printf("Enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&num[i]);
bubblesort(num,n);
printf("sorted elements are\n"); for(i=0;i<n;i++) printf("%d\t",num[i]);
getch();}
void bubblesort(int x[],int n)
{
inthold,j,pass,K=TRUE;
for(pass=0;pass<n-1&&K==TRUE;pass++)
{ K=FALSE;
for(j=0;j<n-pass-1;j++) if(x[j]>x[j+1])
{
K=TRUE;
hold=x[j];
x[j]=x[j+1];
x[j+1]=hold;}}}
OUTPUT:
Enter the no of elements 5
Enter the elements 36 23 59 68 2
Sorted elements are 2 23 36 59 68
Practical no.19
Program: Implementation for Selection Sort
#include<stdio.h>
#include<conio.h> void main()
{
intn,i,j,a[10],min,t;
clrscr();
printf("enter how many elements\n"); scanf("%d",&n);
printf("enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
t=a[i];
a[i]=a[min];
a[min]=t;
87}