DATA STRUCTURE Lab Book Solution Sy BCA



 
Assignment No-1
 
Set A
 
1) write a C program  to Count number of occurence (or frequency) given sorted array.
Input = are [ ]={1,1,2,2,2,2,3 3, x=2
output: 4/1/x (or 2) occurs & times in arr[ ]
 
#include<stdio.h>
void read(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\nEnter the number: ");
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;
    for (i = 0; i < n; i++)
        printf("%d\t", a[i]);
}
int count_occurrence(int a[], int n, int s) {
    int i, count = 0;
    for (i = 0; i < n; i++) {
        if (a[i] == s) {
            count++;
        }
    }
    return count;
}
int main() {
    int a[20], n, s, c;
    printf("Enter how many elements in the array: ");
    scanf("%d", &n);
    read(a, n);
    printf("\nArray elements are: ");
    display(a, n);
    printf("\nEnter search element: ");
    scanf("%d", &s);
    c = count_occurrence(a, n, s);
    if (c == 0)
        printf("\nSearch element not found");
    else
        printf("\nSearch element %d occurs %d times", s, c);
    return 0;
}


  

Example Output:
 
Enter how many elements in the array: 9
Enter the number: 1
Enter the number: 1
Enter the number: 2
Enter the number: 2
Enter the number: 2
Enter the number: 2
Enter the number: 3
Enter the number: 3
Enter the number: 3
 
Array elements are: 1   1   2   2   2   2   3   3   3  
 
Enter search element: 2
 
Q2. write a c program to accept n elements, Store those elements and Store the Square of these numbers in another array & display both the array
 
#include<stdio.h>
void read(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\nEnter the number: ");
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d\t", a[i]);
    }
}
void square(int a[], int n, int b[]) {
    for (int i = 0; i < n; i++) {
        b[i] = a[i] * a[i];
    }
}
int main() {
    int a[20], b[20], n;
    printf("\nEnter how many elements in the array: ");
    scanf("%d", &n);
    printf("\nEnter array elements:");
    read(a, n);
    square(a, n, b);
    printf("\nFirst array: ");
    display(a, n);
    printf("\nSecond array (squares): ");
    display(b, n);
    return 0;
}
Example Output:
Enter how many elements in the array: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
First array: 1  2   3   4   5  
Second array (squares): 1   4   9   16  25  
 

 
3) Write a C program to Copy one array into another array.
#include <stdio.h>
void read(int a[], int n) {
int i;  // Declare variable at the start of the block
for (i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
 int i;  // Declare variable at the start of the block
 printf("Array elements: ");
 for (i = 0; i < n; i++) {
 printf("%d\t", a[i]);
}
    printf("\n");
}
void copy(int a[], int n, int b[]) {
    int i;  // Declare variable at the start of the block
    for (i = 0; i < n; i++) {
        b[i] = a[i];
    }
}
int main() {
    int a[20], b[20];
    int n;
    printf("Enter how many elements in the array (max 20): ");
    scanf("%d", &n);
    if (n > 20 || n <= 0) {
        printf("Please enter a valid number of elements (1-20).\n");
        return 1;
    }
    printf("Enter array elements:\n");
    read(a, n);
    copy(a, n, b);
    printf("First array:\n");
    display(a, n);
    printf("Second array (copied elements):\n");
    display(b, n);
    return 0;
}
 

Sample Output
 
Enter how many elements in the array (max 20): 5
Enter array elements:
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
First array:
Array elements: 10   20   30   40   50
Second array (copied elements):
Array elements: 10   20   30   40   50
 
SET B:
 
1) Write a program to accept n elements of 1D array and then display sum of all elements of array.
 #include <stdio.h>
void read(int a[], int n) {
    int i;  // Declare variable at the start of the block
    for (i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;  // Declare variable at the start of the block
    printf("Array elements: ");
    for (i = 0; i < n; i++) {
        printf("%d\t", a[i]);
    }
    printf("\n");
}
int sum(int a[], int n) {
    int i;  // Declare variable at the start of the block
    int count = 0;
    for (i = 0; i < n; i++) {
        count += a[i];
    }
    return count;
}
int main() {
    int a[20];
    int n;
    int total;  // Move this declaration to the top
    printf("Enter how many elements in the array (max 20): ");
    scanf("%d", &n);
    if (n > 20 || n <= 0) {
        printf("Please enter a valid number of elements (1-20).\n");
        return 1;
    }
    printf("Enter array elements:\n");
    read(a, n);
    printf("First array:\n");
    display(a, n);
    total = sum(a, n);  // Now you can use the 'total' variable
    printf("The sum of the elements in the array = %d\n", total);
    return 0;
}
 Sample Output
Enter how many elements in the array (max 20): 5
Enter array elements:
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50
First array:
Array elements: 10    20    30    40    50    
The sum of the elements in the array = 150
 
2) Write a ‘C’ program to accept n elements store those elements in array and find and replace a given number.

 #include <stdio.h>
