Vous êtes sur la page 1sur 54

Gr.

Noida

Data Structures Using C Programming Lab File


Course :- M.C.A IInd Semm.

Submitted ToMr. V. K. Pallav Sir

Submitted ByAnkur Gupta

Write a program to find the sum and difference of two matrixes


#include<stdio.h> #include<conio.h> void main() { int a[5][5],b[5][5],e[5][5],d[5][5],i,j,r,c; clrscr(); printf("Enter the value of Row:"); scanf("%d",&r); printf("\n Enter the value of Column:"); scanf("%d",&c); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("Enter the element a[%d][%d]:",i,j); scanf("%d",&a[i][j]); printf("\n"); } } for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("Enter the element b[%d][%d]:",i,j); scanf("%d",&b[i][j]); printf("\n"); e[i][j]=a[i][j]+b[i][j]; d[i][j]=a[i][j]-b[i][j]; } } printf("Elements of Matrix A:\n\n");

for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("%d",a[i][j]); printf("\t"); } printf("\n"); } printf("Elements of Matrix B:\n\n"); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("%d",b[i][j]); printf("\t"); } printf("\n"); } printf("\nSum of two Matrixes is \n\n"); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("%d",e[i][j]); printf("\t"); } printf("\n"); } printf("\nSubtraction of two Matrixes is \n\n"); for(i=1;i<=r;i++) {

for(j=1;j<=c;j++) { printf("%d",d[i][j]); printf("\t"); } printf("\n"); } printf("\n\nPress Any Key to Exit....."); getch(); }

Write a program to find the transpose matrix


#include<stdio.h> #include<conio.h> void main() { int a[5][5],i,j,r,c; clrscr(); printf("Enter the value of Row:"); scanf("%d",&r); printf("\n Enter the value of Column:"); scanf("%d",&c); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("\n Enter the element a[%d][%d]:",i,j); scanf("%d",&a[i][j]); printf("\n"); } } printf("Elements of Matrix A:\n\n"); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("%d",a[i][j]); printf("\t"); } printf("\n"); } printf("Transpose of Matrix A:\n\n"); for(i=1;i<=c;i++)

{ for(j=1;j<=r;j++) { printf("%d",a[j][i]); printf("\t"); } printf("\n"); } printf("\n\nPress Any Key to Exit...."); getch(); }

Write a program to find Transpose on Matrix


#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[3][3],i,j,r=0,l=0; printf("Enter the values of A:\n"); for (i=0;i<3;i++) { for (j=0;j<1;j++) scanf("%d",&a[i][j]); } printf("The given matrix is \n"); for (i=0;i<3;i++) { for (j=0;j<3;j++) { if(i==j) s=s+a[i][j]; } } for (i=2; i >0; i--) { for (j=0;j<3;j++) { if(i==2&&j==0||i==j||i==0&&j==2) l=l+a[i][j]; } } printf(\n The sum of left diagonal of matrix: %d,l); printf(\n The sum of right diagonal of matrix: %d,r); getch(); }

Write a program to find the product of two matrixes .


#include<stdio.h> #include<conio.h> void main() { int a[5][5],b[5][5],e[5][5],i,j,k,r,r1,c,c1,flag=0; clrscr(); printf("Enter the value of Row of a: "); scanf("%d",&r); printf("\n Enter the value of Column of a:"); scanf("%d",&c); printf("\n Enter the value of Row of b: "); scanf("%d",&r1); printf("\n Enter the value of Column of b:"); scanf("%d",&c1); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { printf("\n Enter the element a[%d][%d]:",i,j); scanf("%d",&a[i][j]); printf("\n"); } } for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { printf("\n Enter the element b[%d][%d]:",i,j);

scanf("%d",&b[i][j]); printf("\n"); } } printf("\nElements of Matrix A:\n\n"); for(i=1;i<=r;i++) { for(j=1;j<=c1;j++) { printf("%d",a[i][j]); printf("\t"); } printf("\n"); } printf("\nElements of Matrix A:\n\n"); for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { printf("%d",b[i][j]); printf("\t"); } printf("\n"); } if(c==r1) { flag=1; for(i=1;i<=r;i++) {

for(j=1;j<=c1;j++) { e[i][j]=0; for(k=1;k<=c;k++) { e[i][j]=e[i][j]+a[i][k]*b[k][j]; } } } } if(flag==1) { printf("\nProduct of two Matrixes is \n\n"); for(i=1;i<=r;i++) { for(j=1;j<=c1;j++) { printf("%d",e[i][j]); printf("\t"); } printf("\n"); } } else { printf("\nMultiplication Cannot Possible"); } printf("\n\nPress Any Key to Exit...."); getch(); }

