0% found this document useful (0 votes)
5 views

Data Structure Lab File

Uploaded by

manjeet.amity3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure Lab File

Uploaded by

manjeet.amity3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

‭1.‬ ‭Implementation of‬‭Traversing Algorithms‬‭.


‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], num, i;‬
‭printf("Enter the number of elements : ");‬
‭scanf("%d", &num);‬
‭printf("Enter %d elements : \n", num);‬
‭for(i = 0; i < num; i++)‬
‭scanf("%d", &a[i]);‬
‭printf("The traversed array elements are : \n");‬
‭for(i = 0; i < num; i++)‬

p‭ rintf("Element no. %d is and value is : %d\n", i+1, a[i]);‬


‭return 0;‬
}‭ ‬
‭Output :‬

‭1‬
‭2.‬‭Implementation of‬‭Inserting Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], number, i, j, k, new_element;‬

p‭ rintf("Enter the number of elements : ");‬


‭scanf("%d", &number);‬

p‭ rintf("Enter elements :\n");‬


‭for(k = 1; k <= number; k++)‬
‭{‬
‭scanf("%d", &a[k]);‬
‭}‬

p‭ rintf("Enter the new elements to be inserted : ");‬


‭scanf("%d", &new_element);‬

p‭ rintf("New elements to be inserted in position : ");‬


‭scanf("%d", &j);‬

i‭ = number;‬
‭while(i >= j)‬
‭{‬
‭a[i+1] = a[i];‬
‭i--;‬
‭}‬
‭a[j] = new_element;‬
‭number = number + 1;‬

p‭ rintf("Array after insertion : \n");‬


‭for(k = 1; k <= number; k++)‬
‭printf("%d\n", a[k]);‬

‭return 0;‬
‭}‬

‭2‬
‭Output :‬

‭3‬
‭3.‬‭Implementation of‬‭deleting Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], number, i, j, k;‬
‭printf("\n");‬
‭printf("\n");‬
‭printf("Enter the number of elements : ");‬
‭scanf("%d", &number);‬
‭printf("Enter elements :\n");‬
‭for(k = 1; k <= number; k++)‬
‭scanf("%d", &a[k]);‬
‭printf("Enter the position of element to be deleted : ");‬
‭scanf("%d", &j);‬
‭i = j;‬
‭while(i <= number-1)‬
‭{‬
‭a[i] = a[i+1];‬
‭i++;‬
‭}‬
‭number = number - 1;‬
‭printf("Array after deletion : \n");‬
‭for(k = 1; k <= number; k++)‬
‭printf("%d\n", a[k]);‬
‭printf("\n");‬
‭printf("\n");‬
‭return 0;‬
‭}‬
‭Output :‬

‭4‬
‭4.‬ ‭Implementation of‬‭Selection Sort Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], num, i, k, temp, min, positions;‬
‭printf("\nEnter the number of elements : ");‬
‭scanf("%d", &num);‬
‭printf("Enter the array elements: \n");‬
‭for(i = 1; i <= num; ++i)‬
‭scanf("%d", &a[i]);‬
‭// interchange array elements‬
‭for(k=1; k <= num-1; k++)‬
‭{‬
‭min = a[k];‬
‭positions = k;‬
‭for(i=k+1; i <= num; ++i)‬
‭{‬
‭if(min > a[i])‬
‭{‬
‭min = a[i];‬
‭positions = i;‬
‭}‬
‭}‬
‭if(positions != k)‬
‭{‬
‭temp = a[k];‬
‭a[k] = a[positions];‬
‭a[positions] = temp;‬
‭}‬
‭}‬
‭printf("The sorted array is : \n");‬
‭for(k = 1; k <= num; k++)‬
‭{‬
‭printf("%d\n", a[k]);‬
‭}‬
‭return 0;‬
‭}‬

‭5‬
‭Output :‬

‭6‬
‭5.‬ ‭Implementation of‬‭Bubble Sort Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], num, i, j, temp;‬
‭printf("\nEnter the number of elements in the array : ");‬
‭scanf("%d", &num);‬
‭printf("Enter the array elements : \n");‬
‭for(i = 1; i <= num; ++i)‬
‭scanf("%d", &a[i]);‬
‭// interchange array elements‬
‭for(i = 1; i <= num-1; i++)‬
‭{‬
‭for(j = 1; j <= num-i; ++j)‬
‭{‬
‭if(a[j] > a[j+1]) {‬
‭temp = a[j];‬

a‭ [j] = a[j+1];‬
‭a[j+1] = temp;‬
‭}‬
‭}‬
}‭ ‬
‭// Output‬
‭printf("The sorted array is : \n");‬
‭for(i = 1; i<=num; i++)‬
‭printf("%d\n", a[i]);‬
‭return 0;‬
}‭ ‬
‭Output :‬

