Data Structure Practical File: Submitted By: Submitted To
Data Structure Practical File: Submitted By: Submitted To
Data Structure Practical File: Submitted By: Submitted To
Practical File
Submitted By:
Ritwik Mishra
GB Pant Engineering College
01220902712
CSE 3rd Sem
Submitted To:
INDEX
S.No
Aim
Page No
Date of
submissio
n
Teachers
Signature
Program 1
Aim
Binary search and linear search in an array
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int c, f, l, m, n, s, a[100],k,q,w,temp;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&a[c]);
printf("Enter value to find\n");
scanf("%d",&s);
printf("\tUSE\n1)Binary Search\tOR\t2)Linear Search : ");
scanf("%d",&k);
if(k==1)
{f = 0;
l = n - 1;
m = (f+l)/2;
for(q=0;q<n;q++)
for(w=0;w<n-1;w++)
if(a[w]>a[w+1])
a[w]=a[w]+a[w+1]-(a[w+1]=a[w]);
printf("Now your array is:\n");
for(c=0;c<n;c++)
printf("%d,",a[c]);
while( f <= l )
{
printf("\n");
if ( a[m] < s )
f = m + 1;
else if ( a[m] == s )
{
printf("%d found at location %d.\n", s, m+1);
break;
}
else
l = m - 1;
m = (f + l)/2;
}
if ( f > l )
printf("Not found! %d is not present in the list.\n", s);}
else
{
for(c=0;c<n;c++)
if(a[c]==s)
{printf("%d found at %d position\n",s,c+1);break;}
if(c==n)
printf("Not found! %d is not present in the list.\n", s);
}
01220902712
system("pause");
return 0;
}
Output
01220902712
Program 2
Aim
Basic matrix operations
Source Code:
#include <stdio.h>
#include<stdlib.h>
int main()
{
int m,k, n, x, y, MatA[100][100], MatB[100][100];
int Result[100][100];
printf("Enter the number of rows and columns of matrices \nRows:");
scanf("%d",&m);
printf("Columns: ");
scanf("%d",&n);
printf("\t1)Addition\t\t2)Subtraction\nEnter your choice:");
scanf("%d",&k);
if(k==1)
{
printf("Enter the elements of Matrix A\n");
for ( x = 0 ; x < m ; x++ )
{
for ( y = 0 ; y < n ; y++ )
{
scanf("%d",&MatA[x][y]);
}
}
printf("Enter the elements of Matrix B\n");
for ( x = 0 ; x < m ; x++ )
{
for ( y = 0 ; y < n ; y++ )
{
scanf("%d",&MatB[x][y]);
}
}
for ( x = 0 ; x < m ; x++ )
{
01220902712
01220902712
Output
01220902712
Program 3
Aim
Implementation of stack using array
Source Code:
#include<stdio.h>
#include<stdlib.h>
const int n=25;
void push(int stack[],int &top,int n,int val)
{
if (top>0)
{top--;
stack[top]=val;}
else
printf("Stack overflow\n");
}
void pop(int stack[],int &top)
{
if(top<n&&top>=0)
{printf("Value deleted is %d\n",stack[top]);
top++;}
else
printf("Stack underflow\n");
}
void show(int stack[], int top)
{
01220902712
Output
01220902712
Program 4
Aim
01220902712
10
int main()
{
node *top;
top=NULL;
int k=0;
char ch='y';
01220902712
11
while(1)
{
printf("1)ENTER A RECORD 2)DELETE A RECORD 3)SHOW ALL THE RECORDS
4)EXIT\n\tenter: ");
scanf("%d",&k);
if(k==1)
top=push(top);
else if(k==2)
top=del(top);
else if(k==3)
show(top);
else
break;//to exit the loop
printf("\n");
}
system("pause");
return 0;
}
Output
Program 5
Aim
01220902712
12
01220902712
13
return 0;
}
Output
Program 6
Aim
Implementation of Queue using Linked List
01220902712
14
Source Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int a;
char b[20];
node *c;
};
void show(node *start)
{ node *temp;
temp=start;
while(temp!=NULL)
{
printf("Name: %s\n",temp->b);
temp=temp->c;
}
delete temp;
}
node *del(node *start)
{
node *temp=start;
start=start->c;
printf("popped value is:%s\t\tNo.%d\n",temp->b,temp->a);
return start;
}
int main()
{
node *temp,*start,*last;
start=last=NULL;
char ch='y';
printf("First enter the values:\n");
while(ch=='y'||ch=='Y')
{
temp=new node;
printf("Number.:");
scanf("%d",&temp->a);
printf("Name: ");
scanf("%s",&temp->b);
clearerr(stdin);
if (start==NULL)
{start=temp;}
else
{
last->c=temp;
temp->c=NULL;
}
last=temp;
scanf("%c",&ch);
printf("\n\t\tWant to enter more(y/n): ");
scanf("%c",&ch);
}
for(;;)
{
int z=0;
printf("Edits\n1)Show all 2)Delete one: ");
scanf("%d",&z);
if(z==1)
show(start);
01220902712
15
else if(z==2)
start=del(start);
else
break;
}
system("pause");
return 0;
}
Output
01220902712
16