A Practical File of Data Structure Lab BCA-206: Session: 2020-21

You might also like

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

A

Practical File
Of
Data Structure Lab
BCA- 206
Session: 2020-21

Submitted by: Submitted to:


Submitted By: Gaurav
Gaurav Mehndiratta Mrs. aayushi
1320081
Roll no.: 1320081 (Assistant Professor)

M.M. INSTITUTION OF COMPUTER TECHNOLOGY & BUSINESS MANAGEMENT


MAHARISHI MARKANDESWAR (DEEMED TO BE UNIVERSITY)
MULLANA-AMBALA, HARYANA (INDIA) – 133207
INDEX

S. Name of Experiment Date of Page Signature


No. Practical No.

1. Write a program for Linear search on 1-D Array 24-04- 3


2021

2. Write a program for Binary search on 1-D Array. 26-04- 4


2021

3. Write a program to insert and delete an element in 1-D 03-05- 5-6


array. 2021

4. Write a program to allocate and de-allocate memory 10-05- 7


dynamically. 2021

5. Write a program to create, traverse and search an 17-05- 8-9


element in Singly Linked List. 2021

6. Write a program for inserting and deleting element in 24-05- 10-12


Singly Linked List. 2021

7. Write a program to create Stack and do push and pop 28-05- 13-15
operation. 2021

8. Write a program to implement Queue using an array. 31-05- 16-18


2021

9. Write a program for Bubble Sort 04-06- 19


2021

10. Write a program for Selection Sort. 11-06- 20


2021

11. Write a program for Insertion Sort. 18-06- 21


2021
12. Write a program for Merge Sort. 21-06- 22-23
2021

13. Write a program to reverse a string using stack. 05-07- 24


2021

14. Write a progrm to multiply of two matrix. 12-07- 25-26


2021
Pracrtical No: 1
Aim: Write a program for linear search on 1-D array.

Solution:

//Write a program for linear search on 1-D array....