void read(int a[], int n) {
    int i;  // Declare variable at the start of the block
    for (i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;  // Declare variable at the start of the block
    printf("Array elements: ");
    for (i = 0; i < n; i++) {
        printf("%d\t", a[i]);
    }
    printf("\n");
}
void replace(int a[], int n, int x, int y) {
    int i;  // Declare variable at the start of the block
    for (i = 0; i < n; i++) {
        if (a[i] == x) {
            a[i] = y;
        }
    }
}
int main() {
    int a[20];  // Declare variables at the top of the function
    int n, oldValue, newValue;  // Declare variables at the top
    printf("Enter how many elements in the array (max 20): ");
    scanf("%d", &n);
    if (n > 20 || n <= 0) {
        printf("Please enter a valid number of elements (1-20).\n");
        return 1;
    }
    printf("Enter array elements:\n");
    read(a, n);
    printf("Current ");
    display(a, n);
    printf("Enter the element to change: ");
    scanf("%d", &oldValue);
    printf("Enter the new value: ");
    scanf("%d", &newValue);
    replace(a, n, oldValue, newValue);
    printf("Updated ");
    display(a, n);
    return 0;
}
 Sample Output
 Enter how many elements in the array (max 20): 5
Enter array elements:
Enter number 1: 1
Enter number 2: 2
Enter number 3: 2
Enter number 4: 3
Enter number 5: 2
Current Array elements: 1   2   2   3   2    
Enter the element to change: 2
Enter the new value: 5
Updated Array elements: 1   5   5   3   5    



Assignment 2

 

SET A

 1). Write a C program to accept n elements from user store it in an array. Accept a value from the user and use linear/Sequential search method to check whether the value is present in array or not. Display proper message.

 

#include <stdio.h>

#include <conio.h>

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

    int i;

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

        printf("Enter the number: ");

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

    }

}

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

    int i;

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

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

    }

    printf("\n");

}

int linear_search(int a[], int n, int key) {

    int i;

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

        if (a[i] == key) {

            return i;

        }

    }

    return -1;

}

void main() {

    int a[20], n, key, result;

    clrscr();

    printf("Enter how many elements in the array: ");

    scanf("%d", &n);

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

    read(a, n);

    printf("Array elements are:\n");

    display(a, n);

    printf("Enter search element: ");

    scanf("%d", &key);

    result = linear_search(a, n, key);

    if (result == -1) {

        printf("\nSearch element not found\n");

    } else {

        printf("\nSearch element found at position %d\n", result + 1);

    }

    getch();  

} 


 Example Output:

 Enter how many elements in the array: 5

Enter array elements:

Enter the number: 10

Enter the number: 20

Enter the number: 30

Enter the number: 40

Enter the number: 50

 

Array elements are:

10    20    30    40    50    

 

Enter search element: 30

 Search element found at position 3

 

 

2) Write a C program to accept n elements from user store it in an array. Accept a value from the user and use binary search method to check whether the value is present in array or not. Display proper message. (Students should accept sorted array and use Recursive function).

 

#include <stdio.h>

#include <conio.h>

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

    int i;

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

        printf("Enter the number: ");

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

    }

}

 

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

    int i;

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

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

    }

    printf("\n");

}

int binary_search(int a[], int key, int low, int high) {

    if (low > high) {

        return -1;

    }

        int mid = (low + high) / 2;

    if (a[mid] == key) {

        return mid;

    } else if (key < a[mid]) {

        return binary_search(a, key, low, mid - 1);  

    } else {

        return binary_search(a, key, mid + 1, high);

    }

}

void main() {

    int a[20], n, key, result;

    clrscr();  

    printf("Enter how many elements (sorted) in the array: ");

    scanf("%d", &n);

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

    read(a, n);

    printf("Array elements are:\n");

    display(a, n);

    printf("Enter the element to search: ");

    scanf("%d", &key);

    result = binary_search(a, key, 0, n - 1);

    if (result == -1) {

        printf("\nSearch element not found\n");

    } else {

        printf("\nSearch element found at position %d\n", result + 1);

    }

    getch();  


}

 

 Example Output:

 

Enter how many elements (sorted) in the array: 5

Enter array elements in ascending order:

Enter the number: 10

Enter the number: 20

Enter the number: 30

Enter the number: 40

Enter the number: 50