Write a program of stack to insert ,delete, display &exit .


#include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int top=-1; int stack[size]; void PUSH(); void POP(); void DISPLAY(); void EXIT(); void main() { int ch; clrscr(); while(1) { printf("\n[1]PUSH\n[2]POP\n[3]DISPLAY\n[4]EXIT"); printf("Enter the choice="); scanf("%d",&ch); switch(ch) { case 1: PUSH(); break; case 2: POP(); break; case 3:

DISPLAY(); break; case 4: EXIT(); default: printf("Enter wrong choice"); } } } void PUSH() { int item; if(top==size-1) { printf("Stack is full"); } else { top=top+1; printf("Enter the element="); scanf("%d",&item); stack[top]=item; } } void POP() { int item; if(top==-1) {

printf("Stack is empty"); } else { item=stack[top]; top=top-1; printf("The deleted item="); scanf("%d",&item); } } void DISPLAY() { int i; for(i=top;i>=0;i--) { printf("%d",stack[i]); } } void EXIT() { exit(0);

Write a program for factorial number by recursion method.


#include<stdio.h> #include<conio.h> int Fact(int); void main() { int n,F; clrscr(); printf("Enter the number for factorial="); scanf("%d",&n); F=Fact(n); printf("The factorial number=%d",F); getch(); } int Fact(int m) { if(m==0) return(1); else if(m==1) return(1); else return(m*Fact(m-1)); }

Write a program for linear queue of insert ,delete, display & exit.
#include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int Q[5]; void Insert(); void Delete(); void Display(); void Exit(); int front=-1,rear=-1; void main() { int ch; clrscr(); while(1) { printf("\n[1]PUSH\n[2]POP\n[3]DISPLAY\n[4]EXIT\n"); printf("Enter the choice="); scanf("%d",&ch); switch(ch) { case 1: Insert(); break; case 2: Delete(); break;

case 3: Display(); break; case 4: Exit(); default: printf("Enter wrong choice"); } } } void Insert() { int item; if(rear==size-1) { printf(Lqueue is full); } else if((front==-1)||(rear==-1)) { front=0; rear=0; printf(Enter the item=); scanf(%d,&item); Q[rear]=item; } else { rear=rear+1; printf(Enter the item=);

scanf(%d,&item); Q[rear]=item; } } void Delete() { int item; if((front==-1)||(rear==-1)) { printf(Lqueue is empty); } else if(front==rear) { item=Q[front]; front=-1; rear=-1; } else { item=Q[front]; front=front+1; } Printf(The Deleted item=%d,item); } void Display() { int i; printf(\n The item of Queue=\n); for(i=front;i<=rear;i++)

{ Printf(\n%d,Q[i]); } } void Exit() { exit(); }

Write a program for Circular Queue for insert, delete, display& exit.
#include<stdio.h> #include<conio.h> #include<process.h> #define size 5 int Q[5]; void CInsert(); void CDelete(); void CDisplay(); void CExit(); int front=-1,rear=-1; void main() { int ch; clrscr(); while(1) { printf("\n[1]CInsert\n[2]CDelete\n[3]CDisplay\n[4]CExit\n"); printf("Enter the choice="); scanf("%d",&ch); switch(ch) { case 1: CInsert(); break; case 2: CDelete(); break;

case 3: CDisplay(); break; case 4: Exit(); default: printf("Enter wrong choice"); } } } void CInsert() { int item; if(front==(rear+1)%size) { printf(Cqueue is full); } else if((front==-1)||(rear==-1)) { front=0; rear=0; printf(Enter the item=); scanf(%d,&item); Q[rear]=item; } else { rear=(rear+1)%size; printf(Enter the item=);

scanf(%d,&item); Q[rear]=item; } } void CDelete() { int item; if((front==-1)||(rear==-1)) { printf(Cqueue is empty); } else if(front==rear) { item=Q[front]; front=-1; rear=-1; printf(The deleted item=%d,item); } else { item=Q[front]; front=(front+1)%size; Printf(The Deleted item=%d,item); } } void CDisplay() { int i; if(front==-1)

{ printf(Cqueue is empty\n); return; } if(front>rear) { for(i=front;i<size;i++) printf(\t\t%d,Q[i]); for(i=0;i<=rear;i++) printf(\t\t%d,Q[i]); } else { for(i=front;i<=rear;i++) { printf(%d,Q[i]); } printf(\n) } } void CExit() { exit(); }