#include<stdio.h>
#include<conio.h>
int main()
{
int array[50], search, c, n;
printf("Enter the size of array :");
scanf("%d",&n);
printf("Enter the element in the array :\n");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter the number you want to search :\n");
scanf("%d",&search);
for(c=0;c<n;c++)
{
if (array[c]==search)
{
printf("\n%d is present at position %d\n",search,c+1);
break;
}
}
if (c==n)
printf("% is not present at position in the array\n",search);

1
Output:

Fig 1.1: The output of linear seraching an element in 1-D array

2
Practical No: 2

Aim: Write a program for binary search on 1-D array.

Solution:

// Write a program for Binary search on 1-D array....

#include<stdio.h>
#include<conio.h>

int main()
{
int array[100], c, first,last, mid, n, search;
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the elements in array :");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("Enter the element you want to search :");
scanf("%d",&search);

first = 0;
last = n-1;
mid = (first+last)/2;
while(first<=last)
{
if(array[mid]<search)
first = mid + 1;
else if(array[mid] == search)
{
printf("%d is present at position %d",search, mid +1 );
break;
}
else last = mid -1;
mid = (first + last)/2;

if(first > last)


printf("%d is not present in the array",search);

3
Output:

Fig 1.2: The output of binary seraching an element in 1-D array

4
Practical No: 3

Aim: Write a program to insert and delete an element in 1-D array.

Solution:

//Write a program to insert and delete an element u 1-D array.....

#include<stdio.h>
#include<conio.h>

int main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k=3,n=5;
int i = 0, j = n;
int choice;
printf("The elements present in an array element are :\n");
for(i=0; i<n; i++)
{
/ printf("LA[%d]=%d\n",i,LA[i]);
}
printf("1. Inserting an element in an array\n");
printf("2. Deleting an element in an array\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1:
n = n+1;
while(j>=k)
{
LA[j+1]=LA[j];
j = j-1;
}
LA[k] = item;
printf("The array element after insertion :\n");
for(i=0;i<n;i++)
{
printf("LA[%d]=%d\n",i,LA[i]);
}
break;
case 2:
j = k;
while(j<n)
{
LA[j-1]=LA[j];
j=j+1;
}

5
n=n-1;

printf("The array element after deletion :\n");


for(i=0;i<n;i++)
{
printf("LA[%d]=%d\n",i,LA[i]);
}
break;
default:
printf("invalid choice");
}
}
Output:

Fig 1.3: The output of insertion of an element

Fig 1.4: The output of deletion of an element


6
Practical No: 4

Aim: Write program to Allocate and De-allocate memory dynamically.

Solution:

.//Write a program for dynamic memory allocation and deallocation..// (malloc, calloc, free)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
/ int i;
int *A, *B;
int N; //how many elements you want to unsert..
printf("\n Enter number of elements required :");
scanf("%d",&N);
//c...
// memory allocation..

A = (int*) malloc(N); // size of array not fixed.


//A= (int*) calloc(N,sizeof(int));
printf("\n Enter the array:\n");
for(i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
printf("\n The array is:\n");
for(i=0;i<N;i++)
{

printf("%d\t",A[i]);

free(A); //memory will be deallocate...


//c closed

7
Output:

Fig 1.5: The output of allocating and deallocating memory dynamically

8
Practical No: 5

Aim: Write a program to create, traverse and search an element in singly linked list.

Solution:

// Write a program to create, traverse and search an element in singly linked list...

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create(int);
void traverse();
struct node
{
int data;
struct node * next;
};
struct node*head;
int main()
{
int choice, item;
do
{

printf("\n1. Append list\n2.Traverse\n3.exit\n4.Enter your choice :");


scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\n Enter the item :");
scanf("%d",&item);
create(item);/
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
default:
printf("\n Please enter a valid choice :");
}
}
while(choice!=3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
9
if(ptr == NULL)

{
printf("\N OVERFLOW");
}
else
{/
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\n node inserted");
}
}
void traverse()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("empty list..");
}
else
{
printf("printing values...\n");
while(ptr!=NULL)
{
printf("\n %d",ptr->data);
ptr = ptr->next;
}
}
}
Output:

Fig 1.6: The output of to create, traverse and search an element in singly linked list
10
Practical No: 6

Aim: Write a program for inserting and deleting an element in singly linked list.

Solution:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>

struct node
/{
int data;
struct data *next;
};
struct node *head;

void Insert();
void Delete();
void Display();
int main()
{
int choice;
while(1)
{
printf("\n***Menu***\n");
printf("\n1.Insert\n2.Delete\n3.Display\n4.exit");
printf("\nEnter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
Display();
break;
case 4:
exit(0);
/ default:
printf("Please enter a valid choice..");
}
}

11
}

void Insert()
{
struct node*ptr;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the numbers :");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
/ printf("\n Node inserted is :%d\n",item);
}
}
void Delete()
{
struct node*ptr;
if(head == NULL)
{
printf("\n List is empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted is %d: ");
}
}
void Display()
{
struct node*ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\n Printing values...\n");
while(ptr!=NULL)
{
12
/ printf("\n%d",ptr->data);
ptr =ptr->next;
}
}
}

Output:/

Fig 1.7: The output of insertion and deletion and display in linked list

13
Practical No: 7

Aim: Write a program to create Stack and do push and pop operation.

Solution:

// Write a program to create Stack and do push and pop operation....

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();

int main()
{
/ int ch;
while(1)
{
printf("\n***stack menu***");
printf("\n\n1.push\n2.pop\n3.display\n4.Exit()");
printf("\n Enter your choice(1-4) :");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:
printf("\n wrong choice");
}
}
}
void push()
{

14
int n,x;
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d\n",stack[top]);
top--;
}
}
void display()
{
int i;
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n\nPress Next Choice\n");
}
else
{
printf("\n The STACK is empty");
}
}

Output:

15
Fig 1.8: The output of insert and delete and display element in stack

16
Practical No: 8

Aim: Write a program to implement Queue using an array.

Solution:

// Write a program to implement Queue using array...

#include<stdio.h>
#include<conio.h>
#include<process.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 ch;
while(1)
{
printf("\n1.Insert element in queue");
printf("\n2.Delete element from queue");
printf("\n3.Display all element of queue");
printf("\n4.Quite\n");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
Delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}

17
}
}