‭7‬
‭6.‬ ‭Implementation of‬‭Linear search Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[50], num, i, search, found = 0;‬
‭printf("\nEnter the number of elements : ");‬
‭scanf("%d", &num);‬
‭printf("Enter the array elements: \n");‬
‭for(i = 1; i <= num; ++i)‬
‭scanf("%d", &a[i]);‬
‭printf("Enter the elements to be searched for : ");‬
‭scanf("%d", &search);‬
‭// search for array elements‬
‭i = 1;‬
‭while(i<=num && found == 0)‬
‭{‬
‭if(a[i] == search)‬
‭found = 1;‬
‭i++;‬
‭}‬
‭// Output‬
‭if(found == 1)‬
‭printf("Elements found at positions : %d", i-1);‬
‭else‬
‭printf("Element not found");‬
‭return 0;‬
‭}‬
‭Output :‬

‭8‬
‭7.‬‭Explore the‬‭‘&’ operator and ‘*’ pointer‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int alpha = 1;‬
‭int *beta;‬

‭// print alpha address‬

p‭ rintf("The value %d is stored at this location : %d\n\n",alpha, &alpha);‬


‭printf("The value %d is stored at this location : %d\n\n",*(&alpha), &alpha);‬

‭// print pointer beta alpha address‬

b‭ eta = &alpha;‬
‭printf("The value %d is stored at this location : %d\n\n",beta, &beta);‬
‭printf("The value %d is stored at this location : %d\n\n",*beta, beta);‬

‭// print sizeof data types‬

p‭ rintf("Size of character is %d\n", sizeof(char));‬


‭printf("Size of int is %d\n", sizeof(int));‬
‭printf("Size of float is %d\n", sizeof(float));‬
‭printf("Size of double is %d\n", sizeof(double));‬
‭printf("Size of long is %d\n", sizeof(long));‬
‭printf("Size of long long int is %d\n", sizeof(long int));‬

‭// print array stored values‬

i‭nt a[20];‬
‭int i;‬

f‭ or(i=0; i<=5; ++i)‬


‭printf("Element no. is %d and address is %u\n", i, &a[i]);‬

‭return 0;‬
‭}‬

‭9‬
‭Output :‬

‭10‬
‭8.‬ ‭Implementation of‬‭Binary search Algorithms‬‭.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int i, first_element, last_element, middle_element, n, search, array[100];‬
‭printf("Enter number of elements:\n");‬
‭scanf("%d",&n);‬
‭printf("Enter %d integers:\n", n);‬
‭for (i = 0; i < n; i++)‬
‭scanf("%d",&array[i]);‬
‭printf("Enter the value to find:\n");‬
‭scanf("%d", &search);‬
‭first_element = 0;‬
‭last_element = n - 1;‬
‭middle_element = (first_element+last_element)/2;‬
‭while (first_element <= last_element) {‬
‭if (array[middle_element] < search)‬
‭first_element = middle_element + 1;‬
‭else if (array[middle_element] == search) {‬
‭printf("%d is present at index %d.\n", search, middle_element+1);‬
‭break;‬
‭}‬
‭else‬
‭last_element = middle_element - 1;‬
‭middle_element = (first_element + last_element)/2;‬
‭}‬
‭if (first_element > last_element)‬
‭printf("Not found! %d is not present in the list.\n", search);‬
‭return 0;‬
‭}‬

‭11‬
‭Output :‬

‭12‬
‭9.‬ ‭To demonstrate the use of dynamic allocation function‬‭malloc()‬‭. This program finds the sum and‬
‭average of all elements of the array.‬
‭Code :‬
‭#include<stdio.h>‬
‭#include<stdlib.h>‬
‭#define NULL 0‬
‭void main()‬
‭{‬
‭int *ptr, i, n, sum = 0;‬
‭float avg;‬
‭printf("Enter the elements you want to store in array : ");‬
‭scanf("%d", &n);‬
‭ptr = (int *) malloc (n * sizeof(int));‬
‭if(ptr == NULL)‬
‭{‬
‭printf("The required amount of memory is not available");‬
‭exit (0);‬
‭}‬
‭else‬
‭{‬
‭printf("Enter the elements :\n");‬
‭for(i = 0; i < n; i++)‬
‭scanf("%d", ptr+i);‬
‭for(i = 0; i < n; i++)‬
‭{‬
‭sum = sum + (*(ptr + i));‬
‭}‬
‭printf("Sum of %d elements of arrays is = %d\n", n, sum);‬
‭avg = (float)sum/n;‬
‭printf("The average of %d number of array is : %.2f", n, avg);‬
‭}‬
‭}‬
‭Output :‬