Array elements are:

10    20    30    40    50    

Enter the element to search: 30

 

Search element found at position 3

 

3) Write a ‘C’ program to create a random array of n integers. Accept a value of n from user and use Binary search algorithm to check whether the number is present in array or not

(Students should accept sorted array and use Non-Recursive function also use random function).

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

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

    int i;

    a[0] = rand() % 10;

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

        a[i] = a[i-1] + rand() % 10;  

    }

}

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

    int i;

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

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

    }

    printf("\n");

}

int binary_search(int a[], int n, int key) {

    int low = 0, high = n - 1, mid;

    while (low <= high) {

        mid = (low + high) / 2;

        if (a[mid] == key) {

            return mid;  

        } else if (key < a[mid]) {

            high = mid - 1;  

        } else {

            low = mid + 1;  

        }

    }

    return -1;  

}

int main() {

    int a[100], n, key, result;

    srand(time(0));  

    printf("Enter how many elements in the array: ");

    scanf("%d", &n);

    generate_random_array(a, n);

    printf("Generated array elements are:\n");

    display(a, n);

    printf("Enter the element to search: ");

    scanf("%d", &key);

    result = binary_search(a, n, key);

    if (result == -1) {

        printf("\nSearch element not found\n");

    } else {

        printf("\nSearch element found at position %d\n", result + 1);

    }

    return 0;

}

 


 Example Output:

 

Enter how many elements in the array: 10

Generated array elements are:

3     7     14    21    25    34    40    47    50    55    

Enter the element to search: 34

 

Search element found at position 6

 

SET B

 

1) Write a ‘C’ program to accept the names of cities and store them in array. Accept the city name from user and use linear search algorithm to check whether the city is present in array or not.

 

#include <stdio.h>

#include <conio.h>

#include <string.h>

void read(char ct[][30], int n) {

    int i;

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

        printf("Enter the city name: ");

        scanf("%s", ct[i]);

    }

}

void display(char ct[][30], int n) {

    int i;

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

        printf("%s\t", ct[i]);

    }

    printf("\n");

}

int linear_search(char ct[][30], int n, char search_city[]) {

    int i;

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

        if (strcmp(ct[i], search_city) == 0) {

            return i;  

        }

    }

    return -1;  

}

int main() {

    int n, result;

    char ct[20][30], search_city[30];

    clrscr();  

    printf("Enter how many cities: ");

    scanf("%d", &n);

    read(ct, n);

    printf("City names stored in array:\n");

    display(ct, n);

    printf("Enter the city name to search: ");

    scanf("%s", search_city);

    result = linear_search(ct, n, search_city);

    if (result == -1) {

        printf("\nSearch city is not found.\n");

    } else {

        printf("\nSearch city is found at position %d.\n", result + 1);

    }

    getch();  

    return 0;

}

 


 Example Output:

 

Enter how many cities: 3

Enter the city name: Pune

Enter the city name: Mumbai

Enter the city name: Delhi

City names stored in array:

Pune    Mumbai  Delhi    

Enter the city name to search: Mumbai

 

Search city is found at position 2.

 

 

Assignment 3

 

SET A:

 

1) Write a C program to accept and sort n elements in ascending order by using bubble sort.

 

#include <stdio.h>

#include <conio.h>

void bubbleSort(int arr[], int n) {

    int i, j, temp;

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

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

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

                // Swap arr[j] and arr[j + 1]

                temp = arr[j];

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

                arr[j + 1] = temp;

            }

        }

    }

}

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

    int i;

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

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

    }

    printf("\n");

}

void main() {

    int n, i;

    int arr[100];  

    clrscr();  

    printf("Enter the number of elements: ");

    scanf("%d", &n);

 

    printf("Enter %d elements: ", n);

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

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

    }

    bubbleSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    getch();  

}

 


Sample Output:

 

Enter the number of elements: 5

Enter 5 elements: 12 7 22 9 4

Sorted array: 4 7 9 12 22

 

 

2) Write a C program to accept and sort n elements in ascending order by using insertion sort.

 

#include <stdio.h>

#include <conio.h>

void insertionSort(int arr[], int n) {

    int i, key, j;

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

        key = arr[i];

        j = i - 1;

        while (j >= 0 && arr[j] > key) {

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

            j = j - 1;

        }

        arr[j + 1] = key;

    }

}

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

    int i;

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

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

    }

    printf("\n");

}

void main() {

    int n, i;

    int arr[100];  

    clrscr();  

    printf("Enter the number of elements: ");

    scanf("%d", &n);

    printf("Enter %d elements: ", n);

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

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

    }

    insertionSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    getch();  

}