void insert()
{
int add_item;
if(rear == MAX - 1)
printf("Queue is overflow\n");
else
{
if(front == -1)
front = 0;
printf("Insert the element in queue:");
scanf("%d",&add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void Delete()
{
if(front == -1)
{
printf("Queue is underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d",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:

18
Fig 1.9: The output of insert, delete and display an element in queue.

19
Practical No: 9

Aim: Write a program for bubble short.

Solution:
// Write a program for bubble short...
#include<stdio.h>
#include<conio.h>

int main()
{
int array[100];
int n, i, j, swap;
printf("Enter number of elements :");
scanf("%d",&n);
printf("Enter %d integers\n",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if (array[j]>array[j+1])
{
swap = array[j];
array[j]= array[j+1];
array[j+1]= swap;
}
}
}
printf("sorted list in shorted form :");
for(i=0;i<n;i++)
printf("%d\n",array[i]);
printf("\n");

}
Output:

20
Fig 2.1: The output of shorted list

21
Practical No:10

Aim: Write a program for selection short.

Solution:

//Write a program for seletion short

#include<stdio.h>
#include<conio.h>

int main()
{
int size,i,j,temp,list[100];
printf("Enter the size of list:");
scanf("%d",&size);
printf("Enter %d inegter values :\n",size);
for(i=0;i<size;i++)
scanf("%d",&list[i]);
// selection short logic
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("List after shorting is :");
for(i=0;i<size;i++)
printf("%d\n",list[i]);
}

Output:

22
Fig 2.2: The output of selection short

23
Practical No:11

Aim: Write a program for Insertion short

Solution:

// Write a program on insertion short...

#include<stdio.h>
#include<conio.h>
int main()
{
int array[100],num,i,j,pos,temp;
printf("Enter the number of elements in array :");
scanf("%d",&num);
printf("Enter the numbers:");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
for(i=0;i<num;i++)
{
temp= array[i];
j=i;
while(j>0 && temp<array[j-1])
{
array[j]=array[j-1];
j=j-1;
}
array[j]=temp;
}
printf("\n the array sorted is :\n");
for(i=0;i<num;i++)
{
printf("%d\n",array[i]);

Output:

24
Fig 2.3: The output of insertion sort .

25
Practical No: 12

Aim: Write a program on Merge short.

Solution:

// Write a program for Merge short...

#include<stdio.h>
#include<conio.h>
#define max 10

int a[11]={10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0};
int b[10];
void merging(int beg, int mid, int end )
{

int beg_1, beg_2, i;

for(beg_1 = beg, beg_2 = mid + 1, i = beg; beg_1 <= mid && beg_2 <= end; i++)
{
if(a[beg_1] <= a[beg_2])
b[i] = a[beg_1++];
else
b[i] = a[beg_2++];
}

while(beg_1 <= mid)


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

while(beg_2 <= end)


b[i++] = a[beg_2++];
for(i = beg; i <=end; i++)
a[i] = b[i];
}

void sort(int beg, int end)


{
int mid;

if(beg < end)


{
mid = (beg + end) / 2;
sort(beg, mid);
sort(mid+1, end);
merging(beg, mid, end);
}
else
{
return;
26
}
}

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:

Fig 2.4: The output of merge short

27
Practical No: 13

Aim: Write a program to reverse a string using stack.

Solution:

#include<stdio.h>
#include<string.h>
#define max 100
int top, stack[max];
void push(char ch)
{
if(top==(max -1))
printf("stack is overflow");
else
stack[++top] = ch;
}
char pop()
{
if(top == -1)
printf("stack is underflow");
else
return stack[top--];
}
int main()
{
char str[20];
int i;
printf("Enter the string :\n");
gets(str);
for(i=0;i<strlen(str);i++)
{
push(str[i]);
}
for(i=0;i<strlen(str);i++)
{
str[i]=pop();
}
printf("Reversed string is :");
puts(str);
}
Output

28
Fig 2.5: The output of reversing a string.

29
Practical No: 14

Aim: Write a progrm to multiply of two matrix.

Solution:

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row :");
scanf("%d",&r);
printf("enter the number of column :");
scanf("%d",&c);
printf("enter the first matrix element :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix :\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
30
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output:

Fig 2.6: The output of multiply of two matrix.

31

You might also like