Write a program for binary search from an array of 10 elements.


#include<stdio.h> #include<conio.h> void main() { int a[10],i,b,l,m,n; b=0; l=9; clrscr(); for(i=1;i<=10;i++) { printf("Enter the %d element:",i); scanf("%d",&a[i]); printf("\n"); } printf("Enter the value to Search:"); scanf("%d",&n); m=(b+l)/2; while(b<=l&&a[m]!=n) { if(n>a[m]) { b=1; } else { l=m-1; } m=(b+l)/2; } if(a[m]==n)

printf("\n Enter Value is Present=%d",n); else printf("\n Enter Value is Not Present=%d",n); printf("\n\nPress Any Key to Exit....."); getch(); }

Write a program for linear search.


#include<stdio.h> #include<conio.h> void main() { int a[10],i,n,flag=0; clrscr(); for(i=1;i<=10;i++) { printf("Enter the %d element:",i); scanf("%d",&a[i]); printf("\n"); } printf("Enter the value to Search:"); scanf("%d",&n); for(i=1;i<=10;i++) { if(a[i]==n) { flag=1; break; } else flag=0; } if(flag==1) printf("\n Enter Value is Present=%d",n); else printf("\n Enter Value is Not Present=%d",n); printf("\n\nPress Any Key to Exit....."); getch(); }

Write a program for Selection sort of an array of 10 elements.


#include<stdio.h> #include<conio.h> void main() { int a[10],i,j,k,temp=0; clrscr(); for(i=0;i<10;i++) { printf("Enter the %d element:",i); scanf("%d",&a[i]); printf("\n"); } for(i=0;i<10;i++) { for(j=i+1;j<9;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } Printf(The sorted element=); for(i=0;i<10;i++) { printf("%d\t",a[i]);

} printf("\n\nPress Any Key to Exit....."); getch(); }

Write a program for Bubble sort of an array of 10 elements.


#include<stdio.h> #include<conio.h> void main() { int a[10],i,j,temp=0; clrscr(); for(i=0;i<10;i++) { printf("Enter the %d element:",i); scanf("%d",&a[i]); printf("\n"); } for(i=0;i<10;i++) { for(j=0;j<9-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } Printf(After the sorted element=); for(i=0;i<10;i++) { printf("%d\t",a[i]);

} printf("\n\nPress Any Key to Exit....."); getch(); }

Write a program for Insertion sort of an array of 10 elements.


#include<stdio.h> #include<conio.h> #define size 10 void insertsort(int a[size], int n); void insertsort(int a[size], int n) { int i,k,y; for(k=1;k<n;k++) { y=a[k]; for(i=k-1;i>=0 && y<a[i];i--) { a[i+1]=a[i]; a[i]=y; } } for(i=0;i<n;i++) printf("%d",a[i]); } void main() { int a[size],i,n; clrscr(); printf("enter the no of element to sort"); scanf("%d",&n); printf("enter the element of array"); for(i=0;i<n;i++) scanf("%d",&a[i]);

insertsort(a,n); getch(); }

Write a program for Merge sort of an array of 10 elements.


#include<stdio.h> #include<conio.h> void main() { int arr1[20],arr2[20],arr3[40]; int i,j,k; int max1,max2; printf("Enter the number of elements in list1 : "); scanf("%d",&max1); printf("Take the elements in sorted order :\n"); for(i=0;i<max1;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr1[i]); } printf("Enter the number of elements in list2 : "); scanf("%d",&max2); printf("Take the elements in sorted order :\n"); for(i=0;i<max2;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr2[i]); } i=0; j=0; k=0; while( (i < max1) && (j < max2) ) {

if( arr1[i] < arr2[j] ) arr3[k++]=arr1[i++]; else arr3[k++]=arr2[j++]; } while( i < max1 ) { arr3[k++]=arr1[i++]; } while( j < max2 ) { arr3[k++]=arr2[j++]; } printf("List 1 : "); for(i=0;i<max1;i++) printf("%d ",arr1[i]); printf("\nList 2 : "); for(i=0;i<max2;i++) printf("%d ",arr2[i]); printf("\nMerged list : "); for(i=0;i<max1+max2;i++) printf("%d ",arr3[i]); printf("\n"); }

Write a program for Merge sort of an array of 10 elements.


#include<stdio.h> #include<conio.h> int partition(int a[],int p,int r) { int x,i,j,temp; x=a[r]; i=p-1; for(j=p;j<r-1;j++) { if(a[j]<=x) { i++; temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[i+1]; a[i+1]=a[r]; a[r]=temp; return (i+1); } void quick(int a[],int p,int r) { int q; if(p<r) { q=partition(a,p,r);