Sample Output:

 

Enter the number of elements: 5

Enter 5 elements: 11 3 7 9 5

Sorted array: 3 5 7 9 11

 

3) Write a ‘C’ program to accept and sort n elements in ascending order using Selection sort method.

 

#include <stdio.h>

#include <conio.h>

void selectionSort(int arr[], int n) {

    int i, j, min_idx, temp;

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

        min_idx = i;

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

            if (arr[j] < arr[min_idx]) {

                min_idx = j;

            }

        }

        temp = arr[min_idx];

        arr[min_idx] = arr[i];

        arr[i] = temp;

    }

}

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

    int i;

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

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

    }

    printf("\n");

}

void main() {

    int n, i;

    int arr[100];  

    clrscr();  

    printf("Enter the number of elements: ");

    scanf("%d", &n);

    printf("Enter %d elements: ", n);

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

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

    }

    selectionSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    getch();  

}


Sample Output:

 

Enter the number of elements: 4

Enter 4 elements: 20 5 12 9

Sorted array: 5 9 12 20



 SET B:

 

1) Write a C program to create a string array with day of week and sort them using Insertion sort.

 

#include <stdio.h>

#include <conio.h>

#include <string.h>

void insertionSort(char arr[][20], int n) {

    int i, j;

    char key[20];

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

        strcpy(key, arr[i]);

        j = i - 1;

        while (j >= 0 && strcmp(arr[j], key) > 0) {

            strcpy(arr[j + 1], arr[j]);

            j = j - 1;

        }

        strcpy(arr[j + 1], key);

    }

}

void printArray(char arr[][20], int size) {

    int i;

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

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

    }

}

void main() {

    char days[7][20] = {"Monday", "Wednesday", "Friday", "Tuesday", "Thursday", "Saturday", "Sunday"};

    int n = 7;  

    clrscr();  

    printf("Days before sorting:\n");

    printArray(days, n);

    insertionSort(days, n);

    printf("\nDays after sorting:\n");

    printArray(days, n);

    getch();  

}

Sample Output:

 

Days before sorting:

Monday

Wednesday

Friday

Tuesday

Thursday

Saturday

Sunday

 

Days after sorting:

Friday

Monday

Saturday

Sunday

Thursday         Tuesday          Wednesday

 

2) Write a ‘C’ program to accept names from the user and sort in alphabetical order using bubble sort

a) Accept n name                              b) Bubble sort Function                               c) Display

 

#include <stdio.h>

#include <conio.h>

#include <string.h>

void bubbleSort(char names[][50], int n) {

    char temp[50];

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

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

            if (strcmp(names[j], names[j + 1]) > 0) {

                strcpy(temp, names[j]);

                strcpy(names[j], names[j + 1]);

                strcpy(names[j + 1], temp);

            }

        }

    }

}

void printNames(char names[][50], int n) {

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

        printf("%s\n", names[i]);

    }

}

void main() {

    int n, i;

    char names[10][50];  

    clrscr();  

    printf("Enter the number of names: ");

    scanf("%d", &n);

    printf("Enter %d names:\n", n);

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

        scanf("%s", names[i]);

    }

    bubbleSort(names, n);

    printf("\nSorted names in alphabetical order:\n");

    printNames(names, n);

    getch();  

}


Sample Output:

 

Enter the number of names: 3

Enter 3 names:

John

Anna

Peter

 

Sorted names in alphabetical order:

Anna

John

Peter

 

3) Write a C program to accept and sort n elements in ascending order by using bubble sort and also count the number of swaps. Display the sorted list and total no of swap count.

#include <stdio.h>

#include <conio.h>

int bubbleSort(int arr[], int n) {

    int i, j, temp, swap_count = 0;

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

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

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

                // Swap arr[j] and arr[j + 1]

                temp = arr[j];

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

                arr[j + 1] = temp;

                swap_count++;  

            }

        }

    }

    return swap_count;

}

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

    int i;

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

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

    }

    printf("\n");

}

void main() {

    int n, i, swaps;

    int arr[100];  

    clrscr();  

    printf("Enter the number of elements: ");

    scanf("%d", &n);

   printf("Enter %d elements: ", n);

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

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

    }

    swaps = bubbleSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    printf("Total number of swaps: %d\n", swaps);

    getch();  

}


Sample Output:

 

Enter the number of elements: 5

Enter 5 elements: 10 7 4 3 1

Sorted array: 1 3 4 7 10

Total number of swaps: 10

 

Assignment4

 

SET A:

 

1) Write a C program to accept and sort n elements in ascending order by using merge sort.

 

 

#include <stdio.h>

#include <stdlib.h>

