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

DFS Lab File

Lab file

Uploaded by

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

DFS Lab File

Lab file

Uploaded by

mocetow663
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 229

1. Write A C Program to Perform These Operation on An Array.

1.Traverse

2.Insert

3.Delete

4.Linear Search

5.Binary Search

6.Binary Search Using Recursion

7.Bubble Sort

8.Insertion Sort

9.Selection Sort

10.Find Second Largest Element in Array

Shuffle

Exit

#include <stdio.h>

int traverse(int *array, int size);

int insert(int *array, int size, int idx, int key);

int delete(int *array, int size, int key);

int linear_search(int *array, int size, int key);

int binary_search(int *array, int size, int key);

int bubble_sort(int *array, int size);


int insertion_sort(int *array, int size);

int selection_sort(int *array, int size);

int second_largest(int *array, int size);

int main(){

int array[100];

int size, key;

int idx;

int temp;

int choice2;

int choice;

int choice3;

printf("Enter the size of array: ");

scanf("%d", &size);

printf("Enter the element in array: \n");

for (int i = 0; i < size; i++){

scanf("%d", &array[i]);

printf("Entered element in array is: \n");


for (int i = 0; i < size; i++){

printf("%d\t", array[i]);

do{

printf("\nMenuDriven\n");

printf("1. Traverse \n");

printf("2. Insert \n");

printf("3. Delete \n");

printf("4. Search \n");

printf("5. Sorting \n");

printf("6. Second largest number \n");

printf("7. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice){

case 1:

// Traverse the array

printf("You selected Option 1.\n");

traverse(array, size);

break;

case 2:

// Insert Value in array

printf("You selected Option 2.\n");

insert(array, size, idx, key);

break;

case 3:

// Delete value in array

printf("You selected Option 3.\n");

delete (array, size, key);

break;

case 4:

// Searching value in array


printf("You selected Option 4.\n");

do{

printf("\n");

printf("1. Linear Search \n");

printf("2. Binary Search \n");

printf("3. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice2);

switch (choice2){

case 1:

// Linear Search in array

printf("You selected Option 1.\n");

linear_search(array, size, key);

break;

case 2:

// Binary Search in array

printf("You selected Option 2.\n");


binary_search(array, size, key);

break;

case 3:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice2 != 3);

break;

case 5:

// Sorting of array

printf("You selected Option 5.\n");

do{

printf("\n");

printf("1. Bubble Sort \n");


printf("2. Insertion Sort \n");

printf("3. Selection Sort \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice3);

switch (choice3){

case 1:

// Bubble sort of the array

printf("You selected Option 1.\n");

bubble_sort(array, size);

break;

case 2:

// Insertion sort of the array

printf("You selected Option 2.\n");

insertion_sort(array, size);

break;

case 3:
// Selection sort of the array

printf("You selected Option 3.\n");

selection_sort(array, size);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice3 != 4);

break;

case 6:

// Second largest number of the array

printf("You selected Option 6.\n");

second_largest(array, size);

break;
case 7:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice != 7);

return 0;

// Traverse the array

int traverse(int *array, int size){

int i;

printf("Entered values of array are:\n");

for (i = 0; i < size; i++){

printf("%d\t", array[i]);

}
return 0;

// Insert Value in array

int insert(int *array, int size, int idx, int key){

int i;

printf("Enter position where you want to insert:");

scanf("%d", &idx);

printf("Enter the value want to insert in array:");

scanf("%d", &key);

for (i = size; i >= idx; i--){

array[i] = array[i - 1];

array[idx - 1] = key;

size++;

printf("Updated array is:\n");

for (int i = 0; i < size; i++){

printf("%d\t", array[i]);
}

return 0;

// Delete value in array

int delete(int *array, int size, int key){

int count;

int i, j;

printf("Enter the value want to delete in array:");

scanf("%d", &key);

for (i = 0; i < size; i++){

if (array[i] == key){

for (j = i; j < size - 1; j++){

array[j] = array[j + 1];

size--;

count++;

}
if (count == 0){

printf("Entered value want to delete is not found in array.\n");

else{

printf("Updated array after deletion is:\n");

for (i = 0; i < size; i++){

printf("%d\t", array[i]);

return 0;

// Linear search of value in array

int linear_search(int *array, int size, int key){

int i;

int count = 0;

int flag = 0;

printf("Enter the value want to search in array:");

scanf("%d", &key);
for (i = 0; i < size; i++){

if (array[i] == key){

if (flag == 0){

printf("\nThe element is found at position: ");

flag = 1;

printf("%d\n", i + 1);

count++;

if (count > 0){

printf("Searching value %d present %d times", key, count);

else if (count == 0){

printf("searching value is not exist in array");

printf("\n");

return 0;

// Binary search of value in array


int binary_search(int *array, int size, int key){

int i, j;

int temp, start, end, mid;

int count = 0;

for (i = 0; i < size; i++){

for (j = 0; j < size - i; j++){

if (array[j] > array[j + 1]){

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

printf("Updated array after sorting is:\n");

for (i = 0; i < size; i++){

printf("%d\t", array[i]);

Printf(“\n”);

printf("Enter the value want to search in array:");

scanf("%d", &key);
start = 0;

end = size - 1;

while (start <= end){

mid = (start + end) / 2;

if (array[mid] == key){

printf("Searched value present at index %d in sorted array", mid);

count++;

break;

else if (array[mid] < key){

start = mid + 1;

else if (array[mid] > key){

end = mid - 1;

if (count == 0){

printf("Searched value is not present in array.");


}

printf("\n");

return 0;

// Bubble sorting

int bubble_sort(int *array, int size){

int i, j;

int temp;

for (i = 1; i < size; i++){

for (j = 0; j < size - i; j++){

if (array[j] > array[j + 1]){

temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

Printf(“\n”);

printf("sorted of array after phase %d is:\n", i);

for (j = 0; j < size; j++){


printf("%d\t", array[j]);

return 0;

// Insertion sorting

int insertion_sort(int *array, int size){

int i, j;

int temp;

for (i = 1; i <= size - 1; i++){

temp = array[i];

for (j = i; j >= 0; j--){

if (array[j - 1] > temp){

array[j] = array[j - 1];

else

break;

array[j] = temp;
printf("\nSorted array is after phase %d is: \n", i);

for (j = 0; j < size; j++){

printf("%d\t", array[j]);

return 0;

// Selection sorting

int selection_sort(int *array, int size){

int i, j, k;

int min, temp;

for (i = 0; i < size - 1; i++){

min = i;

for (j = i + 1; j < size; j++){

if (array[min] > array[j]){

min = j;

}
if (min != i){

temp = array[i];

array[i] = array[min];

array[min] = temp;

printf("Sorted array after phase %d are:\n", i + 1);

for (k = 0; k < size; k++){

printf("%d\t", array[k]);

return 0;

// Second largest number

int second_largest(int *array, int size){

int temp;

int i, j;

for (i = 0; i < size; i++){

for (j = 0; j < size - (i + 1); j++){

if (array[j] > array[j + 1]){


temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

printf("Second largest element of array is: %d", array[size - 2]);

}
2. Write a C program to check whether a matrix is sparse or not.

#include <stdio.h>

int main(){

int i,j;

int row, col;

int count=0;

int array[10][10];

printf("Enter row of array: ");

scanf("%d", &row);

printf("Enter colum of array: ");

scanf("%d", &col);

printf("Enter element in array:\n");


for(i=0; i<row; i++){

for(j=0; j<col; j++){

scanf("%d", &array[i][j]);

printf("\nTotal number of element in array are: %d\n", row*col);

printf("\nEntered element in array are: \n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", array[i][j]);

printf("\n");

printf("\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(array[i][j] == 0){

count++;

}
}

if(count > (row*col)/2){

printf("It is a sparse matrix.");

else

printf("It is not a sparse matrix");

return 0;

Output:-
3. Write a C program to convert sparse matrix into 3 - row triplet
representation.

#include <stdio.h>

int main(){

int i,j;

int row, col, val;

int count=0;

int array[10][10];

printf("Enter row of array: ");

scanf("%d", &row);

printf("Enter colum of array: ");

scanf("%d", &col);

printf("Enter element in array:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

scanf("%d", &array[i][j]);

printf("\nTotal number of element in array are: %d\n", row*col);

printf("\nEntered element in array are: \n");


for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", array[i][j]);

printf("\n");

printf("\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(array[i][j] != 0){

count++;

if(count < (row*col)/2){

printf("It is a sparse matrix.\n");

int sm[3][count];

int ele=0;

printf("\nEnter 1 for row representation and 2 for coloumn representation:");

scanf("%d", &val);
if(val== 1){

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(array[i][j] != 0){

sm[0][ele] = i;

sm[1][ele] = j;

sm[2][ele] = array[i][j];

ele++;

printf("\n3-row triplet represention:\n");

for(i=0; i<3; i++){

for(j=0; j<count; j++){

printf("%d\t", sm[i][j]);

printf("\n");

}
else if(val == 2){

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(array[i][j] != 0){

sm[ele][0] = i;

sm[ele][1] = j;

sm[ele][2]= array[i][j];

ele++;

printf("\n3-coloumn triplet represention:\n");

for(i=0; i<count; i++){

for(j=0; j<3; j++){

printf("%d\t", sm[i][j]);

printf("\n");

}
}

else

printf("It is not a sparse matrix.");

return 0;

Output:-
4. Write A C Program to Perform Following Operation on Matrix.

//Addition Of Two Matrix.

//Subtraction Of Two Matrix.

//Multiplication Of Two Matrix.

//Transpose Of Two Matrix.

//Diagonal Sum of Two Matrix (row and col must be same.

//Check If Matrix Is Identity Or not.

Exit.

#include <stdio.h>

int sum(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row,


int col);

int sub(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row, int
col);

int multiply(int array1[10][10], int array2[10][10], int arrayresult[10][10], int


row, int col);

int transpose(int array1[10][10], int array2[10][10], int arrayresult[10][10], int


row, int col);

int diagsum(int array1[10][10], int array2[10][10], int row, int col);

int identity_not(int array1[10][10], int array2[10][10], int row, int col);

int main(){

int i, j;

int row, col;

int array1[10][10];
int array2[10][10];

int arrayresult[10][10];

int choice;

printf("Enter the size of row:");

scanf("%d", &row);

printf("Enter the size of col:");

scanf("%d", &col);

printf("Enter the element in array1:\n");

for(int i=0; i<row; i++){

for(int j=0; j<col; j++){

scanf("%d", &array1[i][j]);

printf("Enter the element in array2:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

scanf("%d", &array2[i][j]);

printf("Entered element in array1 are:\n");


for(int i=0; i<row; i++){

for(int j=0; j<col; j++){

printf("%d\t", array1[i][j]);

printf("\n");

printf("\n");

printf("Entered element in array2 are:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", array2[i][j]);

printf("\n");

do{

printf("\nMenuDriven\n");

printf("1. Addition \n");

printf("2. Subtraction \n");

printf("3. Multiplication \n");


printf("4. Transpose \n");

printf("5. Diagonal Sum \n");

printf("6. Identity or Not \n");

printf("7. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice){

case 1:

// Addition

printf("You selected Option 1.\n");

sum(array1, array2, arrayresult, row, col);

break;

case 2:

// Subtraction

printf("You selected Option 2.\n");

sub(array1, array2, arrayresult, row, col);

break;
case 3:

// Multiplication

printf("You selected Option 3.\n");

multiply(array1, array2, arrayresult, row, col);

break;

case 4:

// Transpose

printf("You selected Option 4.\n");

transpose(array1, array2, arrayresult, row, col);

break;

case 5:

// Diagonal Sum

printf("You selected Option 5.\n");

diagsum(array1, array2, row, col);

break;

case 6:

// Identity or Not

printf("You selected Option 6.\n");


identity_not(array1, array2, row, col);

break;

case 7:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice != 7);

return 0;

// sum

int sum(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row,


int col){

int i, j;

for(i=0; i<row; i++){


for(j=0; j<col; j++){

arrayresult[i][j] = array1[i][j] + array2[i][j];

printf("Sum of two array are:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

return 0;

//subtraction

int sub(int array1[10][10], int array2[10][10], int arrayresult[10][10], int row, int
col){

int i, j;

for(i=0; i<row; i++){

for(j=0; j<col; j++){


arrayresult[i][j] = array1[i][j] - array2[i][j];

printf("Subtraction of two array are:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

return 0;

//multiply

int multiply(int array1[10][10], int array2[10][10], int arrayresult[10][10], int


row, int col){

int i, j, k;

for(i=0; i<row; i++){

for(j=0; j<col; j++){

arrayresult[i][j] = 0;
}

for(i=0; i<row; i++){

for(j=0; j<col; j++){

for(k=0; k<col; k++){

arrayresult[i][j] += (array1[i][k]*array2[k][j]);

printf("Multiplication of both array are:\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

return 0;

//Transpose
int transpose(int array1[10][10], int array2[10][10], int arrayresult[10][10], int
row, int col){

int i, j;

for(i=0; i<row; i++){

for(j=0; j<col; j++){

arrayresult[j][i] = 0;

for(i=0; i<row; i++){

for(j=0; j<col; j++){

arrayresult[j][i] = array1[i][j];

printf("Tranpose of matrix1 is:\n");

if(row >= col){

for(i=0; i<col; i++){

for(j=0; j<row; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

}
}

else if(row < col){

for(i=0; i<col; i++){

for(j=0; j<row; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

for(i=0; i<row; i++){

for(j=0; j<col; j++){

arrayresult[j][i] = array2[i][j];

printf("Tranpose of matrix2 is:\n");

if(row >= col){

for(i=0; i<col; i++){

for(j=0; j<row; j++){

printf("%d\t", arrayresult[i][j]);

}
printf("\n");

else if(row < col){

for(i=0; i<col; i++){

for(j=0; j<row; j++){

printf("%d\t", arrayresult[i][j]);

printf("\n");

return 0;

//Diagonal sum

int diagsum(int array1[10][10], int array2[10][10], int row, int col){

int i, j;

int sum = 0;

int sum1 = 0;

for(i=0; i<row; i++){


for(j=0; j<col; j++){

if(i == j){

sum += array1[i][j];

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(i == j){

sum1 += array2[i][j];

printf("Sum of diagonal of matrix1 is: %d\n", sum);

printf("Sum of diagonal of matrix1 is: %d", sum1);

return 0;

//Identity or Not

int identity_not(int array1[10][10], int array2[10][10], int row, int col){

int count=0;
int count2 = 0;

int i, j;

for(i=0; i<row; i++){

for(j=0; j<col; j++){

if(i!=j && array1[i][j] == 0){

count++;

else if(i==j && array1[i][j] == 1){

count2++;

if(count == ((row*col)-row) && count2 == row){

printf("Matrix1 is a identity matrix.\n");

else{

printf("EMatrix1 is not a identity matrix.\n");

count=0;

count2 = 0;

for(i=0; i<row; i++){


for(j=0; j<col; j++){

if(i!=j && array2[i][j] == 0){

count++;

else if(i==j && array2[i][j] == 1){

count2++;

if(count == ((row*col)-row) && count2 == row){

printf("Matrix2 is a identity matrix.");

else{

printf("Matrix2 is not a identity matrix.");

return 0;

Outputs:-

Addition:-
Subtraction:-
Multiplication:-

Transpose:-
Diagonal Sum:-
Identity or Not:-

Exit:-
5. Write A C Program to Perform Count Sort on An Unsorted Array.

#include <stdio.h>

int main(){

int i;

int size, max;

int arr[50];

int array[50];

int count[10];

printf("Enter the size of array:");

scanf("%d", &size);

printf("Enter the element in array:\n");

for(i=0; i<size; i++){

scanf("%d", &array[i]);

printf("Entered element in array are:\n");

for(i=0; i<size; i++){

printf("%d\n", array[i]);

max= array[0];

for(i=1; i<size; i++){

if(max < array[i]){


max = array[i];

for(i=0; i<max+1; i++){

count[i] = 0;

for(i=0; i<size; i++){

count[array[i]]++;

for(i=1; i<max+1; i++){

count[i]= count[i]+count[i-1];

for(i=size-1; i>=0; i--){

arr[count[array[i]]-1] = array[i];

count[array[i]]--;

for(i=0; i<size; i++){

array[i] = arr[i];

printf("Sorted array by count algorithm is:\n");

for(i=0; i<size; i++){


printf("%d\n", array[i]);

return 0;}

Output:-
6. Write a C program to perform Radix sort on an unsorted array.

#include<stdio.h>

#include<stdlib.h>

int getMax(int A[], int n) {

int max = A[0];

for (int i = 1; i < n; i++) {

if (A[i] > max)

max = A[i];

return max;

void CountSort(int a[], int n, int pos) {

int Count[10] = {0};

int b[n];

for (int i = 0; i < n; i++) {

Count[(a[i] / pos) % 10]++;

for (int i = 1; i < 10; i++) {

Count[i] += Count[i - 1];


}

for (int i = n - 1; i >= 0; i--) {

b[Count[(a[i] / pos) % 10] - 1] = a[i];

Count[(a[i] / pos) % 10]--;

for (int i = 0; i < n; i++) {

a[i] = b[i];

void redixSort(int a[], int n) {

int max = getMax(a, n);

for (int pos = 1; max / pos > 0; pos *= 10) {

CountSort(a, n, pos);

int main() {

int a[] = {432, 8, 530, 90, 88, 231, 11, 45, 677, 199};
int n = sizeof(a) / sizeof(a[0]);

redixSort(a, n);

printf("Sorted array: \n");

for (int i = 0; i < n; i++)

printf("%d ", a[i]);

return 0;

}
7. Write a C program to dynamically allocate an array and calculate sum of its
elements.

Using malloc:-

#include <stdlib.h>

#include <stdio.h>

int main(){

int *p;

int n, i;

int sum = 0;

printf("Enter the size of array: ");

scanf("%d", &n);

p= (int*) malloc (n*sizeof(int));

printf("Enter element in array: \n");

for(i=0; i<n; i++){

scanf("%d", p+i);

printf("Entered element in array are:\n");

for(i=0; i<n; i++){

printf("%d\n", *(p+i));

for(i=0; i<n; i++){


sum +=*(p+i);

printf("Sum of all element of array is: %d", sum);

return 0;

Output:-

Using calloc:-

#include <stdlib.h>

#include <stdio.h>

int main(){

int *p;
int n, i;

int sum = 0;

int size;

printf("Enter the size of array: ");

scanf("%d", &n);

p= (int*) calloc (n,size);

printf("Enter element in array: \n");

for(i=0; i<n; i++){

scanf("%d", p+i);

printf("Entered element in array are:\n");

for(i=0; i<n; i++){

printf("%d\n", *(p+i));

for(i=0; i<n; i++){

sum +=*(p+i);

printf("Sum of all element of array is: %d", sum);

return 0;

Output:-
8. Write A C Program to Remove Duplicate Elements from An Array.

#include <stdio.h>

int main(){

int i, j;

int size, temp;

int arr[20];

printf("Enter the size of array: ");

scanf("%d", &size);

printf("Enter elements in array: \n");

for(i=0; i<size; i++){

scanf("%d", &arr[i]);

printf("Entered elements in array are: \n");

for(i=0; i<size; i++){

printf("%d\n", arr[i]);

for(i=0; i<size-1; i++){

for(j=i+1; j<size; j++){

if(arr[i]>arr[j]){

temp=arr[i];

arr[i]=arr[j];
arr[j]=temp;

printf("Sorted array is: \n");

for(i=0; i<size; i++){

printf("%d\n", arr[i]);

for(i=1; i<size; i++){

if(arr[i] == arr[i+1]){

for(j=i; j<size-1; j++){

arr[j]=arr[j+1];

size--;

printf("Updated array is: \n");

for(i=0; i<size; i++){

printf("%d\n", arr[i]);

return 0;
}

Output:-

9. Write a program to following operations on singly linked list

Addition of a new node when list is empty.

Addition of a node at the end of the list.

Addition of a node at the beginning of linked list.

Addition of a node at the specific position.

Traversing a linked list.


Counting number of nodes in linked list.

Deleting a node from the linked list by specifying the value of the deleting
node.

Reverse A linked List.


Sorting.
Search
Insert a node in a sorted linked list such that linked list remains sorted.

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node*next;

};

// void add_new_node(struct node ** start);

void add_node_end(struct node* start);

void add_node_begin(struct node* start);

void add_node_speposc(struct node* start);

void traverse(struct node* start);

int count_node(struct node* start);

void delete_node_beg(struct node* start);


void delete_node_spec(struct node* start);

void delete_node_end(struct node* start);

void reverse(struct node* start);

void sorting(struct node* start);

void search(struct node* start);

void insert_sort(struct node* start);

int main(){

struct node *newnode;

struct node *start = NULL;

struct node *temp;

int choice = 1;

int choice2, choice3, choice4;

while(choice){

newnode = (struct node*) malloc(sizeof(struct node));

printf("Enter the data in list: ");

scanf("%d", &newnode->data);

newnode->next = NULL;
if(start == NULL){

start = newnode;

temp = newnode;

else{

temp->next = newnode;

temp = newnode;

printf("created successsfully\n");

printf("Do you want to add more node if yes type any number if no type 0:
");

scanf("%d", &choice);

printf("\n");

temp = start;

while(temp != NULL){

printf("Entered data at address %d in list is: %d\n",temp,temp->data);

temp = temp -> next;

do{

printf("\nMenuDriven\n");
printf("1. Addition \n");

printf("2. Traverse \n");

printf("3. Node count \n");

printf("4. Deletion \n");

printf("5. Reverse \n");

printf("6. Sorting \n");

printf("7. Search \n");

printf("8. Insert data in sorted way\n");

printf("9. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice2);

switch (choice2){

case 1:

// Additin of new node when list is empty

printf("You selected Option 1.\n");

printf("\n");

do{

printf("1. Addition at end \n");

printf("2. Addition at beginning \n");


printf("3. Addition at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice4);

switch (choice4){

case 1:

// Addition of node at end

printf("You selected Option 1.\n");

add_node_end(start);

break;

case 2:

// Addition of node at begin

printf("You selected Option 2.\n");

add_node_begin(start);

break;

case 3:

// Addition of node at specific position

printf("You selected Option 3.\n");

add_node_speposc(start);
break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice4 != 4);

break;

case 2:

// traverse of node

printf("You selected Option 2.\n");

traverse(start);

break;

case 3:

// counting of node
printf("You selected Option 3.\n");

count_node(start);

break;

case 4:

// deletion of node

printf("You selected Option 4.\n");

printf("\n");

do{

printf("1. Deletion at beginning \n");

printf("2. Deletion at end \n");

printf("3. Deletion at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice3);

switch (choice3){

case 1:

// Deletion node at begin

printf("You selected Option 1.\n");


delete_node_beg(start);

break;

case 2:

// Deletion node at begin

printf("You selected Option 2.\n");

delete_node_end(start);

break;

case 3:

// Deletion node at begin

printf("You selected Option 3.\n");

delete_node_spec(start);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");


}

while (choice3 != 4);

break;

case 5:

// reverse of node

printf("You selected Option 5.\n");

reverse(start);

break;

case 6:

// sorting of node

printf("You selected Option 6.\n");

sorting(start);

break;

case 7:

// searching of node

printf("You selected Option 7.\n");

search(start);
break;

case 8:

// insert data in sorted way

printf("You selected Option 8.\n");

insert_sort(start);

break;

case 9:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice2 != 9);

//add node at end

void add_node_end(struct node* start){


struct node *newnode;

struct node* temp;

newnode = (struct node*) malloc(sizeof(struct node));

printf("Enter the data in the list: ");

scanf("%d", &newnode->data);

newnode->next = NULL;

temp = start;

while( temp ->next != NULL){

temp = temp ->next;

temp -> next = newnode;

printf("Added successsfully\n");

temp = start;

while(temp != NULL){

printf("Entered data at address %d is : %d\n", temp, temp->data);

temp = temp->next;

}
//add new node at begin

void add_node_begin(struct node* start){

struct node *newnode;

struct node *temp;

newnode = (struct node*) malloc (sizeof(struct node));

temp = start;

printf("Enter the data in the list: ");

scanf("%d", &newnode->data);

newnode->next = NULL;

start = newnode;

newnode ->next = temp;

printf("Added successsfully\n");

temp = start;

while(temp != NULL){

printf("Entered data at address %d is : %d\n", temp, temp->data);

temp = temp->next;
}

//add new node at specific position

void add_node_speposc(struct node* start){

int choice = 1;

struct node *temp;

struct node *newnode;

int pos, i;

printf("Enter position where you want to enter data in list: ");

scanf("%d", &pos);

temp = start;

i = 1;

while(i<pos-1){

temp = temp-> next;

i++;

newnode = (struct node*) malloc (sizeof(struct node));

printf("Enter the data you want to enter: ");


scanf("%d", &newnode-> data);

newnode->next=NULL;

newnode->next = temp-> next;

temp->next = newnode;

printf("Added successsfully.\n");

temp = start;

while(temp != NULL){

printf("Entered data at address %d is : %d\n", temp, temp->data);

temp = temp->next;

//traverse of nodes

void traverse(struct node* start){

struct node *temp;

struct node *newnode;

temp = start;
while(temp !=NULL){

printf("Entered node present at %d address is: %d\n", temp, temp->data);

temp = temp->next;

//counting of node

int count_node(struct node* start){

int count = 0;

struct node *temp;

struct node *newnode;

temp = start;

while(temp !=NULL){

printf("Entered node present at %d address is: %d\n", temp, temp->data);

temp = temp->next;

count++;

printf("Number of nodes in linked list are: %d", count);

//deletion of nodes
void delete_node_beg(struct node* start){

struct node *temp;

struct node *newnode;

temp = start;

start= temp->next;

free(temp);

temp = start;

while(temp !=NULL){

printf("Entered node present at %d address is: %d\n", temp, temp->data);

temp = temp->next;

//delete node at specific position

void delete_node_spec(struct node* start){

struct node *newnode;

struct node *temp;

struct node *temp1;

int i = 1;
int pos;

printf("Enter the position you want to delete:");

scanf("%d", &pos);

temp = start;

while(i < pos-1){

temp = temp -> next;

i++;

temp1 = temp-> next;

temp -> next = temp1 -> next;

free(temp1);

temp = start;

while(temp != NULL){

printf("Data in node present at address %d is %d \n", temp, temp->data);

temp = temp -> next;

}
// delete node at end

void delete_node_end(struct node* start){

struct node *temp;

struct node *prev;

int choice;

temp = start;

while(temp->next != NULL){

prev = temp;

temp = temp -> next; }

free(temp);

prev ->next = NULL;

printf("After deletion end node. \n");

temp = start;

while(temp != NULL){

printf("Data in node present at address %d is %d \n", temp, temp->data);

temp = temp -> next;

}
//reverse of linked list

void reverse(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int count = 0;

int temp2;

int i, j;

temp = start;

while (temp != NULL){

temp = temp->next;

count++;

temp = start;

for(i=0; i<count/2; i++){

tail=start;

for(j = 1; j<count-i; j++){

tail = tail -> next;

temp2 = temp -> data;


temp -> data = tail -> data;

tail -> data = temp2;

temp = temp -> next;

printf("\n");

printf("After Reverse of linked list.\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

// sorting of linked list

void sorting(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int count = 0;

int temp2, i, j;
temp = start;

while(temp !=NULL){

temp = temp->next;

count++;

for (j = 0; j < count; j++){

temp = start;

tail = temp->next;

for (i = 1; i < count - j; i++){

if (temp->data > tail->data){

temp2 = temp->data;

temp->data = tail->data;

tail->data = temp2;

temp = tail;

tail = tail->next;

temp = start;
printf("\n");

while (temp != NULL){

printf("Data in node After Sorting present at address %d is %d: \n", temp,


temp->data);

temp = temp->next;

// search of node

void search(struct node* start){

struct node *newnode;

struct node *temp;

int flag = 0;

int ser;

int count = 0;

printf("Enter the data you want to search:");

scanf("%d", &ser);

printf("\n");
temp = start;

while (temp != NULL){

count++;

if(temp->data == ser){

printf("Data in node present at position %d and data is %d\n", count,


temp->data);

flag++;

temp = temp->next;

if(flag){

printf("Data found successfully.");

else{

printf("Data not found.");

// insertion of data in sorted way

void insert_sort(struct node* start){

struct node *newnode;


struct node *temp;

struct node *tail;

int choice2 = 1;

int count = 0;

int temp2; ins, i, j;

temp = start;

while(temp !=NULL){

temp = temp->next;

count++;

for (j = 0; j < count; j++){

temp = start;

tail = temp->next;

for (i = 1; i < count - j; i++){

if (temp->data > tail->data){

temp2 = temp->data;

temp->data = tail->data;

tail->data = temp2;

temp = tail;
tail = tail->next;

temp = start;

printf("\n");

while (temp != NULL){

printf("Data in node After Sorting present at address %d is %d: \n", temp,


temp->data);

temp = temp->next; }

printf("\n");

newnode = (struct node *)malloc(sizeof(struct node));

newnode->next = NULL;

printf("Enter the data you want to insert:");

scanf("%d", &ins);

newnode->data = ins;

temp = start;

tail = temp->next;
if (ins < temp->data){

newnode->next = temp;

start = newnode;

else{

while(tail != NULL && ins > tail -> data){

temp = tail;

tail = tail->next;

newnode->next = tail;

temp->next = newnode;

printf("\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

}
Output:-

Addition at end:-

Addition at beginning:-
Addition at specific position:-
Traversing:-
Counting of Nodes:-

Deletion of Nodes at beginning:-


Deletion of Nodes at end:-
Deletion of Nodes at Specific position:-
Reversing:-

Sorting:-
Searching:-

Inserting Data in sorted order:-


10.Write a program to following operations on Doubly linked list

Addition of a new node when list is empty.


Addition of a node at the end of the list.
Addition of a node at the beginning of linked list.
Addition of a node at the specific position.
Traversing a linked list.
Counting number of nodes in linked list.
Deleting a node from the linked list by specifying the value of the
deleting node.
Reverse A linked List.
Sorting.
Search
Insert a node in a sorted linked list such that linked list remains sorted.

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node*next;

struct node*prev;

};

void add_node_end(struct node* start);

void add_node_begin(struct node* start);

void add_node_speposc(struct node* start);


void traverse(struct node* start);

int count_node(struct node* start);

void delete_node_beg(struct node* start);

void delete_node_spec(struct node* start);

void delete_node_end(struct node* start);

void reverse(struct node* start);

void sorting(struct node* start);

void search(struct node* start);

int main(){

struct node *newnode;

struct node *start = NULL;

struct node *temp;

int choice = 1;

int choice2, choice3, choice4;

while(choice){

newnode = (struct node*) malloc(sizeof(struct node));

printf("Enter the data in list: ");

scanf("%d", &newnode->data);
newnode->next = NULL;

if(start == NULL){

start = newnode;

temp = newnode;

else{

temp->next = newnode;

temp = newnode;

printf("created successsfully\n");

printf("Do you want to add more node if yes type any number if no type 0:
");

scanf("%d", &choice);

printf("\n");

temp = start;

while(temp != NULL){

printf("Entered data at address %d in list is: %d\n",temp,temp->data);

temp = temp -> next; }

do{
printf("\nMenuDriven\n");

printf("1. Addition \n");

printf("2. Traverse \n");

printf("3. Node count \n");

printf("4. Deletion \n");

printf("5. Reverse \n");

printf("6. Sorting \n");

printf("7. Search \n");

printf("8. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice2);

switch (choice2){

case 1:

// Additin of new node when list is empty

printf("You selected Option 1.\n");

printf("\n");

do{

printf("1. Addition at end \n");

printf("2. Addition at beginning \n");


printf("3. Addition at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice4);

switch (choice4){

case 1:

// Addition of node at end

printf("You selected Option 1.\n");

add_node_end(start);

break;

case 2:

// Addition of node at begin

printf("You selected Option 2.\n");

add_node_begin(start);

break;

case 3:

// Addition of node at specific position

printf("You selected Option 3.\n");


add_node_speposc(start);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice4 != 4);

break;

case 2:

// traverse of node

printf("You selected Option 2.\n");

traverse(start);

break;

case 3:

// counting of node
printf("You selected Option 3.\n");

count_node(start);

break;

case 4:

// deletion of node

printf("You selected Option 4.\n");

printf("\n");

do{

printf("1. Deletion at beginning \n");

printf("2. Deletion at end \n");

printf("3. Deletion at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice3);

switch (choice3){

case 1:

// Deletion node at begin

printf("You selected Option 1.\n");


delete_node_beg(start);

break;

case 2:

// Deletion node at begin

printf("You selected Option 2.\n");

delete_node_end(start);

break;

case 3:

// Deletion node at begin

printf("You selected Option 3.\n");

delete_node_spec(start);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

}
}

while (choice3 != 4);

break;

case 5:

// reverse of node

printf("You selected Option 5.\n");

reverse(start);

break;

case 6:

// sorting of node

printf("You selected Option 6.\n");

sorting(start);

break;

case 7:

// searching of node

printf("You selected Option 7.\n");

search(start);

break;
case 8:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice2 != 8);

//add node at end

void add_node_end(struct node* start){

struct node *newnode;

struct node *temp;

temp = start;

while (temp -> next != NULL){

temp = temp->next;

}
newnode = (struct node *)malloc(sizeof(struct node));

newnode->next = NULL;

newnode->prev = NULL;

printf("Enter the data you want to insert:");

scanf("%d", &newnode->data);

newnode -> prev = temp -> next;

temp -> next = newnode;

printf("\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

//add new node at begin

void add_node_begin(struct node* start){

struct node *newnode;

struct node *temp;


newnode = (struct node *)malloc(sizeof(struct node));

newnode->next = NULL;

newnode->prev = NULL;

printf("Enter the data you want to insert in beginning:");

scanf("%d", &newnode->data);

temp =start;

start = newnode;

newnode -> next = temp;

temp -> prev = newnode;

printf("\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp-


>data);

temp = temp->next;

}
//add new node at specific position

void add_node_speposc(struct node* start){

struct node *newnode;

struct node *temp;

int pos;

printf("Enter the position where you want to insert:");

scanf("%d", &pos);

temp = start;

int i =1;

while (i < pos-1){

temp = temp->next;

i++;

newnode = (struct node *)malloc(sizeof(struct node));

newnode->next = NULL;

newnode->prev = NULL;

printf("Enter the data you want to insert:");

scanf("%d", &newnode->data);

newnode -> next = temp -> next;


newnode -> prev = temp;

temp -> next = newnode;

printf("\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp-


>data);

temp = temp->next;

//traverse of nodes

void traverse(struct node* start){

struct node *newnode;

struct node *temp;

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

}
}

//counting of node

int count_node(struct node* start){

struct node *newnode;

struct node *temp;

int count = 0;

temp = start;

while (temp != NULL){

temp = temp->next;

count++;

printf("\n");

printf("Number of nodes in linked list: %d", count); }

//deletion of nodes

void delete_node_beg(struct node* start){

struct node *newnode;

struct node *temp;

temp = start;
start = temp ->next;

temp ->next->prev = NULL;

free(temp);

printf("\n");

printf("After Deletion of beginner node in linked list.\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

//delete node at specific position

void delete_node_spec(struct node* start){

struct node *newnode;

struct node *temp;

struct node *temp2;

int pos;

int i = 0;
printf("Enter the position which you want to delete:");

scanf("%d", &pos);

temp = start;

while(i < pos-1){

temp2 = temp;

temp = temp->next;

i++;

temp -> next -> prev = temp2;

temp2 ->next = temp -> next;

free(temp);

printf("\n");

printf("After Deletion specific node in linked list.\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

// delete node at end


void delete_node_end(struct node* start){

struct node *newnode;

struct node *temp;

struct node *temp2;

temp = start;

while(temp -> next != NULL){

temp2 = temp;

temp = temp->next;

temp2 ->next = NULL;

free(temp);

printf("\n");

printf("After Deletion of End node in linked list.\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

}
//reverse of Linked list(it does not work)

void reverse(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int count = 0;

int temp2;

temp = start;

while (temp != NULL){

tail = temp;

temp = temp->next;

count++;

temp = start;

for(int i=0; i < count/2; i++){

temp2 = temp ->data;

temp -> data = tail -> data;

tail -> data = temp2;

temp= temp -> next;

tail = tail -> prev; }


printf("\n");

printf("After Reverse of linked list.\n");

temp = start;

while (temp != NULL){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

// sorting of linked list

void sorting(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int count =0;

int i, j, temp2;

temp = start;

while (temp != NULL){

temp = temp->next;

count++;
}

for(i=0; i<count; i++){

temp = start;

tail = temp -> next;

for(j=1; j<count-i; j++){

if(temp-> data > tail-> data){

temp2 = temp ->data;

temp -> data = tail -> data;

tail -> data = temp2; }

temp = temp -> next;

tail = temp -> next;

printf("\n");

temp = start;

while (temp != NULL){

printf("Data in node After Sorting present at address %d is %d: \n", temp,


temp->data);

temp = temp->next;

}
// search of node

void search(struct node* start){

struct node *newnode;

struct node *temp;

int ser;

int flag =1;

int count =0;

printf("Enter the data you want to search:");

scanf("%d", &ser);

temp = start;

while(temp != NULL){

count++;

if(temp -> data == ser){

printf("Entered data found at position %d and data is: %d\n", count,


temp-> data);

printf("Data Found successfully.");

flag = 0;

temp = temp -> next;


}

if(flag ==1){

printf("Data Not Found.");

Outputs:-

Addition at end:-

Addition at beginning:-
Addition at specific position:-

Traversing:-
1Counting of nodes:-

Deletion of nodes at beginnig:-


Deletion of nodes at end:-
Deletion of nodes at specific position:-
Sorting:-

Searching:-
11.Write a program to following operations on Circular singly linked list

Addition of a new node when list is empty.


Addition of a node at the end of the list.
Addition of a node at the beginning of linked list.
Addition of a node at the specific position.
Traversing a linked list.
Counting number of nodes in linked list.
Deleting a node from the linked list by specifying the value of the
deleting node.
Reverse A linked List.
Sorting.
Search.
Insert a node in a sorted linked list such that linked list remains sorted.

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node*next;

};

void add_node_end(struct node* start);

void add_node_begin(struct node* start);

void add_node_speposc(struct node* start);


void traverse(struct node* start);

int count_node(struct node* start);

void delete_node_beg(struct node* start);

void delete_node_spec(struct node* start);

void delete_node_end(struct node* start);

void reverse(struct node* start);

void sorting(struct node* start);

void search(struct node* start);

int main(){

struct node *newnode;

struct node *start = NULL;

struct node *temp;

int choice = 1;

int choice2, choice3, choice4;

while(choice){

newnode = (struct node*) malloc(sizeof(struct node));

printf("Enter the data in list: ");

scanf("%d", &newnode->data);
newnode->next = NULL;

if(start == NULL){

start = newnode;

temp = newnode;

else{

temp->next = newnode;

newnode -> next = start;

temp = newnode;

printf("created successsfully\n");

printf("Do you want to add more node if yes type any number if no type 0:
");

scanf("%d", &choice);

printf("\n");

temp = start;

while(temp -> next != start){

printf("Entered data at address %d in list is: %d\n",temp,temp->data);

temp = temp -> next;


}

printf("Entered data at address %d in list is: %d\n",temp,temp->data);

do{

printf("\nMenuDriven\n");

printf("1. Addition \n");

printf("2. Traverse \n");

printf("3. Node count \n");

printf("4. Deletion \n");

printf("5. Reverse \n");

printf("6. Sorting \n");

printf("7. Search \n");

printf("8. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice2);

switch (choice2){

case 1:

// Additin of new node when list is empty

printf("You selected Option 1.\n");

printf("\n");
do{

printf("1. Addition at end \n");

printf("2. Addition at beginning \n");

printf("3. Addition at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice4);

switch (choice4){

case 1:

// Addition of node at end

printf("You selected Option 1.\n");

add_node_end(start);

break;

case 2:

// Addition of node at begin

printf("You selected Option 2.\n");

add_node_begin(start);

break;
case 3:

// Addition of node at specific position

printf("You selected Option 3.\n");

add_node_speposc(start);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice4 != 4);

break;

case 2:

// traverse of node

printf("You selected Option 2.\n");


traverse(start);

break;

case 3:

// counting of node

printf("You selected Option 3.\n");

count_node(start);

break;

case 4:

// deletion of node

printf("You selected Option 4.\n");

printf("\n");

do{

printf("1. Deletion at beginning \n");

printf("2. Deletion at end \n");

printf("3. Deletion at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice3);
switch (choice3){

case 1:

// Deletion node at begin

printf("You selected Option 1.\n");

delete_node_beg(start);

break;

case 2:

// Deletion node at begin

printf("You selected Option 2.\n");

delete_node_end(start);

break;

case 3:

// Deletion node at begin

printf("You selected Option 3.\n");

delete_node_spec(start);

break;

case 4:
printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice3 != 4);

break;

case 5:

// reverse of node

printf("You selected Option 5.\n");

reverse(start);

break;

case 6:

// sorting of node

printf("You selected Option 6.\n");

sorting(start);

break;
case 7:

// searching of node

printf("You selected Option 7.\n");

search(start);

break;

case 8:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice2 != 8);

//add node at end

void add_node_end(struct node* start){

struct node *newnode;


struct node *temp;

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter the data you want to insert at the end of list:");

scanf("%d", &newnode->data);

newnode->next = NULL;

temp = start;

while (temp -> next != start){

temp = temp->next;

temp -> next = newnode;

newnode -> next = start;

printf("\n");

temp = start;

while (temp ->next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);


printf("Data inserted at the end successfully.\n");

//add new node at begin

void add_node_begin(struct node* start){

struct node *newnode;

struct node *temp;

newnode = (struct node *)malloc(sizeof(struct node));

temp = start;

while (temp ->next != start){

temp = temp->next;

printf("Enter the data you want to insert at the beginning of list:");

scanf("%d", & newnode->data);

newnode -> next = start;

start = newnode;

temp -> next = newnode;

printf("\n");
temp = start;

while (temp -> next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data inserted at the beginning successfully.\n");

//add new node at specific position

void add_node_speposc(struct node* start){

struct node *newnode;

struct node *temp;

int pos;

int i =1;

printf("Enter the position where you want to insert data:");

scanf("%d", &pos);

temp = start;

while(i < pos-1){


temp =temp -> next;

i++;

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter the data to you want to insert:");

scanf("%d", &newnode -> data);

newnode -> next = temp ->next;

temp -> next = newnode;

printf("\n");

temp = start;

while (temp -> next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data inserted at position %d successfully.\n", pos);

//traverse of nodes

void traverse(struct node* start){


struct node *newnode;

struct node *temp;

temp = start;

while (temp-> next!= start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

//counting of node

int count_node(struct node* start){

struct node *newnode;

struct node *temp;

int count =0;

temp = start;

while (temp-> next!= start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

count++;
}

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("\n");

printf("Number of nodes in linked list are: %d\n", count+1);

//deletion of nodes

void delete_node_beg(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

temp = start;

while (temp-> next!= start){

temp = temp->next;

tail = temp;

temp = start;

start = temp -> next;

tail -> next = start;

free(temp);
temp = start;

while (temp-> next!= start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next; }

printf("Data in node present at address %d is %d: \n", temp, temp->data);

//delete node at specific position

void delete_node_spec(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int pos;

int i =0;

printf("Enter the position at which data you want to delete:");

scanf("%d", &pos);

temp = start;

while(i < pos-1){


tail = temp;

temp =temp -> next;

i++;

tail ->next = temp ->next;

free(temp);

printf("\n");

temp = start;

while (temp -> next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data Deleted at position %d successfully.\n", pos); }

// delete node at end

void delete_node_end(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;


tail = start;

temp = tail -> next;

while (temp-> next!= start){

tail = tail->next;

temp = temp->next;

tail -> next = start;

free(temp);

temp = start;

while (temp-> next!= start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

//reverse of Linked list

void reverse(struct node* start){

// struct node *newnode;

// struct node *temp;


// struct node *tail;

// int count = 0;

// int temp2;

// temp = start;

// while (temp != NULL){

// tail = temp;

// temp = temp->next;

// count++;

// }

// temp = start;

// for(int i=0; i < count/2; i++){

// temp2 = temp ->data;

// temp -> data = tail -> data;

// tail -> data = temp2;

// temp= temp -> next;

// tail = tail -> prev;

// }

// printf("\n");

// printf("After Reverse of linked list.\n");


// temp = start;

// while (temp != NULL){

// printf("Data in node present at address %d is %d: \n", temp, temp-


>data);

// temp = temp->next;

// }

// sorting of linked list

void sorting(struct node* start){

struct node *newnode;

struct node *temp;

struct node *tail;

int temp2, I, j;

int count = 0;

temp = start;

while (temp-> next!= start){

count++;

temp = temp->next;
}

printf("\n");

for(i=0; i<=count; i++){

temp = start;

tail = temp-> next;

for(j=0; j<=count-1; j++){

if(temp -> data > tail -> data){

temp2 = temp -> data;

temp -> data = tail -> data;

tail -> data = temp2;

temp = tail;

tail = tail-> next;

temp = start;

while (temp-> next!= start){

printf("Data in node after sorting present at address %d is %d: \n", temp,


temp->data);

temp = temp->next;
}

printf("Data in node after sorting present at address %d is %d: \n", temp,


temp->data);

printf("Data sorted successfully.\n");

// search of node

void search(struct node* start){

struct node *newnode;

struct node *temp;

int count = 0;

int ser;

int flag =1;

printf("Enter the data you want to search: ");

scanf("%d", &ser);

printf("\n");

temp = start;

while(temp -> next != start){

count++;

if(temp -> data == ser){


printf("Data is %d present at position : %d\n", ser, count);

printf("Data found successfully.");

flag--;

break; }

temp = temp -> next;

while(flag){

if(temp ->data == ser){

printf("Data is %d present at position : %d\n", ser, count+1);

printf("Data found successfully.");

else{

printf("Data not found.");

} }

Output:-

Addition at end:-
Addition at beginning:-

Addition at Specific Position:-


Traversing:-
Counting of Node:-

Deletion at Beginning:-
Deletion at Ending:-

Deletion at Specific Position:-


Sorting:-
Searching:-
12.Write a program to following operations on Circular Doubly linked list

1. Addition of a new node when list is empty.


2. Addition of a node at the end of the list.
3. Addition of a node at the beginning of linked list.
4. Addition of a node at the specific position.
5. Traversing a linked list.
6. Counting number of nodes in linked list.
7. Deleting a node from the linked list by specifying the value of the
deleting node.
8. Reverse A linked List.
9. Sorting.
10. Search.

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node *next;

struct node *prev;

};

void add_node_end();

void add_node_begin();

void add_node_speposc();
void traverse();

int count_node();

void delete_node_beg();

void delete_node_spec();

void delete_node_end();

void reverse();

void sorting();

void search();

struct node *newnode;

struct node *start = NULL;

struct node *temp;

struct node *tail;

int choice = 1;

int main(){

int choice2, choice3, choice4;

while (choice){

newnode = (struct node *)malloc(sizeof(struct node));


printf("Enter the data in list: ");

scanf("%d", &newnode->data);

newnode->next = NULL;

newnode->prev = NULL;

if (start == NULL){

start = newnode;

tail = newnode;

newnode->next = start;

newnode->prev = temp;

else{

tail->next = newnode;

newnode->next = start;

newnode->prev = tail;

tail = newnode;

start->prev = tail;

printf("created successsfully\n");

printf("Do you want to add more node if yes type any number if no type 0:
");
scanf("%d", &choice);

printf("\n");

temp = start;

while (temp->next != tail->next){

printf("Entered data at address %d in list is: %d\n", temp, temp->data);

temp = temp->next;

printf("Entered data at address %d in list is: %d\n", temp, temp->data);

do{

printf("\nMenuDriven\n");

printf("1. Addition \n");

printf("2. Traverse \n");

printf("3. Node count \n");

printf("4. Deletion \n");

printf("5. Reverse \n");

printf("6. Sorting \n");

printf("7. Search \n");

printf("8. Exit \n");

printf("Enter your choice: ");


scanf("%d", &choice2);

switch (choice2){

case 1:

// Additin of new node when list is empty

printf("You selected Option 1.\n");

printf("\n");

do{

printf("1. Addition at end \n");

printf("2. Addition at beginning \n");

printf("3. Addition at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice4);

switch (choice4){

case 1:

// Addition of node at end

printf("You selected Option 1.\n");

add_node_end();
break;

case 2:

// Addition of node at begin

printf("You selected Option 2.\n");

add_node_begin();

break;

case 3:

// Addition of node at specific position

printf("You selected Option 3.\n");

add_node_speposc(start, tail);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

}
}

while (choice4 != 4);

break;

case 2:

// traverse of node

printf("You selected Option 2.\n");

traverse(start, tail);

break;

case 3:

// counting of node

printf("You selected Option 3.\n");

count_node(start, tail);

break;

case 4:

// deletion of node

printf("You selected Option 4.\n");

printf("\n");
do{

printf("1. Deletion at beginning \n");

printf("2. Deletion at end \n");

printf("3. Deletion at specific position \n");

printf("4. Exit \n");

printf("Enter your choice: ");

scanf("%d", &choice3);

switch (choice3){

case 1:

// Deletion node at begin

printf("You selected Option 1.\n");

delete_node_beg(start, tail);

break;

case 2:

// Deletion node at begin

printf("You selected Option 2.\n");

delete_node_end(start, tail);

break;
case 3:

// Deletion node at begin

printf("You selected Option 3.\n");

delete_node_spec(start, tail);

break;

case 4:

printf("Exiting the program. Back to main menu! \n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

while (choice3 != 4);

break;

case 5:

// reverse of node

printf("You selected Option 5.\n");

reverse(start, tail);
break;

case 6:

// sorting of node

printf("You selected Option 6.\n");

sorting(start, tail);

break;

case 7:

// searching of node

printf("You selected Option 7.\n");

search(start, tail);

break;

case 8:

printf("Exiting the program. Goodbye!\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

}
}

while (choice2 != 8);

// add node at end

void add_node_end(){

struct node *newnode;

struct node *temp;

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter the data you want to insert at the end of list:");

scanf("%d", &newnode->data);

tail->next = newnode;

newnode->next = start;

newnode->prev = tail;

start->prev = newnode;

tail = newnode;

printf("\n");

temp = start;
while (temp->next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data inserted at the end successfully.\n");

// add new node at begin

void add_node_begin(){

struct node *newnode;

struct node *temp;

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter the data you want to insert at the beginning of list:");

scanf("%d", &newnode->data);

newnode->next = start;

newnode->prev = tail;

start = newnode;

tail->next = newnode;
newnode -> next -> prev = newnode;

printf("\n");

temp = start;

while (temp->next != start){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data inserted at the beginning successfully.\n");

// add new node at specific position

void add_node_speposc(){

struct node *newnode;

struct node *temp;

int pos;

int i = 1;

printf("Enter the position where you want to insert data:");

scanf("%d", &pos);
temp = start;

while (i < pos - 1){

temp = temp->next;

i++;

newnode = (struct node *)malloc(sizeof(struct node));

printf("Enter the data to you want to insert:");

scanf("%d", &newnode->data);

newnode->next = temp->next;

newnode->prev = temp;

temp -> next -> prev = newnode;

temp->next = newnode;

printf("\n");

temp = start;

while (temp->next != tail -> next){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

}
printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data inserted at position %d successfully.\n", pos);

// traverse of nodes

void traverse(){

struct node *newnode;

struct node *temp;

temp = start;

while (temp->next != tail ->next){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

// counting of node

int count_node(){

struct node *newnode;

struct node *temp;

int count = 0;
temp = start;

while (temp->next != tail ->next){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

count++;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("\n");

printf("Number of nodes in linked list are: %d\n", count + 1);

// deletion of nodes

void delete_node_beg(){

struct node *newnode;

struct node *temp;

temp = start;

start = temp->next;

temp -> next -> prev = tail;

tail->next = start;
free(temp);

temp = start;

while (temp->next != tail -> next) {

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

// delete node at specific position

void delete_node_spec(){

struct node *newnode;

struct node *temp;

int pos;

int i = 0;

printf("Enter the position at which data you want to delete:");

scanf("%d", &pos);

temp = start;
while (i < pos - 1){

temp = temp->next;

i++;

temp -> prev -> next = temp -> next;

temp -> next -> prev = temp -> prev;

free(temp);

printf("\n");

temp = start;

while (temp->next != tail ->next){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

printf("Data Deleted at position %d successfully.\n", pos);

// delete node at end

void delete_node_end(){

struct node *newnode;


struct node *temp;

temp = tail;

tail = tail -> prev;

tail -> next = start;

start -> prev = tail;

free(temp);

temp = start;

while (temp->next != tail -> next){

printf("Data in node present at address %d is %d: \n", temp, temp->data);

temp = temp->next;

printf("Data in node present at address %d is %d: \n", temp, temp->data);

// reverse of Linked list

void reverse(){

// struct node *newnode;

// struct node *temp;

// struct node *tail;


// int count = 0;

// int temp2;

// temp = start;

// while (temp != NULL){

// tail = temp;

// temp = temp->next;

// count++;

// }

// temp = start;

// for(int i=0; i < count/2; i++){

// temp2 = temp ->data;

// temp -> data = tail -> data;

// tail -> data = temp2;

// temp= temp -> next;

// tail = tail -> prev;

// }

// printf("\n");

// printf("After Reverse of linked list.\n");

// temp = start;

// while (temp != NULL){


// printf("Data in node present at address %d is %d: \n", temp, temp-
>data);

// temp = temp->next;

// }

// sorting of linked list

void sorting(){

struct node *newnode;

struct node *temp;

struct node *temp2;

int temp3;

int count = 0;

int i, j;

temp = start;

while (temp->next != start){

count++;

temp = temp->next;

printf("\n");
for (i = 0; i <= count; i++){

temp = start;

temp2 = temp->next;

for (j = 0; j <= count - 1; j++){

if (temp->data > temp2->data){

temp3 = temp->data;

temp->data = temp2->data;

temp2->data = temp3;

temp = temp2;

temp2 = temp2->next;

temp = start;

while (temp->next != start){

printf("Data in node after sorting present at address %d is %d: \n", temp,


temp->data);

temp = temp->next;

printf("Data in node after sorting present at address %d is %d: \n", temp,


temp->data);
printf("Data sorted successfully.\n");

// search of node

void search(){

struct node *newnode;

struct node *temp;

int count = 0;

int ser;

int flag = 1;

printf("Enter the data you want to search: ");

scanf("%d", &ser);

printf("\n");

temp = start;

while (temp->next != tail -> next){

count++;

if (temp->data == ser){

printf("Data is %d present at position : %d\n", ser, count);


printf("Data found successfully.");

flag--;

break;

temp = temp->next;

while (flag){

if (temp->data == ser){

printf("Data is %d present at position : %d\n", ser, count + 1);

printf("Data found successfully.");

else{

printf("Data not found.");

Output:-

Addition at end:-
Addition at Beginning:-
Addition at Specific Position:-

Traverse:-
Node Count:-

Deletion at Beginning:-
Deletion at End:-
Deletion at Specific Position:-
Reverse:-

Sorting:-

Searching:-
14. Write a program to perform following operations on Stack using Arrays.

#include<stdio.h>

#include<stdlib.h>

#define N 5

int stack[N];

int top=-1;

void push(){

int x;

printf("Enter the data");

scanf("%d",&x);

if(top==N-1){

printf("stack is overflow\n");

else{

top++;

stack[top]=x;

void pop()

{
//int item;

if(top==-1)

printf("Underflow\n");

else{

int item=stack[top];

top--;

printf("%d",item);

void peek()

if(top==-1)

printf("stack is empty\n");

else{

printf("%d",stack[top]);

}
void traverse(){

if(top==-1){

printf("Stack is empty\n");

else{

printf("stack empty");

for(int i=top; i>=0; i--){

printf("%d",stack[i]);

printf("\n");

int main(){

int choice;

while(1){

printf("\nstack Opration :\n");

printf("1. push\n");

printf("2 pop\n");

printf("3. peek\n");

printf("4. traverse\n");

printf("5. exit\n");
printf("Enter your choice:");

scanf("%d",&choice);

switch(choice){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

traverse();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

}
}

return 0;

}
15. Write a program to implement stack using linked list and perform
following operations on it.

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *link;

};

struct node *top=NULL;

void push(int x){

struct node *new_node;

new_node = (struct node*)malloc(sizeof(struct node));

if(new_node == NULL){

printf("memory allocation faild");

return;

new_node -> data=x;

new_node -> link=top;

top = new_node;

void pop(){
struct node *temp;

//temp=top;

if(top==NULL){

printf("underflow");

return;

else{

temp = top;

printf("%d\n",top->data);

top=top->link;

free(temp);

void peek(){

if(top==NULL)

printf("stack is empty\n");

else{

printf("temp element is %d\n",top->data);

}
}

void display()

//temp = top;

if(top == NULL){

printf("Stack is empty\n");

else {

struct node *temp=top;

printf("Stack element are\n");

while(temp!=NULL){

printf("%d\n", temp->data);

temp = temp->link;

printf("\n");

int main(){

push(10);

push(20);
push(30);

push(40);

push(50);

//display();

pop();

pop();

peek();

display();

return 0;

}
16. Write a program to convert an expression from infix to postfix using stack

#include<stdi.h>

#include <ctype.h> // Include ctype.h for isalnum() function

#define MAX_SIZE 20 // Define maximum size for the stack

char stack[MAX_SIZE];

int top = -1;

void push(char x) {

if (top < MAX_SIZE - 1) {

stack[++top] = x;

} else {

printf("Stack Overflow\n");

char pop() {

if (top == -1) {

return -1; // Stack is empty

} else {
return stack[top--];

int priority(char x) {

if (x == '(') {

return 0;

} else if (x == '+' || x == '-') {

return 1;

} else if (x == '*' || x == '/') {

return 2;

return -1; // Return -1 for invalid operators

int main() {

char exp[MAX_SIZE];

char *e, x;

printf("Enter the expression: ");

scanf("%s", exp);
e = exp;

printf("Postfix Expression: ");

while (*e != '\0')

if (isalnum(*e))

{ // Check if the character is alphanumeric

printf("%c", *e);

else if (*e == '(')

push(*e);

else if (*e == ')')

while ((x = pop()) != '(')

printf("%c", x);

}
else

while (top != -1 && priority(stack[top]) >= priority(*e))

printf("%c", pop());

push(*e);

e++;

while (top != -1)

printf("%c", pop());

printf("\n");

return 0;

}
17. Write a program to evaluate a postfix expression using stack.

#include<stdio.h>

#include<string.h>

#include<ctype.h>

#include<math.h>

void push(int);

int pop();

void display();

int s[20] = {0};

int top = -1; // Corrected initialization of top

void main() {
char postfix[20] = {'\0'}, element;

int i, num1, num2, ans;

printf("\n Enter postfix expression: ");

scanf("%s", postfix);

printf("\n Postfix expression: %s", postfix);

i = 0;

while (i <= strlen(postfix) - 1) { // Corrected the while loop condition

element = postfix[i];

if (isdigit(element)) {

push(element - '0');

} else {

num1 = pop();

num2 = pop();

switch (element) {

case '^':

ans = pow(num2, num1);

break;

case '/':

ans = num2 / num1;


break;

case '*':

ans = num2 * num1;

break;

case '+':

ans = num2 + num1;

break;

case '-':

ans = num2 - num1;

break;

push(ans);

i++;

printf("\n Result: %d", pop());

void push(int element) {

if (top == 19)
printf("\n Stack is full");

else {

top++;

s[top] = element;

int pop() {

if (top == -1) {

printf("\n Stack is empty");

return -1; // Return a meaningful value when stack is empty

} else {

int ch = s[top];

top--;

return ch;

void display() {

int i;

printf("\n Stack: ");


for (i = 0; i <= top; i++)

printf("%d ", s[i]);

18. Write a program to implement linear queue using Array and perform the
following operation.

#include<stdio.h>

#define N 5

int queue[N];

int front = -1;

int rear = -1;

void enqueue(int x) {

if(rear == N-1) {
printf("Overflow\n");

} else if(front == -1 && rear == -1) {

front = rear = 0;

queue[rear] = x;

} else {

rear++;

queue[rear] = x;

void dequeue() {

if(front == -1 && rear == -1) {

printf("Queue is underflow\n");

} else if(front == rear) {

printf("%d\n", queue[front]);

front = rear = -1;

} else {

printf("%d\n", queue[front]);

front++;

}
}

void peek() {

if(front == -1 && rear == -1) {

printf("Underflow\n");

} else {

printf("%d\n", queue[front]);

void display() {

int i;

if(front == -1 && rear == -1) {

printf("Underflow\n");

} else {

for(i = front; i <= rear; i++) {

printf("%d ", queue[i]);

printf("\n");

}
int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

peek();

dequeue();

dequeue();

return 0;

}
19. Write a program to implement linear queue to using linked list and
perform the following operations.

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *next;

};

struct node *front=NULL;

struct node *rear=NULL;

void enqueue(int x){

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

newnode->data=x;

newnode->next=NULL;

if(front==NULL && rear==NULL)

front=rear=newnode;

else{

rear->next=newnode;
rear=newnode;

void dequeue(){

struct node*temp;

temp=front;

if(front==NULL && rear==NULL)

printf("queue is empty\n");

else{

printf("%d\n", front->data);

front=front->next;

free(temp);

void display()

struct node *temp;

if(front==NULL && rear==NULL){


printf("Queue is empty");

else{

temp=front;

while(temp!=NULL)

printf("%d",temp->data);

temp=temp->next;

printf("\n");

void peek(){

if(front==NULL && rear==NULL){

printf("queue is empty\n");

else{

printf("%d\n",front->data);

int main()
{

enqueue(10);

enqueue(20);

enqueue(30);

printf("Queue is: ");

display();

printf("Dequeue is: ");

dequeue();

printf("front of the queue: ");

peek();

return 0;

}
20. Write a program to implement circular queue using array and perform the
following operation.

#include<stdio.h>

#include<stdlib.h>

#define N 5

int queue[N];

int front=-1;

int rear=-1;

void enqueue(int x)

if((rear+1)%N == front)

printf("Queue is full ");

else if(front==-1 && rear==-1){

front=rear=0;

queue[rear]=x;

else{

rear=(rear+1)%N;

queue[rear]=x;
}

void dequeue()

if(front==-1 && rear==-1)

printf("Queue is empty");

else if(front==rear)

front=rear=-1;

else{

printf("%d\n",queue[front]);

front==(front+1)%N;

void display()

int i = 0;

if(front==-1 && rear==-1)


{

printf("Queue is empty");

else{

printf("Queue is:");

while(i!=rear)

printf("%d\t", queue[i]);

i=(i+1)%N;

printf("%d\n", queue[rear]);

void peek()

if(front==-1 && rear==-1){

printf("Queue is underflow");

else{

printf("\t%d",queue[front]);

}
}

int main(){

enqueue(15);

enqueue(25);

enqueue(29);

enqueue(10);

printf("Queue is :");

display();

printf("Dequeue is:");

dequeue();

printf("Front of the queue:");

peek();

return 0;

}
23. Write a program to implement concept of merge Sort.

#include <stdio.h>

void merge(int A[], int lb, int mid, int ub) {

int n1 = mid - lb + 1;

int n2 = ub - mid;

int L[n1], R[n2];

for (int i = 0; i < n1; i++)

L[i] = A[lb + i];

for (int j = 0; j < n2; j++)

R[j] = A[mid + 1 + j];

int i = 0, j = 0, k = lb;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

A[k] = L[i];

i++;

} else {

A[k] = R[j];

j++;
}

k++;

while (i < n1) {

A[k] = L[i];

i++;

k++;

while (j < n2) {

A[k] = R[j];

j++;

k++;

void MergeSort(int A[], int lb, int ub) {

if (lb < ub) {


int mid = lb + (ub - lb) / 2;

MergeSort(A, lb, mid);

MergeSort(A, mid + 1, ub);

merge(A, lb, mid, ub);

void printArray(int A[], int size) {

for (int i = 0; i < size; i++)

printf("%d ", A[i]);

printf("\n");

int main() {

int A[] = {15,5,24,8,1,3,16,10};

int n = sizeof(A) / sizeof(A[0]);

printf("Original array: ");


printArray(A, n);

MergeSort(A, 0, n - 1);

printf("Sorted array: ");

printArray(A, n);

return 0;

}
24. Write a program to implement Quick Sort.

#include<stdio.h>

int partition(int a[], int lb, int ub)

int pivot=a[lb];

int start=lb;

int end=ub;

while(start<end)

while(a[start]<=pivot)

start++;

while(a[end]>pivot)

end--;

if(start<end)

int temp = a[start];

a[start] = a[end];
a[end] = temp;

int temp = a[lb];

a[lb] = a[end];

a[end] = temp;

return end;

void QuickSort(int A[], int lb, int ub)

if(lb<ub)

int loc = partition(A,lb,ub);

QuickSort(A,lb,loc-1);

QuickSort(A,loc+1,ub);

int main() {

int arr[] = {10, 15, 1, 2, 9, 16, 11, 4};


int n = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

QuickSort(arr, 0, n - 1);

printf("Sorted Array: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

}
25. Write a program to implement Shell Sort.

#include<stdio.h>

void shellSort(int a[], int n){

int gap, i,j;

for(gap=n/2; gap>0; gap/=2)

for(j=gap; j<n; j++)

int temp = a[j];

i=j;

while(i>=gap && a[i-gap]>temp){

a[i]=a[i-gap];

i-=gap;

a[i]=temp;

int main(){

int a[]={15,5,24,8,1,3,16,10,20};

int n=sizeof(a)/sizeof(a[0]);
printf("Array before sorting\n");

for(int i=0; i<n; i++){

printf("%d\t",a[i]);

printf("\n");

shellSort(a,n);

printf("Array After sorting\n");

for(int i=0; i<n; i++){

printf("%d\t",a[i]);

printf("\n");

return 0;

You might also like