quick(a,p,q-1); quick(a,q+1,r); } } void main() { int a[100],n,p,r,i; clrscr(); printf("\n Enter the size of the array: "); scanf("%d",&n); printf("\n Enter the elements in the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); p=0; r=n-1; quick(a,p,r); printf(\nThe sorted array is: ); for(i=0;i<n;i++) printf(%d ,a[i]); getch(); }

Write a program to implement all operations on a linked list


#include<stdio.h> #include<alloc.h> #include<process.h> #include<conio.h> Struct node{ Int num; Struct node *link; }; Void addbeg(struct node **,int); Void append(struct node **,int); Void addpos(struct node **,int); Void delbeg(struct node **); Void delend(struct node **); Void delpos(struct node **); Void display(struct node *); Void main() { struct node *b; int num,choice; clrscr(); b=null; while(1) { printf("1.add at begining"); printf("\n2. Append "); printf("\n3.add at position"); printf("\n4.delete from begining"); printf("\n5.delete from end"); printf("\n6.delete from position"); printf("\n7. printf("\n8. Display "); Exit ");

printf("\n Enter your choice "); scanf("%d",&choice); switch(choice) { case 1:printf("enter the number "); scanf("%d",&num); addbeg(&b,num); break; case 2:printf("enter the number "); scanf("%d",&num); append(&b,num); break; case 3:printf("enter the number "); scanf("%d",&num); addpos(&b,num); break; case 4:printf("enter the number "); scanf("%d",&num); delbeg(&b); break; case 5:delend(&b); break; case 6:delpos(&b); break; case 7:display(b); break; case 8:exit(0); } } getch(); } Void addbeg(struct node **q,int num)

{ struct node *temp; temp=*q; temp=malloc(sizeof(struct node)); if(*q==null) { temp->num=num; temp->link=null; *q=temp; } else { temp->num=num; temp->link=*q; *q=temp; } clrscr(); } Void append(struct node **q,int num) { struct node *temp,*r; temp=*q; if(*q==null) { temp=malloc(sizeof(struct node)); temp->num=num; temp->link=null; *q=temp; } else { while(temp->link!=null)

temp=temp->link; r=malloc(sizeof(struct node)); r->num=num; r->link=null; temp->link=r; } clrscr(); } Void addpos(struct node **q,int num) { struct node *temp,*r; int loc,i; temp=*q; printf("\n Enter the location "); scanf("%d",&loc); for(i=0;i<loc;i++) { temp=temp->link; if(temp==null) { printf("not enough elements !!!"); return; } } r=malloc(sizeof(struct node)); r->num=num; r->link=temp->link; temp->link=r; clrscr(); } Void delbeg(struct node **q) {

struct node *temp; temp=*q; if(*q==null) { printf("node cannot be deleted !!!!"); return; } else { *q=temp->link; free(temp); } clrscr(); } Void delend(struct node **q) { struct node *temp,*old; temp=*q; while(temp!=null) {old=temp; temp=temp->link; } old->link=null; free(temp); clrscr(); } Void delpos(struct node **q) { struct node *temp,*old; int i,pos; printf("enter the position "); scanf("%d",&pos);

temp=*q; for(i=0;i<pos;i++) { old->link=temp->link; free(temp); return; } clrscr(); } Void display(struct node *q) { while(q!=null) { printf("%d ",q->num); q=q->link; } }

Write a program of Stack using linked list


# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; void insert(struct node **front, struct node **rear, int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->link=NULL; if(*rear == NULL) { *rear = temp; *front = *rear; } else { (*rear)->link = temp; *rear = temp; } } void delete(struct node **front, struct node **rear, int *value) {

struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty cannot delete Error\n"); exit(0); } *value = (*front)->data; temp = *front; *front = (*front)->link; if(*rear == temp) *rear = (*rear)->link; free(temp); } void main() { struct node *front=NULL,*rear = NULL; int n,value; do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(&front,&rear,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(&front,&rear,&value);

printf("The value deleted is %d\n",value); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }

Write a program of Queue using linked list