‭13‬
‭10.‬ ‭To demonstrate the use of dynamic allocation function‬‭calloc().‬‭This program finds the sum‬
‭and average of all elements of the array.‬
‭Code :‬
‭#include<stdio.h>‬
‭#include<stdlib.h>‬

v‭ oid main()‬
‭{‬
‭int *ptr, i, n, sum = 0;‬
‭float avg;‬
‭printf("Enter the number of elements : ");‬
‭scanf("%d", &n);‬
‭ptr = (int *) calloc(n, sizeof(int));‬
‭if(ptr == NULL)‬
‭{‬
‭printf("Memory not allocated");‬
‭exit(0);‬
‭}‬
‭else‬
‭{‬
‭printf("Enter the elements : \n");‬
‭for(i = 0; i < n; i++)‬
‭scanf("%d", ptr+i);‬
‭for(i = 0; i < n; i++)‬
‭{‬
‭sum = sum + (*(ptr + i));‬
‭}‬
‭printf("Sum of %d elements of arrays is = %d\n", n, sum);‬
‭avg = (float)sum/n;‬
‭printf("The average of %d number of array is : %.2f", n, avg);‬
‭printf("\n");‬
‭printf("\n");‬
‭}‬
‭}‬

‭14‬
‭Output :‬

‭15‬
‭11.‬ ‭Program of‬‭two - dimensional Array.‬
‭Code :‬
‭#include<stdio.h>‬
‭int main()‬
‭{‬
‭int a[3][3], i, j;‬
‭printf("Enter the array elements : \n");‬
‭for(i = 0; i <= 2; i++)‬
‭{‬
‭for(j = 0; j <= 2; j++)‬
‭{‬
‭scanf("%d", &a[i][j]);‬
‭}‬
‭}‬
‭printf("The element of array are : \n");‬
‭for(i = 0; i <= 2; i++)‬
‭{‬
‭for(j = 0; j <= 2; j++)‬
‭{‬
‭printf(" %d", a[i][j]);‬
‭}‬
‭printf("\n");‬
‭}‬
‭return 0;‬
‭}‬
‭Output :‬

‭16‬
‭12.‬ ‭Implementation of‬‭Stack as an Array.‬
‭Code :‬
‭#include<stdio.h>‬
‭#include<conio.h>‬
‭#define MAX_SIZE 20‬
‭int stack[MAX_SIZE],top=NULL;‬
‭main()‬
‭{‬
‭int ch, x;‬
‭printf("\nSTACK OPERATIONS");‬
‭printf("\n1. Push operation");‬
‭printf("\n2. Pop operation");‬
‭printf("\n3. Exit");‬
‭while(1)‬
‭{‬
‭printf("\nEnter your choice: ");‬
‭scanf("%d", &ch);‬
‭switch(ch)‬
‭{‬
‭case 1:‬
‭printf("\nEnter the number to be pushed : ");‬
‭scanf("%d", &x);‬
‭push(x);‬
‭break;‬
‭case 2:‬
‭x = pop();‬
‭if(x != NULL)‬
‭printf("\nEnter the number to be popped : %d", x);‬
‭else‬
‭printf("\n The stack is empty");‬
‭break;‬
‭case 3:‬
‭exit(0);‬
‭default:‬
‭printf("\nEnter correct choice");‬
‭break;‬
‭}‬
‭}‬
‭}‬
‭push(int x)‬

‭17‬
‭{‬
i‭f(top == MAX_SIZE)‬
‭{‬
‭printf("\n overflow");‬
‭return;‬
‭}‬
‭top= top+1;‬
‭stack[top]=x;‬
‭return;‬
}‭ ‬
‭int pop()‬
‭{‬
‭int x;‬
‭if(top==NULL)‬
‭return(NULL);‬
‭else‬
‭{‬
‭x=stack[top];‬
‭top=top-1;‬
‭return(x);‬

‭}‬
‭}‬

‭Output :‬

