Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 30

DATA STRUCTURES USING C LAB

SUBMITTED BY:
\

INDEX

S No.

Program

Remarks/Sign

1. WAP to implement operations on single dimensional arrays. (i) Insert an element in an array in a particular position. (ii) Delete a particular element from the array. (iii) Finding the largest element of the array. (iv) Searching for a particular element and displaying its position in the array. (v) Adding diagonal elements of an array. 2. WAP to implement operations on two dimensional arrays. (i) Implementing Matrix Addition (ii) Implementing Matrix Multiplication (iii) Finding the Transpose of a Matrix 3. (iv) Adding all the diagonal elements of the Matrix WAP to implement operations on stacks using 4. arrays. (i) PUSH (ii) POP 5. WAP to arrays. (i) (ii) 6. WAP to (i) (ii) 7 . WAP to (i) (ii) (iii) WAP to lists: (i) (ii) (iii) implement operations on queues using QINSERT QDELETE implement various Searching mechanisms. Linear Search Binary Search implement various sorting mechanisms: Bubble Sort Insertion Sort Selection Sort implement following operations on linked Insertion at the beginning of the list. Count the nodes in the list. Deleting from the beginning of the list.

(iv) (v) (vi)

Deleting from the end of the list. Insertion at the end of the list. Searching for a node in a list.

1. Write a program to implement operations on single dimensional arrays. (i) Insert an element in an array in a particular position. (ii) Delete a particular element from the array. (iii) Finding the largest element of the array. (iv) Searching for a element and displaying its position in the array. (v) Adding diagonal elements of an array.
#include<stdio.h> #include<conio.h> void main() { int array[50],i,j,k,n,item,option,large; clrscr(); printf("Enter number of elements in array "); scanf("%d",&n); printf("\nEnter elements of array "); for(i=1;i<=n;i++) scanf("%d",&array[i]); printf("\nChoose an operation to perform: \n1.Insert A New Element \n2.Delete An Element Find The Largest Element \n4.Search An Existing Element \t\t"); scanf("%d",&option); switch (option) { case 1: { /* INSERTION */ printf("\nEnter New Element "); scanf("%d",&item); printf("\nEnter Position to Insert New Element: "); scanf("%d",&k); for (j=n;j>=k;j--) array[j+1]=array[j]; array[k]=item; n=n+1; printf("Displaying New Array: "); for (i=1;i<=n;i++) printf("\t%d",array[i]); break; } case 2: { /* DELETION */ printf("Enter element to be deleted: "); scanf("%d",&item); for (i=1;i<=n;i++)

if (array[i]==item) j=i; for (k=j;k<=n;k++) array[k]=array[k+1]; n=n-1; printf("Displaying New Array: "); for (i=1;i<=n;i++) printf("\t%d",array[i]); break; } case 3: { /* LARGEST ELEMENT */ large=array[1]; for (i=1;i<=n;i++) if (array[i]>large) large = array[i]; printf("\nLargest Element Is: %d",large); break; } case 4: { printf("\nEnter Element To Be Searched: "); scanf("%d",&item); for (i=1;i<=n;i++) if (array[i]==item) j=i; printf("\nLocation Of The Element Is: %d",j); break; } default: { printf("\nInvalid Option Entered."); break; } } getch(); } ********** O U T P U T *********** Enter number of elements in array 5 Enter elements of array 1 2 3 4 5 Choose an operation to perform: 1.Insert A New Element 2.Delete An Element 3.Find The Largest Element 4.Search An Existing Element

Enter New Element 6 Enter Position to Insert New Element 3 Displaying New Array: 1 2 6

(v)adding the diagonal elements of matrix #include<stdio.h> #include<conio.h> void main() { int m1[10][10],r,c,i,j,sum=0; clrscr(); printf("enter the no of rows of matrices\n"); scanf("%d",&r); printf("enter the no of columns of matrices\n"); scanf("%d",&c); printf("enter the matrix\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&m1[i][j]); } } printf("adding diagonal elements of matrix is\n"); for(i=0;i<r;i++) { sum=m1[i][i]+sum; } printf("sum is %d ",sum); getch(); } ********OUTPUT********* enter the no of rows of matrices 2 enter the no of columns of matrices 2 enter the matrix 2 46 6 8 adding diagonal elements of matrix is sum is 10

2. WAP (i) (ii) (iii) (iv)

to implement operations on two dimensional arrays. Implementing Matrix Addition Implementing Matrix Multiplication Finding the Transpose of a Matrix Adding all the diagonal elements of the Matrix

