








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
C programs for various data structures including arrays, matrix multiplication, binary search, sorting algorithms (bubble sort, insertion sort, merge sort, quick sort), and linked lists (queue). The programs demonstrate how to input elements, search, sort, and manipulate data structures using c.
What you will learn
Typology: Exams
1 / 14
This page cannot be seen from the preview
Don't miss anything!
Data Structure Program File
#include<stdio.h> int main() { int a[10][10], b[10][10], c[10][10], i, j, k; int sum = 0; printf("\nEnter First Matrix : n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &a[i][j]); } } printf("\nEnter Second Matrix:n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { scanf("%d", &b[i][j]); } } printf("The First Matrix is: \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", a[i][j]); } printf("\n"); } printf("The Second Matrix is : \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", b[i][j]); } printf("\n"); } //Multiplication Logic for (i = 0; i <= 2; i++) { for (j = 0; j <= 2; j++) { sum = 0; for (k = 0; k <= 2; k++) { sum = sum + a[i][k] * b[k][j]; } c[i][j] = sum; } } printf("\nMultiplication Of Two Matrices : \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf(" %d ", c[i][j]); } printf("\n"); } Output
return (0); }
/* Implementation of Linear Search*/ #include<stdio.h> #include<conio.h> void main() { char ch; int arr[50],n,i,item; clrscr(); printf("\nHow many elements you want to enter in the array:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter element %d:",i+1); scanf("%d",&arr[i]); } printf("\n\nPress any key to continue....."); getch(); do { clrscr(); printf("\nEnter the element to be searched:"); scanf("%d",&item); for(i=0;i < n;i++) { if(item == arr[i]) { printf("\n%d found at position %d\n",item,i+1); break; } } if (i == n) printf("\nItem %d not found in array\n",item); printf("\n\nPress (Y/y) to continue: "); fflush(stdin); scanf("%c",&ch); }while(ch == 'Y'|| ch == 'y'); }
/* Implementation of Binary Search */ #include<stdio.h> #include<conio.h> void main() { char ch; int arr[20],start,end,mid,n,i,data; Output
if(x[j]>x[j+1]) { tmp=x[j]; x[j]=x[j+1]; x[j+1]=tmp; } } } } void main() { int x[MAXSIZE],n,i; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("\nEnter the elements: \n"); for(i=0;i<n;i++) scanf("%d",&x[i]); bubble(x,n); printf("\nThe sorted output:\n"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }
#include<stdio.h> #include<conio.h> void insertion(int x[],int n) { int i,j,temp; for(i=0;i<n;i++) { temp=x[i]; for(j=i-1;j>=0;j--) { if(temp<x[j]) x[j+1]=x[j]; else break; } Output
x[j+1]=temp; } } void main() { int x[10],n,i; clrscr(); printf("Enter the number of elements: "); scanf("%d",&n); printf("\nEnter the elements: \n"); for(i=0;i<n;i++) scanf("%d",&x[i]); insertion(x,n); printf("\nThe sorted output:\n"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }
#include<stdio.h> #include<conio.h> #define MAX 20 int array[MAX]; void merge(int low, int mid, int high ) { int temp[MAX]; int i = low; int j = mid +1 ; int k = low ; while( (i <= mid) && (j <=high) ) { if (array[i] <= array[ j]) temp[k++] = array[i++] ; else temp[k++] = array[ j++] ; } while( i <= mid ) temp[k++]=array[i++]; while( j <= high ) temp[k++]=array[j++]; for (i= low; i <= high ; i++) array[i]=temp[i]; } void merge_sort(int low, int high ) { int mid; if ( low != high ) { Output
if (low < high) { j = partition(a,low, high); quicksort (a, low, j-1); quicksort(a, j+1, high); } } int partition(int a[], int low, int high) { int i, j, temp, key; key = a[low]; i = low + 1; j = high; while (1) { while (i < high && key >= a[i]) i++; while (key < a[j]) j--; if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } else { temp = a[low]; a[low] = a[j]; a[j] = temp; } return j; } }
/* Implementation of stack by Array */ #include <stdio.h> #include <conio.h> #define MAX 50 int stack [MAX+1], top = 0; void main ( ) { clrscr(); void create ( ), traverse ( ), push ( ), pop ( ); create ( ); printf("\n Stack is :\n"); Output
traverse ( ); push ( ); printf("After Push an element the stack is:\n"); traverse ( ); pop ( ); printf("After pop the element the stack is:\n"); traverse ( ); getch ( ); } void create ( ) { char ch; do { top ++; printf ("Input Element"); scanf ("%d", &stack[top]); printf ("Press
struct node ptr; ptr = (struct node) malloc (sizeof (struct node)); int item; printf ("Input the element for inserting :\n"); scanf ("%d", &item); ptr-> info = item; ptr->link = NULL; if (front==NULL) /* queue is empty*/ front = ptr; else rear->link = ptr; rear = ptr; void delet( ) { struct node *ptr; if (front==NULL) { printf ("Queue is underflow \n"); return; } if (front==rear) { free (front); rear = NULL; } else { ptr = front; front = ptr->link; free (ptr); } } void display ( ) { struct node *ptr; ptr = front; if (front==NULL) printf("Queue is empty\n"); else { printf("\nElements in the Queue are:\n"); while(ptr!=NULL) { printf(" %d\n",ptr->info); ptr=ptr->link; } printf("\n"); } } Output Output
/* Single linklist */ #include <stdio.h> #include <conio.h> #include <alloc.h> struct node { int info; struct node *link; }; node first; void main ( ) { void create ( ); clrscr ( ); create ( ); getch ( ); } void create ( ) { struct node * ptr, cpt; char ch; ptr = (struct node) malloc (sizeof (struct node)); printf("Input first node information"); scanf("%d", &ptr->info); first=ptr; do { cpt=(struct node) malloc(sizeof(struct node)); printf("Input next node information"); scanf("%d",&cpt->info); ptr->link = cpt; ptr = cpt; printf ("Press <Y/N> for more node"); ch = getch ( ); } while (ch=='Y'); ptr->link = NULL; } Output