void merge(int arr[], int left, int mid, int right) {

    int i, j, k;

    int n1 = mid - left + 1;

    int n2 = right - mid;    

    int L[n1], R[n2];

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

        L[i] = arr[left + i];

    }

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

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

    }

    i = 0;

    j = 0;

    k = left;

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

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

            arr[k] = L[i];

            i++;

        } else {

            arr[k] = R[j];

            j++;

        }

        k++;

    }

    while (i < n1) {

        arr[k] = L[i];

        i++;

        k++;

    }

    while (j < n2) {

        arr[k] = R[j];

        j++;

        k++;

    }

}

void mergeSort(int arr[], int left, int right) {

    if (left < right) {

        int mid = left + (right - left) / 2;

        mergeSort(arr, left, mid);

        mergeSort(arr, mid + 1, right);

        merge(arr, left, mid, right);

    }

}

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

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

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

    }

    printf("\n");

}

int main() {

    int n;

    printf("Enter number of elements: ");

    scanf("%d", &n);

   

    int arr[n];

        printf("Enter %d elements: ", n);

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

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

    }

    printf("Unsorted array: ");

    printArray(arr, n);

    mergeSort(arr, 0, n - 1);

    printf("Sorted array in ascending order: ");

    printArray(arr, n);

    return 0;

}

Example Output:

 

Enter number of elements: 5

Enter 5 elements: 34 7 23 32 5

Unsorted array: 34 7 23 32 5

Sorted array in ascending order: 5 7 23 32 34

 

SET B:

 

1) Write a C program to create a string array with months (accept atleast 6 month) and sort them using Quick sort.

 

#include <stdio.h>

#include <string.h>

void swap(char *str1, char *str2) {

    char temp[20];

    strcpy(temp, str1);

    strcpy(str1, str2);

    strcpy(str2, temp);

}

int partition(char arr[][20], int low, int high) {

    char pivot[20];

    strcpy(pivot, arr[high]);

    int i = low - 1;

    for (int j = low; j < high; j++) {

        if (strcmp(arr[j], pivot) < 0) {

            i++;

            swap(arr[i], arr[j]);

        }

    }

    swap(arr[i + 1], arr[high]);

    return i + 1;

}

void quickSort(char arr[][20], int low, int high) {

    if (low < high) {

        int pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);

        quickSort(arr, pi + 1, high);

    }

}

 

void printArray(char arr[][20], int size) {

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

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

    }

}

int main() {

    int n = 6;

    char months[12][20] = {

        "January", "February", "March", "April", "May", "June",

        "July", "August", "September", "October", "November", "December"

    };

    printf("Enter the number of months to sort (minimum 6): ");

    scanf("%d", &n);

    if (n < 6) {

        printf("You must enter at least 6 months!\n");

        return 1;

    }

    printf("\nMonths before sorting:\n");

    printArray(months, n);

    quickSort(months, 0, n - 1);

    printf("\nMonths after sorting:\n");

    printArray(months, n);

    return 0;

}


 

Example Output:

 

Enter the number of months to sort (minimum 6): 6

 

Months before sorting:

January

February

March

April

May

June

 

Months after sorting:

April

February

January

June

March

May




Assignment5

 

SET A

 

1) Write a C program to implement a singly linked list with Create and Display operation.

 

#include<stdio.h>

#include<stdlib.h>

typedef struct node {

    int data;

    struct node *next;

} node;

node* create(int x) {

    node *newnode, *head = NULL;

    while (x > 0) {

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

        newnode->data = x % 10;                

        newnode->next = head;                  

        head = newnode;                        

        x = x / 10;                            

    }

    return head;  

}

void display(node *head) {

    node *temp;

    if (head == NULL) {

        printf("\nThe singly linked list is empty.\n");

    } else {

        temp = head;

        printf("\nElements of the singly linked list: ");

        while (temp != NULL) {

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

            temp = temp->next;          

        }

        printf("\n");

    }

}

int main() {

    node *head = NULL;

    int x;

    printf("\nEnter a number: ");

    scanf("%d", &x);

    head = create(x);  

    display(head);    

    return 0;

}


 

Example Output:

 

Enter a number: 12345

Elements of the singly linked list: 5    4    3    2    1    

 

2) Write a C program to implement a Circular Singly linked list with Create and Display operation.

 

#include<stdio.h>

#include<stdlib.h>

typedef struct node {

    int data;

    struct node *next;

} node;