#include<conio.h> #include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],m,n,p,i,j,k,option,sode=0; clrscr(); printf("Choose An Option:\n1.Add Two Matrices \n2.Multiply Two Matrices \n3.Find Transpose Of A Matrix \n4.Add Diagonal Elements Of A Matrix\n"); scanf("%d",&option); switch (option) { case 1: { /* Matrix Addition */ printf("\nEnter no. of rows and columns in matrices A & B: "); scanf("%d%d",&m,&n); printf("\nEnter elements of matrix A\n"); for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&a[i][j]); printf("\nEnter elements of matrix B\n"); for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&b[i][j]); for (i=1;i<=m;i++) for (j=1;j<=n;j++) c[i][j]=a[i][j]+b[i][j]; printf("Sum of matrices A and B is: "); for (i=1;i<=m;i++) { printf("\n"); for (j=1;j<=n;j++) printf("\t%d",c[i][j]); } break; } case 2: { /* Matrix Multiplication */ printf("\nEnter no. of rows and columns in matrix A: "); scanf("%d%d",&m,&p); printf("Enter no. of rows and columns in matrix B: ");

scanf("%d%d",&p,&n); printf("\nEnter elements of matrix A:\n"); for (i=1;i<=m;i++) for (j=1;j<=p;j++) scanf("%d",&a[i][j]); printf("\nEnter elements of matrix B:\n"); for (i=1;i<=p;i++) for (j=1;j<=n;j++) scanf("%d",&b[i][j]); for (i=1;i<=m;i++) for (j=1;j<=n;j++) { c[i][j]=0; for (k=1;k<=p;k++) c[i][j]+=a[i][k]*b[k][j]; } printf("Sum of matrices A and B is: "); for (i=1;i<=m;i++) { printf("\n"); for (j=1;j<=n;j++) printf("\t%d",c[i][j]); } break; } case 3: { /* TRANSPOSE OF MATRIX */ printf("\nEnter no. of rows and columns in matrix A: "); scanf("%d%d",&m,&n); printf("\nEnter elements of matrix A:\n"); for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&a[i][j]); for (i=1;i<=m;i++) for (j=1;j<=n;j++) b[j][i]=a[i][j]; printf("\nTranspose of matrix A is: "); for (i=1;i<=m;i++) { printf("\n"); for (j=1;j<=n;j++) printf("\t%d",b[i][j]); } break; } case 4: {

/* SUM OF DIAGONAL ELEMENTS */ printf("\nEnter no. of rows and columns in matrix A: "); scanf("%d%d",&m,&n); printf("\nEnter elements of matrix A:\n"); for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&a[i][j]); for (i=1;i<=m;i++) for (j=1;j<=n;j++) if (i==j) sode+=a[i][j]; printf("\nSum Of Diagonal Elements is: %d",sode); break; } default: { printf("\nInvalid Option Selected."); } } getch(); } ********** O U T P U T *********** Choose An Option: 1.Add Two Matrices 2.Multiply Two Matrices 3.Find Transpose Of A Matrix 4.Add Diagonal Elements Of A Matrix 1 Enter no. of rows and columns in matrices A & B: 2 2 Enter elements of matrix A 1111 Enter elements of matrix B 2222 Sum of matrices A and B is: 3 3 3 3

3. WAP to implement operations on stacks using arrays. (iii) PUSH (iv) POP
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int stack[50],top,maxstk,i,n,choice,item; clrscr(); printf("Enter maximum no. of elements in stack: "); scanf("%d",&maxstk); printf("\nEnter no. of elements originally in stack: "); scanf("%d",&n); if (n>maxstk) { printf("\nOverflow."); exit; } printf("\nEnter stack elements: "); for (i=1;i<=n;i++) scanf("%d",&stack[i]); top=n; printf("\nChoose an action to perform: \n1.PUSH an element \n2.POP an element \n3.Exit\t\t"); scanf("%d",&choice); while (choice!=3) { if(choice==1) /* PUSH */ { if (top==maxstk) printf("\nOverflow."); else { printf("\nEnter New Element: "); scanf("%d",&item); stack[top+1]=item; top=top+1; } printf("\nDisplaying Modified Stack: "); for(i=1;i<=top;i++) printf("\t%d",stack[i]); } else if (choice==2) /* POP */ { if(top==0) printf("\nUnderflow."); else

top=top-1; printf("\nDisplaying Modified Stack: "); for(i=1;i<=top;i++) printf("\t%d",stack[i]); } else printf("\nInvalid Option."); printf("\nChoose an action to perform: \n1.PUSH an element \n2.POP an element \n3.Exit\t\t"); scanf("%d",&choice); } getch(); } ********** O U T P U T *********** Enter maximum no. of elements in stack: 10 Enter no. of elements originally in stack: 5 Enter stack elements: 1 2 3 4 5 Choose an action to perform: 1.PUSH an element 2.POP an element 3.Exit 1 Enter New Element: 6 Displaying Modified Stack: 123456 Choose an action to perform: 1.PUSH an element 2.POP an element 3.Exit 2 Displaying Modified Stack: Choose an action to perform: 1.PUSH an element 2.POP an element 3.Exit 3

