0% found this document useful (0 votes)
52 views

ReferenceDataStructure Praactical

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

ReferenceDataStructure Praactical

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Date Structures Lab Using C/C++

Subject Code: MCAL11


A Practical Journal Submitted in Fulfilment of the Degree of
MASTER
In
COMPUTER APPLICATION
Year 2022-2023
By
Mr. Yadav Uday Sagindra Poonam
(Application Id:- 168857)

Under the Guidance of


Ms. Kavita Chouk

Institute of Distance and Open Learning


Vidya Nagari, Kalina, Santacruz East – 400098.
University of Mumbai

PCP Center
[Satish Pradhan Dyanasadhana College, Thane]
Institute of Distance and Open Learning
Vidyanagari, Kalina, Santacruz (E) -400098

CERTIFICATE

This to certify that, Yadav Uday Sagindra Poonam appearing Master in


Computer Application (Semester I) Application ID: 168857 has satisfactory
completed the prescribed Practical of MCAL11- Data Structures Lab Using
C/C++ as laid down by the University of Mumbai for the academic year 2022-
23

Teacher in charge Examiners Coordinator


IDOL, MCA
University of Mumbai
Date: -
Place: -
Index

Module Practical Page Sign.


No No.
I 1. Implement program for Bubble Sort.
2. Implement program for Insertion Sort.
3. Implement program for Selection Sort.

II 1. Implement program for Linear Search.


2. Implement program for Binary Search.
III Implement program for Stack using array.

IV Implementation program for Queue.

V Implementation program for Linked List.


VI Creating Binary Search Tree.

VII Graph creation using adjacency matrix.


Data Structures Lab Using C/C++

Module: - I
1] Bubble Sort:
Aim: Implement program for Bubble Sort.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n, temp, j, arr[10];
printf("Enter the maximum elements you want to store : ");
scanf("%d", &n);
printf("Enter the elements \n");
for(i=0;i<n;i++)
{
scanf("%d", & arr[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
1
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

printf("%d\t", arr[i]);
return 0;
}

Output:

2] Insertion Sort:
Aim: Implement program for Insertion Sort.
Code:
#include<stdio.h>
#include<conio.h>
int main ()
{
int i, j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
printf("\nprinting sorted elements...\n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
2
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

}
for(i=0;i<10;i++)
{
printf("\n%d\n",a[i]);
}
getch();
return 0;
}

Output:

3] Selection Sort:
Aim: Implement program for Selection Sort.
Code:
#include<stdio.h>
#include<conio.h>
int smallest(int[],int,int);
void main ()
{

3
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
printf("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
int smallest(int a[], int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]<small)
{
small = a[j];
pos=j;
}
}
getch();
return pos;

4
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

}
Output:

5
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

6
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - II

1] Linear Search:
Aim: Implement program for Linear Search.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10]={10,23,40,1,2,0,14,13,50,9};
int item,i,flag;
printf("\n Enter item which is to be searched\n");
scanf("%d",&item);
for(i=0;i<10;i++)
{
if(a[i]==item)
{
flag=i+1;
break;
}
else
{
flag=0;
}
}
if(flag!=0)
{
printf("\nItem found at location %d\n",flag);
}
else
7
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

{
printf("\nItem not found\n");
}
getch();

}
output:

2] Binary Search:
Aim: Implement program for Binary Search.
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int f,l,m,s,i,k,list[100];
printf("Enter the size of the list:");
scanf("%d",&s);
printf("Enter %d integer values in Ascending border \n",s);
for(i=0;i<s;i++)
{
scanf("%d",&list[i]);

8
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

}
printf("Enter value to be search:");
scanf("%d",&k);
f=0;
l=s-1;
m=(f+l)/2;
while(f<=l)
{
if(list[m]<k)
{
f=m+1;
}
else if(list[m]==k)
{
printf("Elemnet found at index %d \n",m);
break;
}
else
{
l=m-1;}
m=(f+l)/2;
}
if(f>l)
{
printf("Element Not foubd in the list");
}
getch();
}

9
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Output:

10
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - III
1] Stack using array:
Aim: Implement program for Stack using array.
Code:
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
int main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("***Stack operations using array***");
printf("\n \n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
11
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

pop();
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting ... ");
break;
}
default:
{
printf("Please Enter valid choice ");
}
}
}
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;

12
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}

Output:

13
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

14
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - IV

Queue:
Aim: Implementation program for Queue.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
int queue_array[MAX];
int rear = -1;
int front = -1;
insert()
{
int add_item;
if(rear==MAX-1)
printf("Queue 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;
}
return 1;
}

deleteq()
15
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

{
if(front == -1 || front>rear)
{
printf("Queue Underflow\n");
return 1;
}
else
{
printf("Element deleted from queue is:\t");
printf("%d",queue_array[front]);
front=front+1;
}
return 1;
}
display()
{
int i;

if(front == -1 || front>rear)
{
printf("Queue is empty\n");
}
else
{
printf("Queue is : \n");
for(i=front;i<=rear;i++)
printf("%d",queue_array[i]);
printf("\n");
}
return 1;

16
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

void main()
{
int ch;
//clrscr();
while(1)
{
printf("\n1. Insert\n");
printf ("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: deleteq();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\n Wrong choice\n");
}
}
}

17
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Output:

18
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - V

Linked List:
Aim: Implementation program for Linked List.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* create(int n);
void display(struct node* head);
void main()
{
int n=0;
//clrscr();
struct node* head=NULL;
printf("Enter the how many nodes\n");
scanf("%d",&n);
head=create(n);
display(head);
getch();
}

struct node* create(int n)


{
int i=0;
19
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

struct node* head=NULL;


struct node* temp=NULL;
struct node* p=NULL;
printf ("\nEnter the element of the linklist");
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node*));
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
p=head;
while(p->next!=NULL)
p=p->next;
p->next=temp;
}
}
return head;
}
void display(struct node*head)
{

printf("linked list is");


struct node* p=head;
while(p!=NULL)
{

20
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

printf(" %d",p->data);
printf("->");
p=p->next;
}
printf("NULL");
}
Output:

21
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - VI
BSF Tress:
Aim: Creating Binary Search Tree.
Code:
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>

void insert(int);
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
int main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insert(item);
printf("\n Press 0 to insert more? \n");
scanf("%d",&choice);
}while(choice == 0);
return 0;
}
void insert(int item)
22
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

{
struct node *ptr, *parentptr , *nodeptr;
ptr = (struct node *) malloc(sizeof (struct node));
if(ptr == NULL)
{

printf("cannot insert");
}
else
{
ptr -> data = item;
ptr -> left = NULL;
ptr -> right = NULL;
if(root == NULL)
{
root = ptr;
root -> left = NULL;
root -> right = NULL;
}
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
{
parentptr = nodeptr;
if(item < nodeptr->data)
{
nodeptr = nodeptr -> left;
}

23
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

else
{
nodeptr = nodeptr -> right;
}
}
if(item < parentptr -> data)
{
parentptr -> left = ptr;

}
else
{
parentptr -> right = ptr;
}
}
printf("Node Inserted");
}
}

Output:

24
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Module: - VII
1] Adjacent Matrix of graph:
Aim: Graph creation using adjacency matrix
Code:
#include<stdio.h>
int vertArr[20][20]; //the adjacency matrix initially 0
int count = 0;
void displayMatrix(int v) {
int i, j;
for(i = 0; i < v; i++) {
for(j = 0; j < v; j++) {
printf("%d ",vertArr[i][j]);
}
printf("\n");
}
}
void add_edge(int u, int v) { //function to add edge into the matrix
vertArr[u][v] = 1;
vertArr[v][u] = 1;
}
int main() {
int v = 5; //there are 6 vertices in the graph
add_edge(0, 1);
add_edge(0, 2);
add_edge(0, 4);
add_edge(1, 3);
add_edge(3, 2);
add_edge(2, 4);
displayMatrix(v);
return 0;

25
Name: Yadav Uday Sagindra
Application ID: 168857
Data Structures Lab Using C/C++

Output:

26
Name: Yadav Uday Sagindra
Application ID: 168857

You might also like