‭18‬
‭19‬
‭13.‬ ‭Implementation of‬‭Queue as An Array.‬
‭Code :‬
‭#include<stdio.h>‬
‭#define MAX_SIZE 50‬

‭int queue[MAX_SIZE], front = -1, rear = -1;‬

i‭nt main()‬
‭{‬
‭int choice, element;‬
‭printf("Queue Operations\n");‬
‭printf("1. Insert Operation\n");‬
‭printf("2. delete Operation\n");‬
‭printf("3. Exit\n");‬

‭ hile(1)‬
w
‭{‬
‭printf("Enter your choice : ");‬
‭scanf("%d", &choice);‬

s‭ witch(choice)‬
‭{‬
‭case 1:‬
‭printf("Enter number to be inserted : ");‬
‭scanf("%d", &element);‬
‭insert(element);‬
‭break;‬

‭case 2:‬
‭element = delete();‬
‭if(element != -1)‬
‭printf("The number deleted is : %d\n", element);‬
‭else‬
‭printf(" The queue is empty\n");‬
‭break;‬

c‭ ase 3:‬
‭exit(0);‬
‭default :‬
‭printf("Enter correct choice\n");‬

‭20‬
‭break;‬
‭}‬
‭}‬
‭}‬

‭int insert(int element)‬


‭{‬
‭if(rear == (MAX_SIZE - 1))‬
‭{‬
‭printf("Overflow");‬
‭return;‬
‭}‬
‭else‬
‭{‬
‭rear = rear + 1;‬
‭queue[rear] = element;‬
‭}‬

‭if(front == -1)‬
‭front = 0;‬
‭return(element);‬
‭}‬

i‭nt delete()‬
‭{‬
‭int element;‬
‭if(front == -1)‬
‭{‬
‭printf("Underflow");‬
‭return(-1);‬
‭}‬

e‭ lement = queue[front];‬
‭if(front == rear)‬
‭front = rear = -1;‬
‭else‬
‭front = front + 1;‬

i‭f(front >= MAX_SIZE)‬


‭{‬

‭21‬
f‭ ront = -1;‬
‭rear = -1;‬
‭}‬

‭return(element);‬
‭}‬

‭Output :‬

‭22‬
‭14.‬ ‭Implementation of‬‭Circular Queue.‬
‭Code :‬
‭#include<stdio.h>‬
‭#define MAX_SIZE 50‬

‭int queue[MAX_SIZE], front = -1, rear = -1;‬

i‭nt main()‬
‭{‬
‭int choice, element;‬
‭printf("Queue Operations\n");‬
‭printf("1. Insert Operation\n");‬
‭printf("2. delete Operation\n");‬
‭printf("3. Exit\n");‬

‭ hile(1)‬
w
‭{‬
‭printf("Enter your choice : ");‬
‭scanf("%d", &choice);‬

s‭ witch(choice)‬
‭{‬
‭case 1:‬
‭printf("Enter number to be inserted : ");‬
‭scanf("%d", &element);‬
‭insert(element);‬
‭break;‬

‭case 2:‬
‭element = delete();‬
‭if(element != -1)‬
‭printf("The number deleted is : %d\n", element);‬
‭else‬
‭printf("The queue is empty\n");‬
‭break;‬

c‭ ase 3:‬
‭exit(0);‬
‭default :‬
‭printf("Enter correct choice\n");‬

‭23‬
‭break;‬
‭}‬
‭}‬
‭}‬

‭int insert(int element)‬


‭{‬
‭if((front == 0 && rear == (MAX_SIZE - 1)) || (front == rear +1))‬
‭{‬
‭printf("Overflow\n");‬
‭return;‬
‭}‬

i‭f(front == -1)‬
‭{‬
‭front = rear = 0;‬
‭}‬
‭else‬
‭{‬
‭if (rear == MAX_SIZE - 1)‬
‭rear = 0;‬
‭else‬
‭rear = rear + 1;‬
‭}‬
‭queue[rear] = element;‬
‭return;‬
‭}‬

i‭nt delete()‬
‭{‬
‭int element;‬
‭if(front == -1)‬
‭{‬
‭printf("Underflow\n");‬
‭return(-1);‬
‭}‬

e‭ lement = queue[front];‬
‭if(front == rear)‬
‭front = rear = -1;‬

‭24‬
e‭ lse‬
‭{‬
‭if(front == MAX_SIZE-1)‬
‭front = 0;‬
‭else‬
‭front = front + 1;‬
‭}‬
‭return(element);‬
}‭ ‬
‭Output :‬

‭25‬

You might also like