12345

4. WAP to implement operations on queues using arrays. (iii) QINSERT (iv) QDELETE
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int q[50],front,rear,i,n,choice,item; clrscr(); printf("Enter maximum no. of elements in queue: "); scanf("%d",&n); front=0; rear=0; for (i=1;i<=n;i++) q[i]=NULL; printf("\nChoose an action to perform: \n1.Insert an element \n2.Delete an element \n3.Exit\t\t"); scanf("%d",&choice); while (choice!=3) { if(choice==1) /* INSERTION */ { if (((front==1)&&(rear==n))||(front==rear+1)) printf("\nQueue Overflow."); else { printf("\nEnter New Element: "); scanf("%d",&item); if ((front==0)&&(rear==0)) front=rear=1; else if (rear==n) rear=1; else rear=rear+1; q[rear]=item; } printf("\nDisplaying Queue: "); for(i=1;i<=n;i++) printf(" %d",q[i]); } else if (choice==2) /* DELETION */ { if((front==0)&&(rear==0)) printf("\nQueue Underflow."); else { q[front]=NULL;

if (front==rear) front=rear=0; else if (front==n) front=1; else front=front+1; } printf("\nDisplaying Queue: "); for(i=1;i<=n;i++) printf(" %d",q[i]); } else printf("\nInvalid Option."); printf("\nChoose an action to perform: \n1.Insert an element \n2.Delete an element \n3.Exit\t\t"); scanf("%d",&choice); } getch(); } ********** O U T P U T *********** Enter maximum no. of elements in queue: 5 Choose an action to perform: 1.Insert an element 2.Delete an element 3.Exit 1 Enter New Element: 1 Displaying Queue: 1 0 0 0 0 Choose an action to perform: 1.Insert an element 2.Delete an element 3.Exit 1 Enter New Element: 2 Displaying Queue: 1 2 0 0 0 Choose an action to perform: 1.Insert an element 2.Delete an element 3.Exit 2 Displaying Queue: 0 2 0 0 0

5. WAP to implement various Searching mechanisms. (i) Linear Search (ii) Binary Search
LINEAR SEARCH #include<stdio.h> #include<conio.h> void main() { int a[50],i,n,item,loc=0; clrscr(); printf("Enter number of elements in array: "); scanf("%d",&n); printf("\nEnter array elements: "); for (i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nEnter element to be searched: "); scanf("%d",&item); for (i=1;i<=n;i++) if (a[i]==item) { loc=i; break; } if (loc!=0) printf("\nLocation of the element is: %d",loc); else printf("\nSearch Unsuccessful."); getch(); } ********** O U T P U T *********** Enter number of elements in array: 5 Enter array elements: 33 11 55 44 22 Enter element to be searched: 55 Location of the element is: 3

BINARY SEARCH #include<stdio.h> #include<conio.h> void main() { int a[50],i,n,item,mid,beg,end,loc=0; clrscr(); printf("Enter no. of elements in array: "); scanf("%d",&n); printf("\nEnter array elements: "); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nEnter element to be searched: "); scanf("%d",&item); beg=1; end=n; for(i=beg;i<=end;i++) { mid=(beg+end)/2; if (a[mid]==item) { loc=mid; break; } if (a[mid]>item) end=mid-1; if (a[mid]<item) beg=mid+1; } if (loc!=0) printf("\nLocation of the element is: %d",loc); else printf("\nSearch Unsuccessful"); getch(); } ********** O U T P U T *********** Enter no. of elements in array: 8 Enter array elements: 11 22 33 44 55 66 77 88 Enter element to be searched: 55 Location of the element is: 5

6. WAP to implement various Sorting mechanisms.

(i) (ii) (iii)

Bubble Sort Insertion Sort Selection Sort

BUBBLE SORT #include<conio.h> #include<stdio.h> void bubble(int arr[], int size); void main() { int a[50],i,n; clrscr(); printf("Enter number of elemens in array "); scanf("%d",&n); printf("\nEnter array elements : \n"); for (i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nOriginal array is : \n"); for (i=1;i<=n;i++) printf(" %d ",a[i]); bubble(a,n); getch(); } void bubble(int arr[],int size) { int j,k,temp; for (j=0; j<size; j++) { for (k=1; k<size-j; k++) { if (arr[k] > arr[k+1]) { temp = arr[k]; arr[k] = arr[k+1]; arr[k+1] = temp; } } } printf("\nArray after bubble sort is :\n"); for (j=1; j<=size; j++) printf(" %d ",arr[j]); }