node* create(int x) {

    node *newnode, *head = NULL, *temp = NULL;

    int value;

 

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

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

        printf("Enter data for node %d: ", i+1);

        scanf("%d", &value);

        newnode->data = value;

        newnode->next = NULL;

        if (head == NULL) {

            head = newnode;  

            newnode->next = head;

        } else {

            temp = head;

            while (temp->next != head) {

                temp = temp->next;

            }

            temp->next = newnode;  

            newnode->next = head;  

        }

    }

    return head;

}

void display(node *head) {

    node *temp;

    if (head == NULL) {

        printf("\nCircular Singly Linked List is empty.\n");

    } else {

        temp = head;

        printf("\nElements of the Circular Singly Linked List: ");

        do {

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

            temp = temp->next;

        } while (temp != head);  

        printf("\n");

    }

}

int main() {

    node *head = NULL;

    int n;

    printf("Enter the number of nodes: ");

    scanf("%d", &n);

    head = create(n);  

    display(head);    

    return 0;

}


 Example Output:

 

Enter the number of nodes: 4

Enter data for node 1: 10

Enter data for node 2: 20

Enter data for node 3: 30

Enter data for node 4: 40

 

Elements of the Circular Singly Linked List: 10    20    30    40

 

3) Write a C program to implement a doubly linked list with Create and Display operation.

 

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

    int data;              

    struct node *next;    

    struct node *prev;    

} node;

node* create(int n) {

    node *head = NULL, *newnode, *temp;

 

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

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

        if (newnode == NULL) {

            printf("Memory allocation failed\n");

            exit(1);

        }

        printf("Enter data for node %d: ", i + 1);

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

        newnode->next = NULL;

        newnode->prev = NULL;

 

        if (head == NULL) {

            head = newnode;  

        } else {

            temp = head;

            while (temp->next != NULL) {

                temp = temp->next;

            }

            temp->next = newnode;  

            newnode->prev = temp;  

        }

    }

    return head;

}

void display(node *head) {

    node *temp = head;

    if (temp == NULL) {

        printf("\nDoubly Linked List is empty.\n");

        return;

    }

 

    printf("\nElements of the Doubly Linked List: ");

    while (temp != NULL) {

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

        temp = temp->next;

    }

    printf("\n");

}

int main() {

    node *head = NULL;

    int n;

    printf("Enter the number of nodes: ");

    scanf("%d", &n);

    head = create(n);  

    display(head);    

    return 0;

}


 Example Output:

Enter the number of nodes: 3

Enter data for node 1: 10

Enter data for node 2: 20

Enter data for node 3: 30

 

Elements of the Doubly Linked List: 10    20    30

 

4) Write a C program to implement a Circular doubly linked list with Create and Display operation

 

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

    int data;

    struct node *next;

    struct node *prev;

} node;

node* create(int n) {

    node *head = NULL, *newnode, *temp;

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

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

        printf("Enter data for node %d: ", i + 1);

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

        newnode->next = NULL;

        newnode->prev = NULL;

        if (head == NULL) {

            head = newnode;  

            newnode->next = head;  

            newnode->prev = head;  

        } else {

            temp = head;

            while (temp->next != head) {

                temp = temp->next;

            }

            temp->next = newnode;  

            newnode->prev = temp;  

            newnode->next = head;  

            head->prev = newnode;    

        }

    }

    return head;

}

void display(node *head) {

    node *temp = head;

    if (temp == NULL) {

        printf("\nCircular Doubly Linked List is empty.\n");

        return;

    }

    printf("\nElements of the Circular Doubly Linked List: ");

    do {

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

        temp = temp->next;

    } while (temp != head);

    printf("\n");

}

int main() {

    node *head = NULL;

    int n;

    printf("Enter the number of nodes: ");

    scanf("%d", &n);

    head = create(n);  

    display(head);    

   return 0;

}


 Example Output:

 

Enter the number of nodes: 3

Enter data for node 1: 10

Enter data for node 2: 20

Enter data for node 3: 30

 

Elements of the Circular Doubly Linked List: 10    20    30

 

Assignment 6

 

SET A

 

1) Write a C program to implement Static implementation of stack of integers with following operation:

-Initialize(), push(), pop(), isempty(), isfull(), display()

 

#include <stdio.h>

#include <conio.h>

#define MAX 5  

typedef struct {

    int a[MAX];

    int top;

} Stack;

void initstack(Stack *st) {

    st->top = -1;

}

 

int isempty(Stack *st) {

    if (st->top == -1)

        return 1;

    else

        return 0;

}

int isfull(Stack *st) {

    if (st->top == MAX - 1)

        return 1;

    else

        return 0;

}

 

void push(Stack *st, int item) {

    if (!isfull(st)) {

        st->top++;

        st->a[st->top] = item;

    } else {

        printf("Stack is full\n");

    }

}

