BCSL-033 Data and File Structures Lab - 240905 - 080604
BCSL-033 Data and File Structures Lab - 240905 - 080604
Ans
Here’s a C program that takes a matrix as input and displays its transpose:
c code
#include <stdio.h>
printf("\n");
int main() {
int matrix[MAX_SIZE][MAX_SIZE], transposed[MAX_SIZE][MAX_SIZE];
scanf("%d", &rows);
scanf("%d", &cols);
scanf("%d", &matrix[i][j]);
printf("\nOriginal Matrix:\n");
printf("\nTransposed Matrix:\n");
return 0;
}
How the Program Works:
1. Input Matrix: The program prompts the user for the number of rows and columns,
followed by the elements of the matrix.
3. Display: Both the original matrix and its transpose are printed.
Example Usage:
Q2. Write a program in `C’ Language to accept 10 strings as input and print them
inlexicographic order (10 Marks)
Ans
Here’s a C program that accepts 10 strings as input and prints them in lexicographic order:
c code
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 10
char temp[MAX_LENGTH];
for (int i = 0; i < count - 1; i++) {
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]);
strcpy(strings[j], temp);
int main() {
char strings[MAX_STRINGS][MAX_LENGTH];
printf("Enter 10 strings:\n");
sortStrings(strings, MAX_STRINGS);
return 0;
}
1. Input Strings: The program prompts the user to enter 10 strings. It uses fgets to read
each string and removes the newline character.
2. Sorting: The sortStrings function implements a bubble sort algorithm to sort the
strings in lexicographic order using the strcmp function for comparison.
Example Usage:
Q3. Write a program to implement singly linked list for user inputs and perform the
following operations on it: (i) Reverse the linked list and display it. (ii) Sort the nodes in
ascending order and display them
Ans
Here's a C program that implements a singly linked list, allows the user to input values, and
performs the following operations: reversing the linked list and sorting the nodes in
ascending order.
c code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->next = NULL;
return newNode;
*head = newNode;
} else {
temp->next = newNode;
// Swap data
temp = current->data;
current->data = next->data;
next->data = temp;
temp = temp->next;
printf("NULL\n");
}
// Main function
int main() {
int n, data;
scanf("%d", &data);
insertNode(&head, data);
displayList(head);
displayList(head);
sortList(&head);
displayList(head);
return 0;
1. Node Structure: A structure Node is defined to represent each node in the linked list,
containing an integer data and a pointer to the next node.
2. Insertion: The insertNode function allows users to add nodes to the end of the linked
list.
3. Reversing the List: The reverseList function reverses the linked list by adjusting the
next pointers of each node.
4. Sorting the List: The sortList function sorts the linked list in ascending order using a
bubble sort algorithm.
5. Display: The displayList function prints the elements of the linked list in order.
Example Usage:
Q4. Write a program using linked list that accepts two polynomials as input and
displays the resultant polynomial after performing the multiplication of the input
polynomials.
Ans
c code
#include <stdio.h>
#include <stdlib.h>
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
if (*poly == NULL) {
*poly = newNode;
} else {
temp = temp->next;
temp->next = newNode;
}
}
// Function to multiply two polynomials
// For each term in the first polynomial, traverse through the second polynomial
return result;
if (ptr1->exp == ptr2->next->exp) {
ptr1->coeff += ptr2->next->coeff;
struct Node* temp = ptr2->next;
ptr2->next = ptr2->next->next;
free(temp);
} else {
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
if (temp->next != NULL) {
printf(" + ");
temp = temp->next;
printf("\n");
}
// Main function
int main() {
insertTerm(&poly1, 6, 0);
insertTerm(&poly2, 4, 0);
printPolynomial(result);
return 0;
}
Explanation:
1. Node Structure:
o Each node of the linked list represents a term of the polynomial with a
coefficient (coeff) and an exponent (exp).
2. Insertion Function:
o insertTerm: This function inserts a term at the end of the polynomial linked
list.
3. Multiplication Function:
5. Print Function:
6. Main Function:
Example:
For the polynomials:
This C program allows you to multiply two polynomials using linked lists effectively.
Q5. Write a program to implement doubly linked list of integers as user inputs and
perform the following operations:
(i) Calculate and display the sum of all the even integers of the doubly linked list
(ii) Insert new elements at the beginning, in the middle and at the end of the linked
list
Ans
C code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
};
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
*head = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (position == 1) {
insertAtBeginning(head, data);
return;
temp = temp->next;
insertAtEnd(head, data);
} else {
newNode->next = temp->next;
temp->next->prev = newNode;
newNode->prev = temp;
temp->next = newNode;
// Function to calculate and display the sum of all even integers in the doubly linked list
int sum = 0;
if (temp->data % 2 == 0) {
sum += temp->data;
temp = temp->next;
temp = temp->next;
}
printf("\n");
int main() {
while (1) {
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
scanf("%d", &position);
break;
case 3:
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 4:
sumOfEvenIntegers(head);
break;
case 5:
displayList(head);
break;
case 6:
exit(0);
default:
}
return 0;
Explanation:
1. Node Structure:
o The Node structure defines a node in the doubly linked list with three
members: data, prev (pointer to the previous node), and next (pointer to
the next node).
2. Insertion Functions:
4. Display Function:
o displayList: Traverses and prints all the elements of the doubly linked list.
5. Main Function:
How to Use:
• Insertions: The user can insert elements at the beginning, in the middle at a
specific position, or at the end of the list.
• Sum Calculation: The user can calculate and display the sum of all even
integers in the list.
• Display List: The user can display the current state of the list.
Ans
Copy code
#include <stdio.h>
int temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
printf("\n");
// Main function
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
printArray(arr, n);
return 0;
Explanation:
o The bubbleSort function takes an array arr, its size n, and pointers to
swaps and comparisons as parameters.
o The inner loop compares adjacent elements and swaps them if they are in
the wrong order.
3. Main Function:
o The program prompts the user to input the number of elements and the
elements themselves.
o After sorting, the program prints the sorted array, the number of swaps,
and the number of comparisons.
Example Run:
Enter 5 elements: 64 34 25 12 22
Sorted array: 12 22 25 34 64
• User Input: The program takes an array of integers as input from the user.
• Output: The program prints the sorted array, the total number of swaps
performed, and the total number of comparisons made during the sorting
process.
This program provides a simple yet effective demonstration of the Bubble Sort
algorithm, along with tracking important metrics like the number of swaps and
comparisons.
Ans
c code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
struct Stack {
int top;
char items[MAX];
};
s->top = -1;
if (isFull(s)) {
printf("Stack Overflow\n");
} else {
s->items[++(s->top)] = value;
}
// Function to pop an element from the stack
if (isEmpty(s)) {
printf("Stack Underflow\n");
return '\0';
} else {
return s->items[(s->top)--];
if (isEmpty(s)) {
return '\0';
} else {
return s->items[s->top];
int isOperator(char c) {
int precedence(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return 0;
exp[length - i - 1] = temp;
struct Stack s;
initStack(&s);
int i, j = 0;
char c;
for (i = 0; infix[i] != '\0'; i++) {
c = infix[i];
if (isalnum(c)) {
} else if (c == '(') {
} else if (c == ')') {
} else if (isOperator(c)) {
while (!isEmpty(&s)) {
reverse(infix);
if (infix[i] == '(') {
infix[i] = ')';
infix[i] = '(';
char postfix[MAX];
infixToPostfix(infix, postfix);
reverse(postfix);
strcpy(prefix, postfix);
// Main function
int main() {
gets(infix);
infixToPrefix(infix, prefix);
return 0;
Explanation:
1. Stack Structure:
o The Stack structure and its associated functions manage the stack
operations required for the conversion process.
2. Reverse Function:
5. Main Function:
o The user inputs the infix expression, and the program converts it to a
prefix expression, which is then printed.
Example Run:
mathematica
Copy code
Steps to Convert:
2. Infix to Postfix: Convert this reversed infix expression to postfix, which yields
LKA/-CB/-A*.
3. Reverse Postfix: Reverse the postfix expression to get the final prefix expression:
*-A/BC-/AKL.
This C program effectively demonstrates the conversion of infix to prefix using stacks
and string manipulation techniques.
Q8. Write a program in ‘C’ language for the creation of a Red Black tree.
Also,implement insertion and deletion operations.
Ans
c code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
// Function to create a new node
node->data = data;
node->color = RED;
return node;
x->right = y->left;
if (y->left != NULL)
y->left->parent = x;
y->parent = x->parent;
if (x->parent == NULL)
*root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
y->left = x->right;
if (x->right != NULL)
x->right->parent = y;
x->parent = y->parent;
if (y->parent == NULL)
*root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
x->right = y;
y->parent = x;
if (z->parent == z->parent->parent->left) {
struct Node *y = z->parent->parent->right;
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
leftRotate(root, z);
z->parent->color = BLACK;
z->parent->parent->color = RED;
rightRotate(root, z->parent->parent);
} else {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
rightRotate(root, z);
}
z->parent->color = BLACK;
z->parent->parent->color = RED;
leftRotate(root, z->parent->parent);
(*root)->color = BLACK;
while (x != NULL) {
y = x;
x = x->left;
else
x = x->right;
z->parent = y;
if (y == NULL)
*root = z;
else if (z->data < y->data)
y->left = z;
else
y->right = z;
fixInsert(root, z);
node = node->left;
return node;
if (x == x->parent->left) {
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
leftRotate(root, x->parent);
w = x->parent->right;
}
if (w->left->color == BLACK && w->right->color == BLACK) {
w->color = RED;
x = x->parent;
} else {
if (w->right->color == BLACK) {
w->left->color = BLACK;
w->color = RED;
rightRotate(root, w);
w = x->parent->right;
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
leftRotate(root, x->parent);
x = *root;
} else {
if (w->color == RED) {
w->color = BLACK;
x->parent->color = RED;
rightRotate(root, x->parent);
w = x->parent->left;
x = x->parent;
} else {
if (w->left->color == BLACK) {
w->right->color = BLACK;
w->color = RED;
leftRotate(root, w);
w = x->parent->left;
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
rightRotate(root, x->parent);
x = *root;
x->color = BLACK;
void replaceNode(struct Node **root, struct Node *u, struct Node *v) {
if (u->parent == NULL)
*root = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
if (v != NULL)
v->parent = u->parent;
struct Node *y = z;
if (z->left == NULL) {
x = z->right;
replaceNode(root, z, z->right);
x = z->left;
replaceNode(root, z, z->left);
} else {
y = minimum(z->right);
y_original_color = y->color;
x = y->right;
if (y->parent == z)
x->parent = y;
else {
replaceNode(root, y, y->right);
y->right = z->right;
y->right->parent = y;
replaceNode(root, z, y);
y->left = z->left;
y->left->parent = y;
y->color = z->color;
if (y_original_color == BLACK)
fixDelete(root, x);
return root;
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
// Main function
int main() {
while (1) {
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
insert(&root, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
if (node != NULL)
deleteNode(&root, node);
else
break;
case 3:
scanf("%d", &data);
printf("Data found!\n");
else
break;
case 4:
inOrder(root);
printf("\n");
break;
case 5:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
return 0;
Explanation:
1. Node Structure:
o Each node contains a data field, color (RED/BLACK), and pointers to its
left child, right child, and parent.
2. Rotation Functions:
3. Insertion:
o The insert function adds a node and then calls fixInsert to ensure the Red-
Black Tree properties are maintained.
4. Deletion:
5. Search Function:
6. In-Order Traversal:
7. Main Menu:
Example Run:
1. Insert
2. Delete
3. Search
4. Display (In-order)
5. Exit
1. Insert
2. Delete
3. Search
4. Display (In-order)
5. Exit
1. Insert
2. Delete
3. Search
4. Display (In-order)
5. Exit
In-order display: 10 20
This program demonstrates the creation, insertion, deletion, and traversal operations
of a Red-Black Tree. The key balancing mechanisms (rotations and color adjustments)
ensure that the tree remains balanced during insertions and deletions.