********** O U T P U T ***********

Enter number of elements in array 10 Enter array elements: 33 66 44 22 55 11 77 99 88 50 Original array is: 33 66 44 22 55 11 77 99 88 50 Array after bubble sort is: 11 22 33 44 50 55 66 77 88 99 INSERTION SORT #include<stdio.h> #include<conio.h> #include<limits.h> void main() { int ar[50],n,t,i,j; clrscr(); printf("enter the no of elements"); scanf("%d",&n); printf("enter array elements"); for(i=0;i<n;i++) { scanf("%d",&ar[i]); } ar[0]=INT_MIN; for(i=1;i<=n;i++) { t=ar[i]; j=i-1; while(t<ar[j]) { ar[j+1]=ar[j]; j--; } printf("\narray after pass %d is\n",i); for(int k=0;k<=n;k++) printf("%d ",ar[j]); } printf("the sorted array is shown as"); for(int k=0;k<=n;k++) printf("%d ",ar[k]); getch(); }

**********OUTPUT********* enter the no of elements 4 enter array elements 6 8 2 5 array after paas 1 is 2865 array after pass 2 is 2568 array after pass 3 is 2568 array after pass 4 is 2568 the sorted array is shown as 2568 SELECTION SORT #include<stdio.h> #include<conio.h> void main() { int ar[50],n,small,t,pos,i,j; clrscr(); printf("enter the no of elements"); scanf("%d",&n); printf("enter array elements"); for(i=0;i<n;i++) { scanf("%d",&ar[i]); } { for(i=0;i<n;i++) { small=ar[i];; pos=i; for(j=i+1;j<n;j++) { if(ar[j]<small) { small=ar[j]; pos=j; } } t=ar[i]; ar[i]=ar[pos]; ar[pos]=t;

printf("\narray after pass %d is\n",i+1); for(j=0;j<n;j++) printf("%d",ar[j]); } } printf("the sorted array is shown as"); for(i=0;i<n;i++) { printf("%d ",ar[i]); } getch(); } *********OUTPUT********** enter the no of elements 4 enter array elements 6 8 2 5 array after paas 1 is 2865 array after pass 2 is 2568 array after pass 3 is 2568 array after pass 4 is 2568 the sorted array is shown as 2 5 6 8

7.

WAP to implement following operations on linked lists: (i) Insertion at the beginning of the list. (ii) Count the nodes in the list.

(iii) (iv) (v) (vi)

Deleting from the beginning of the list. Deleting from the end of the list. Insertion at the end of the list. Searching for a node in a list.

/*insertion at beginning of list*/ #include<stdio.h> #include<conio.h> struct node { int info; node *next; } *start,*newptr,*save,*ptr; void display(node*); void main() { start=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); printf("creating new node"); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr!=NULL) { printf("\nnew node created succesfully\n.press enter to continue"); getch(); } else { printf("cannot create new node"); getch(); } printf("\nnow inserting this node at beg of list\n press enter to continue\n"); getch(); if(start==NULL) start=ptr; else { save=start; start=ptr; ptr->next=save;

} printf("now the list is\n"); display(start); printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); getch(); } } void display(node *np) { while(np!=NULL) { printf("%d ",np->info); np=np->next; } printf("\n"); } ********OUTPUT******** enter information of node 7 creating new node new node created succesfylly press enter to continue now inserting this node at beg of list press enter to continue now the list is 7 enter y to enter more nodes and n to exit y enter information of node 9 creating new node new node created succesfylly press enter to continue now inserting this node at beg of list press enter to continue now the list is 97

/*counting the nodes in the list*/ #include<stdio.h> #include<conio.h> struct node {

int info; node *next; }*start,*newptr,*save,*ptr,*rear; void display(node*); void main() { start=rear=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr==NULL) { printf("cannot create new node"); getch(); } if(start==NULL) { start=ptr; rear=ptr; } else { rear->next=ptr; rear=ptr; } printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); } printf("now the list is\n"); display(start); getch(); } void display(node *np) { int n=0; while(np!=NULL) { printf("%d ",np->info); np=np->next; n++; } printf("\n no of nodes are%d",n); }