int pop(Stack *st) {

    if (!isempty(st)) {

        int item = st->a[st->top];

        st->top--;

        return item;

    } else {

        printf("Stack is empty\n");

        return -1;

    }

}

int peek(Stack *st) {

    if (!isempty(st)) {

        return st->a[st->top];

    } else {

        printf("Stack is empty\n");

        return -1;

    }

}

 

void display(Stack *st) {

    if (!isempty(st)) {

        printf("Stack elements = ");

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

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

        }

        printf("\n");

    } else {

        printf("Stack is empty\n");

    }

}

int main() {

    int item, ch;

    Stack st;

    initstack(&st);  

    do {

        printf("\n1: Push\n2: Pop\n3: Peek\n4: Display\n5: Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &ch);

        switch (ch) {

            case 1:

                printf("Enter the data: ");

                scanf("%d", &item);

                push(&st, item);

                break;

 

            case 2:

                item = pop(&st);

                if (item != -1) {

                    printf("Popped element = %d\n", item);

                }

                break;

            case 3:

                item = peek(&st);

                if (item != -1) {

                    printf("Peeked element = %d\n", item);

                }

                break;

            case 4:

                display(&st);

                break;

            case 5:

                exit(0);

            default:

                printf("Invalid choice!\n");

        }

    } while (ch != 5);

    getch();  

    return 0;

}


 Example Output:

 

1: Push

2: Pop

3: Peek

4: Display

5: Exit

Enter your choice: 1

Enter the data: 10

 

1: Push

2: Pop

3: Peek

4: Display

5: Exit

Enter your choice: 1

Enter the data: 20

 

1: Push

2: Pop

3: Peek

4: Display

5: Exit

Enter your choice: 4

Stack elements = 10    20    

 

1: Push

2: Pop

3: Peek

4: Display

5: Exit


Enter your choice: 2

Popped element = 20

 

1: Push

2: Pop

3: Peek

4: Display

5: Exit

Enter your choice: 3

Peeked element = 10

 

2) Write a C program to implement Dynamic implementation of stack of integers with following operation:

-Initialize(), push(), pop(), isempty(), display().

 

#include <stdio.h>

#include <stdlib.h>

typedef struct Stack {

    int *a;      

    int top;    

    int maxSize;

} Stack;

void initStack(Stack *st, int size) {

    st->a = (int *)malloc(size * sizeof(int));

    if (st->a == NULL) {

        printf("Memory allocation failed\n");

        exit(1);

    }

    st->top = -1;      

    st->maxSize = size;

}

int isEmpty(Stack *st) {

    return (st->top == -1);

}

void push(Stack *st, int item) {

    if (st->top == st->maxSize - 1) {

        printf("Stack is full\n");

        return;

    }

    st->a[++(st->top)] = item;

}

int pop(Stack *st) {

    if (isEmpty(st)) {

        printf("Stack is empty\n");

        return -1;

    }

    return st->a[(st->top)--];

}

void display(Stack *st) {

    if (isEmpty(st)) {

        printf("Stack is empty\n");

        return;

    }

    printf("Stack elements: ");

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

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

    }

    printf("\n");

}

int main() {

    int choice, item, size;

    Stack st;

    printf("Enter the maximum size of the stack: ");

    scanf("%d", &size);

    initStack(&st, size);

    do {

        printf("\n1: Push\n2: Pop\n3: Display\n4: Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                printf("Enter the element to push: ");

                scanf("%d", &item);

                push(&st, item);

                break;

            case 2:

                item = pop(&st);

                if (item != -1) {

                    printf("Popped element = %d\n", item);

                }

                break;

            case 3:

                display(&st);

                break;

            case 4:

                exit(0);

            default:

                printf("Invalid choice!\n");

        }

    } while (choice != 4);

    free(st.a);

    return 0;

}


 Example Output:

 

Enter the maximum size of the stack: 5

 

1: Push

2: Pop

3: Display

4: Exit

Enter your choice: 1

Enter the element to push: 10

 

1: Push

2: Pop

3: Display

4: Exit


Enter your choice: 1

Enter the element to push: 20

 

1: Push

2: Pop

3: Display

4: Exit

Enter your choice: 3

Stack elements: 10    20    

 

1: Push

2: Pop

3: Display

4: Exit

Enter your choice: 2

Popped element = 20

 

1: Push

2: Pop

3: Display

4: Exit

Enter your choice: 3

Stack elements: 10

 

SET B

 

1) Write a ‘C’ program which accepts the string and check whether the string is Palindrome or not using stack. (Use Static/Dynamic implementation of Stack).

 

#include <stdio.h>

