0% found this document useful (0 votes)
34 views16 pages

Data Structure Practical File: Submitted By: Submitted To

This document contains 6 programs implementing different data structures using C programming language. The programs implement binary search and linear search in an array, basic matrix operations, a stack using array and linked list, and a queue using array and linked list. Each program includes the aim, source code, and sample output.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views16 pages

Data Structure Practical File: Submitted By: Submitted To

This document contains 6 programs implementing different data structures using C programming language. The programs implement binary search and linear search in an array, basic matrix operations, a stack using array and linked list, and a queue using array and linked list. Each program includes the aim, source code, and sample output.

Uploaded by

Sachin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Structure

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

for ( y = 0 ; y < n ; y++ )


{
Result[x][y] = MatA[x][y] + MatB[x][y];
}
}
printf("Resultant Matrix after Addition- \n");
for ( x = 0 ; x < m ; x++ )
{
for ( y = 0 ; y < n ; y++ )
printf("
%d",Result[x][y]);
printf("\n");
}}
else if(k==2)
{
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++ )
{
for ( y = 0 ; y < n ; y++ )
{
Result[x][y] = MatA[x][y] - MatB[x][y];
}
}
printf("Resultant Matrix after Subtraction- \n");
for ( x = 0 ; x < m ; x++ )
{
for ( y = 0 ; y < n ; y++ )
printf("
%d",Result[x][y]);
printf("\n");
}
}
else
{
system("cls");
printf("Exiting\n");
}
system("pause");
return 0;
}

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

printf("Stored values are: ");


for(int i=top;i<n;i++)
printf("%d",stack[i]);
printf("\n");
}
int main()
{ int stack[n];
int top=n;
int val=0;
int choice;
do
{
if(top==n)
printf("\t\tStack is empty\n");
printf("1)Insert 2)Delete 3)Show 4) quit\n\tenter: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter: ");
scanf("%d",&val);
push(stack,top,n,val);
break;
case 2: pop(stack,top);break;
case 3: show(stack, top);break;
}}while(choice>=1 && choice<=3);
system("pause");
return 0;
}

Output

01220902712

Program 4
Aim
01220902712

10

Implementation of stack using Linked List


Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int a;
char b[20];
node *c;
};
node *push(node *top)
{
node *temp=NULL;
temp=new node;
printf("No.:");
scanf("%d",&temp->a);
printf("Name: ");
scanf("%s",&temp->b);
if(top!=NULL)
temp->c=top;
else
{temp->c=NULL;}
top=temp;
return top;
}
void show(node *top)
{
node *temp=NULL;
temp=top;
while(temp!=NULL)
{
printf("Name:%s \n",temp->b);
temp=temp->c;
}
}
node* del(node *top)
{
node *temp=NULL;
if (top!=NULL)
{
temp=top;
top=top->c;
printf("Record deleted is:%d %s\n",temp->a,temp->b);
delete temp;
}
else
printf("Stack underflow");
return top;
}

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

Implementation of Queue using an array


Source Code:
#include<stdio.h>
#include<stdlib.h>
const int n=25;
void push(int que[],int &rear,int &front,int n,int val)
{
if (rear==-1)
{front++;
rear++;
que[rear]=val;}
else if(rear<n-1)
que[++rear]=val;
else
printf("Stack overflow\n");;
}
void pop(int que[],int &front,int &rear)
{
int val;
if(front==rear)
{
printf("value deleted is %d\n",que[front]);
rear=front=-1;
}
else if(front<rear)
{printf("value deleted is %d\n",que[rear]); rear--;}
else if(front=-1)
printf("Queue underflow\n");
}
void show(int que[],int front, int rear)
{
if(front==-1)
{printf("\tQueue Empty\n");return;}
printf("Data in queue is:");
for(int i=front;i<=rear;i++)
printf("%d,",que[i]);
printf("\n");
}
int main()
{
int que[n];
int rear=-1;
int front=-1;
int val;
int choice;
do
{
printf("1)Insert 2)delete 3)Show 4) quit\n\tenter: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter: ");
scanf("%d",&val);
push(que,rear,front,n,val);
break;
case 2: pop(que,front,rear);break;
case 3: show(que,front,rear);break;}
}while(choice>=1 && choice<=3);
system("pause");

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

You might also like