Data Structures Using C' Programming Lab File: Gr. Noida
Data Structures Using C' Programming Lab File: Gr. Noida
Noida
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++) {
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(); }
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 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++)
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(); }
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(); }
insertsort(a,n); getch(); }
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"); }
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(); }
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; } }
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); }
} 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) }
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 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(); }