*******OUTPUT******* enter information of node 5 enter y to enter more nodes and n to exit y enter information of node 6 enter y to enter more nodes and n to exit 7 enter information of node 9 enter y to enter more nodes and n to exit n now the list is 5679 no of nodes are 4 /*Deleting from the beginning of the list*/ #include<stdio.h> #include<conio.h> struct node { int info; node *next; }*start,*newptr,*save,*ptr,*rear; void display(node*); void main() { start=rear=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr==NULL) { printf("cannot create new node"); getch(); } if(start==NULL) { start=ptr;

rear=ptr; } else { rear->next=ptr; rear=ptr; } printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); } clrscr(); do { printf("now the list is\n"); display(start); getch(); printf("want to delete first node y/n"); scanf("%s",&ch); if(ch=='y') { if(start==NULL) printf("underflow"); else { ptr=start; start=start->next; delete ptr; } } }while(ch=='y'); } void display(node *np) { while(np!=NULL) { printf("%d ",np->info); np=np->next; } printf("\n"); } *********OUTPUT********** enter information of node 5 enter y to enter more nodes and n to exit y enter information of node 6

enter y to enter more nodes and n to exit 7 enter information of node 9 enter y to enter more nodes and n to exit n now the list is 5679 want to delete first node y/n y now the list is 679 want to delete first node y/n n /*Deleting from the end of the list*/ #include<stdio.h> #include<conio.h> struct node { int info; node *next; }*start,*newptr,*save,*ptr,*rear; void display(node*); void main() { start=rear=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr==NULL) { printf("cannot create new node"); getch(); } if(start==NULL) { start=ptr; rear=ptr; } else { rear->next=ptr; rear=ptr;

} printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); } clrscr(); do { printf("now the list is\n"); display(start); getch(); printf("want to delete last node y/n"); scanf("%s",&ch); if(ch=='y') { if(start==NULL) printf("underflow"); else { ptr=rear; rear=rear->next; delete ptr; } } }while(ch=='y'); } void display(node *np) { while(np!=NULL) { printf("%d ",np->info); np=np->next; } printf("\n"); }

********OUTPUT********* enter 5 enter y enter 6 enter 7 enter information of node y to enter more nodes and n to exit information of node y to enter more nodes and n to exit information of node

9 enter y to enter more nodes and n to exit n now the list is 5679 want to delete last node y/n y now the list is 567 want to delete last node y/n n /*Insertion at the end of the list*/ #include<stdio.h> #include<conio.h> struct node { int info; node *next; }*start,*newptr,*save,*ptr,*rear; void display(node*); void main() { start=rear=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); printf("creating new node"); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr!=NULL) { printf("\nnew node created succesfully\n.press enter to continue"); getch(); } else { printf("cannot create new node"); getch(); } printf("\nnow inserting this node at end of list\n press enter to continue\n"); getch(); if(start==NULL) { start=ptr;

rear=ptr; } else { rear->next=ptr; rear=ptr; } printf("now the list is\n"); display(start); printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); getch(); } } void display(node *np) { while(np!=NULL) { printf("%d ",np->info); np=np->next; } printf("\n"); }

*********OUTPUT********* enter information of node 7 creating new node new node created succesfylly press enter to continue now inserting this node at end of list press enter to continue now the list is 7 enter y to enter more nodes and n to exit y enter information of node 9 creating new node new node created succesfylly press enter to continue now inserting this node at end of list press enter to continue now the list is 79 /*Searching for a node in the list*/

#include<stdio.h> #include<conio.h> struct node { int info; node *next; }*start,*newptr,*save,*ptr,*rear; void display(node*); void main() { start=rear=NULL; int inf; char ch='y'; while(ch=='y') { clrscr(); printf("enter information of node\n"); scanf("%d",&inf); ptr=new node; ptr->info=inf; ptr->next=NULL; if(ptr==NULL) { printf("cannot create new node"); getch(); } if(start==NULL) { start=ptr; rear=ptr; } else { rear->next=ptr; rear=ptr; } printf("\nenter y to enter more nodes and n to exit\n"); scanf("%s",&ch); } printf("now the list is\n"); display(start); getch(); } void display(node *np) { int item,n=0; while(np!=NULL) { printf("%d ",np->info);

np=np->next; n++; } printf("enter the item to be searched"); scanf("%d",&item); while(np!=NULL) { if(item==np->info) printf("the item is at loc%d",n); else { printf("the item is not found"); } } }

***********OUTPUT************ enter information of node 5 enter y to enter more nodes and n to exit y enter information of node 6 enter y to enter more nodes and n to exit 7 enter information of node 9 enter y to enter more nodes and n to exit n now the list is 5679 enter the item to be searched 6 item is found at loc 2

You might also like