#include <string.h>

#define MAX 50

typedef struct Stack {

    char a[MAX];  

    int top;      

} Stack;

void initStack(Stack *st) {

    st->top = -1;  

}

int isEmpty(Stack *st) {

    return (st->top == -1);

}

int isFull(Stack *st) {

    return (st->top == MAX - 1);

}

void push(Stack *st, char item) {

    if (isFull(st)) {

        printf("Stack is full\n");

    } else {

        st->a[++(st->top)] = item;  

    }

}

char pop(Stack *st) {

    if (isEmpty(st)) {

        printf("Stack is empty\n");

        return -1;  

    } else {

        return st->a[(st->top)--];  

    }

}

int checkPalindrome(char s[]) {

    Stack st;

    initStack(&st);

   int i, length = strlen(s);

   

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

        push(&st, s[i]);

    }

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

        if (pop(&st) != s[i]) {

            return 0;  

        }

    }

    return 1;  

}

int main() {

    char s[MAX];

    printf("Enter a word: ");

    scanf("%s", s);

    if (checkPalindrome(s)) {

        printf("%s is a palindrome\n", s);

    } else {

        printf("%s is not a palindrome\n", s);

    }

    return 0;

} 

 Example Output:

 

Enter a word: madam

madam is a palindrome

 

Enter a word: hello

hello is not a palindrome




Assignment 7

 

1) Write a C program to Implement Static implementation of circular queue of integers which includes operation as: a) Initialize() b) insert() c) delete() d) isempty() e) isfull() f) display()

g) peek()

#include<stdio.h>
#include<stdlib.h>
#define MAX 5  
typedef struct queue {
    int a[MAX];
    int front, rear;
} queue;
 
void initqueue(queue *q) {
    q->front = q->rear = -1;
}
int isfull(queue *q) {
    if ((q->rear + 1) % MAX == q->front) {
        return 1;
    } else {
        return 0;
    }
}
int isempty(queue *q) {
    if (q->front == -1) {
        return 1;
    } else {
        return 0;
    }
}
void enqueue(queue *q, int item) {
    if (!isfull(q)) {
        if (isempty(q)) {
            q->front = q->rear = 0;  
        } else {
            q->rear = (q->rear + 1) % MAX;  
        }
        q->a[q->rear] = item;
    } else {
        printf("Queue is full\n");
    }
}
int dequeue(queue *q) {
    int item;
    if (!isempty(q)) {
        item = q->a[q->front];
        if (q->front == q->rear) {  
            q->front = q->rear = -1;  
        } else {
            q->front = (q->front + 1) % MAX;  
        }
        return item;
    } else {
        printf("Queue is empty\n");
        return -1;
    }
}
int peek(queue *q) {
    if (!isempty(q)) {
        return q->a[q->front];
    } else {
        printf("Queue is empty\n");
        return -1;
    }
}
void display(queue *q) {
    int i;
    if (!isempty(q)) {
        printf("Queue elements: ");
        for (i = q->front; i != q->rear; i = (i + 1) % MAX) {
            printf("%d\t", q->a[i]);
        }
        printf("%d\t", q->a[q->rear]);
    } else {
        printf("Queue is empty\n");
    }
}
int main() {
    int item, ch;
    queue q;
    initqueue(&q);
    do {
        printf("\n1: Enqueue \n2: Dequeue \n3: Peek \n4: Display \n5: Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1:
                printf("Enter the data: ");
                scanf("%d", &item);
                enqueue(&q, item);
                break;
            case 2:
                item = dequeue(&q);
                if (item != -1) {
                    printf("Dequeued element = %d\n", item);
                }
                break;
            case 3:
                item = peek(&q);
                if (item != -1) {
                    printf("Peeked element = %d\n", item);
                }
                break;
            case 4:
                display(&q);
                break;
            case 5:
                exit(0);
            default:
                printf("Invalid choice!\n");
        }
    } while (ch != 5);
    return 0;
}

 


 Example Output:

 

1: Enqueue

2: Dequeue

3: Peek

4: Display

5: Exit

Enter your choice: 1

Enter the data: 10

 

1: Enqueue

2: Dequeue

3: Peek

4: Display

5: Exit

Enter your choice: 1

Enter the data: 20

 

1: Enqueue

2: Dequeue

3: Peek

4: Display

5: Exit

Enter your choice: 4

Queue elements: 10    20    

 

1: Enqueue

2: Dequeue

3: Peek

4: Display

5: Exit

Enter your choice: 2

Dequeued element = 10

 

1: Enqueue

2: Dequeue

3: Peek

4: Display

5: Exit

Enter your choice: 3

Peeked element = 20