# include <stdio.h> # include <stdlib.h> struct node { int data; int priority; struct node *link; }; void insert(struct node **front, struct node **rear, int value, int priority) { struct node *temp,*temp1; temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->priority = priority; temp->link=NULL; if(*rear == NULL) /* This is the first node */ { *rear = temp; *front = *rear; } else { if((*front)->priority < priority) { temp->link = *front; *front = temp;

} else if( (*rear)->priority > priority) { (*rear)->link = temp; *rear = temp; } else { temp1 = *front; while((temp1->link)->priority >= priority) { temp1=temp1->link; temp->link = temp1->link; temp1->link = temp; } } void delete(struct node **front, struct node **rear, int *value, int *priority) { struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty cannot delete Error\n"); exit(0); } *value = (*front)->data; *priority = (*front)->priority; temp = *front; *front = (*front)->link; if(*rear == temp) *rear = (*rear)->link; free(temp);

} void main() { struct node *front=NULL,*rear = NULL; int n,value, priority; do { do { printf("Enter the element to be inserted and its priority\n"); scanf("%d %d",&value,&priority); insert(&front,&rear,value,priority); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(&front,&rear,&value,&priority); printf("The value deleted is %d\ and its priority is %d \n", value,priority); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to delete an element\n"); scanf("%d",&n); } while( n == 1) }

Write a program to merge the two Array


# include<stdio.h> # include<conio.h> void main( ) { int a[10],b[10],c[20],i,j,k; clrscr( ); printf ( Enter the Elements in first array); for ( i=0; i<10; i++) { scanf ( %d ,& a[i] ); } printf ( Enter the Elements in second array); for ( i=0; i<10; i++) { scanf ( %d ,& b[i] ); } i=0; j=0; k=0; while(i<10 && j<10) { if(a[i]<=b[j]) { c[k]=a[i]; i++; k++; } else { c[k]=b[j]; k++;

j++; } } while(i<10) { c[k]=a[i]; i++; k++; } while(j<10) { c[k]=b[j]; k++; j++; } printf(\n Merged array is:); for(i=0;i<20;i++) { printf(\n %d,c[i]); }

Write a program to read elements of a matrix and then counts and prints even and odd numbers in it.
# include<stdio.h> # include<conio.h> void main( ) { int a[3][3],even=0,odd=0,sum=0,i,j; clrscr( ); printf ( Enter the Elements in first array); for ( i=0; i<3; i++) { for(j=0;j<3;j++) scanf ( %d ,& a[i][j] ); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]%2==0) { printf(\n Even : %d,a[i][j]); even++; } else { printf(\n Odd : %d,a[i][j]); odd++ } printf(\n Total even numbers are: %d,even); printf(\n Total odd numbers are: %d,odd); getch(); }

Write a program to read elements of a matrix and find the largest and smallest numbers in it.
# include<stdio.h> # include<conio.h> void main( ) { int a[4][4],lar,small,i,j; clrscr( ); printf ( Enter the Elements in first array); for ( i=0; i<4; i++) { for(j=0;j<4;j++) scanf ( %d ,& a[i][j] ); } lar=a[0][0]; small=[0][0]; for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(lar<a[i][j]) lar=a[i][j]; if(small>a[i][j]) small=a[i][j]; } printf(\n The Largest value in matrix is: %d,lar); printf(\n The Smallest value in matrix is: %d,small); getch(); }

Write a program reverse of a String .


# include<stdio.h> # include<conio.h> void main( ) { char *s=ABCDEF; printf(\n Original String : %s\n,s); printf(\n Reverse String : %s\n,strrev(s)); getch(); }

Write a program to implement an array and perform the following operation: (i) Insert element at Kth location.

# include<stdio.h> # include<conio.h> void main( ) { int a[30],x,n,I,loc; printf(\n Enter no. of elements :); scanf(%d,&n); for(i=0;i<n;i++) { printf(Give Elements: %i,(i+1)); scanf(%d,&a[i]); } printf(\n Enter the element to be inserted :); scanf( %d,&x); printf(\n Enter the location); scanf(%d,&loc); for(i=n-1;i>=loc-1;i--) { a[i+1]=a[i]; } n++; a[loc-1]=x; for(i=0;i<n;i++) { printf(\n The new array is: %d,a[i]); } getch(); }

(ii) Delete the element at Kth location.


# include<stdio.h> # include<conio.h> void main( ) { int a[30],x,n,I,loc; printf(\n Enter no. of elements :); scanf(%d,&n); for(i=0;i<n;i++) { printf(Give Elements: %i,(i+1)); scanf(%d,&a[i]); } printf(\n Enter the location to delete); scanf(%d,&loc); for(i=n-1;i>=loc-1;i--) { a[i]=a[i+1]; } n--; for(i=0;i<n;i++) { printf(\n The new array is: %d,a[i]); } getch(); }

Vous aimerez peut-être aussi