dsa
dsa
CODE SNIPPET
1 (a) Write a program to find the maximum element in each row of the matrix using
pointers.
#include<stdio.h>
for (int i = 0; i < m; i++) { // Iterate through all columns in the row
if (max < *arr) { // If the current element is greater than max
max = *arr; // Update max to the current element
}
arr++; // Move pointer to the next element in the row
}
int main() {
int n, m, mat[10][10]; // Variables for matrix dimensions and the matrix itself
1.(b) Write a program to find the nth term in the Fibonacci series using recursion.
#include<stdio.h>
int main() {
int n; // Variable to store the number of terms
2. Write a program to design, Develop and Implement a menu- driven program in C for
#include<stdio.h>
#include<stdlib.h>
#define Max 5 // Define the maximum size of the stack
int stack[Max]; // Array to represent the stack
int top = -1; // Variable to keep track of the top of the stack
int main() {
int element, ch; // Variables for the element and user's choice
switch (ch) {
case 1: // Push operation
printf("Enter the value:\n");
scanf("%d", &element);
push(element);
break;
3. (a) Write a C program to read and display the Time in specified format. Create a
structure called TIME with hour (int), minute(int), second(int) and next (self
Referencing pointer) as its members. Dynamically create two variables of structure
TIME and link the first variable to the second one and display it. Write a Display
function that takes address of first TIME variable and displays both times in the
Format h: m: s
#include <stdio.h>
#include<stdlib.h>
int main() {
struct time *t1, *t2; // Declare pointers for two time nodes
// Allocate memory for the first time node
t1 = malloc(sizeof(struct time));
printf("Enter the hour, minute, and second for time 1\n");
// Input values for the first time node
scanf("%d%d%d", &t1->h, &t1->m, &t1->s);
// Allocate memory for the second time node
t2 = malloc(sizeof(struct time));
printf("Enter the hour, minute, and second for time 2\n");
// Input values for the second time node
scanf("%d%d%d", &t2->h, &t2->m, &t2->s);
// Link the first time node to the second
t1->next = t2;
// Call the display function to print the times
display(t1);
return 0;
}
using stack.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define Max 20
char stack[Max];
int top = -1;
// Push function
void push(char item) {
if (top == Max - 1) {
printf("Stack overflow\n");
return;
}
stack[++top] = item;
}
// Pop function
char pop() {
if (top == -1) {
printf("Stack underflow\n");
return '\0';
}
return stack[top--];
}
int main() {
char input[Max];
printf("Enter a string: ");
scanf("%s", input); // Limit input to prevent overflow
if (ispalindrome(input)) {
printf("The given string is a palindrome.\n");
} else {
printf("The given string is not a palindrome.\n");
}
return 0;
}
4. Write a C program to read and display the student details. Define a structure 'Student' with fields name
(string) , usn (int), marks of 3 subjects (int) and Average (float) in it. Store the details of n students in an array
of structure 'Student'. Display the details of all students in the descending order their total marks.
#include <stdio.h>
// Define the structure for student
struct student {
char name[20]; // Student name
int usn; // University serial number
int m1, m2, m3; // Marks for three subjects
float avg; // Average of marks
};
int main() {
struct student s[20]; // Array to store student details
int n, total = 0; // Number of students and a variable to calculate total
marks
return 0;
}
5. Write a C program using dynamic variables and pointers, to construct singly linked
list. The operations to be supported are:
(i)
Insert at the front of a list.
(ii)
Deleting a node based on specified value.
(iii)
Displaying all the nodes in the list.
#include <stdio.h>
#include <stdlib.h>
// Function to delete a node with a specific key (value) from the linked list
void delete(int key) {
temp = first; // Start with the first node
if (temp == NULL) { // If the linked list is empty
printf("empty ll");
return;
}
// Traverse the linked list to find the node with the given key
while (temp->data != key) {
temp1 = temp; // Keep track of the previous node
temp = temp->next; // Move to the next node
if (temp == NULL) { // If the key is not found
printf("Key not found\n");
return;
}
}
int main() {
int ch, val, key;
switch (ch) {
case 1:
printf("Enter the value to insert:\n");
scanf("%d", &val); // Read the value to be inserted
insert(val); // Call the insert function
break;
case 2:
printf("Enter the value to delete:\n");
scanf("%d", &key); // Read the value to be deleted
delete(key); // Call the delete function
break;
case 3:
display(); // Call the display function
break;
case 4:
exit(0); // Exit the program
default:
printf("Invalid choice\n"); // Handle invalid input
break;
}
}
return 0;
}
6. Write a C program using dynamic variables and pointers, to construct singly linked
list. The operations to be supported are:
(i)
Insert at 3 rd position in the list.
(ii)
Searching a node based on specified value.
(iii)
Displaying all the nodes in the list.
#include <stdio.h>
#include <stdlib.h>
int count = 0; // Counter to track the number of nodes in the linked list
int main() {
int ch, val, key;
switch (ch) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &val); // Read the value to be inserted
insert(val); // Call the insert function
break;
case 2:
printf("Enter the value to find position: ");
scanf("%d", &key); // Read the value to find
position(key); // Call the position function
break;
case 3:
display(); // Call the display function
break;
case 4:
exit(0); // Exit the program
default:
printf("Invalid choice\n"); // Handle invalid input
break;
}
}
return 0;
}
// The previous pointer is now the new head of the reversed list
return prev;
}
// Main function
int main() {
int n, i;
8. Write a C program to support the following operations on doubly linked list where
each node consists of integers.
(i)
Create a doubly linked list by adding each node at the front.
(ii)
Insert a new node to the left of the node whose key value is read as an input.
(iii)
Display all the contents of the list.
#include <stdio.h>
#include <stdlib.h>
int count = 0; // Counter to track the number of nodes in the linked list
// Function to insert a new node to the left of a node with a given key
void insert_at_left(int key) {
int val;
ptr = first; // Start traversal from the first node
int main() {
int ch, val, key;
switch (ch) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &val); // Read the value to be inserted
insert(val); // Call the insert function
break;
case 2:
printf("Enter the key to insert left of: ");
scanf("%d", &key); // Read the key to find
insert_at_left(key); // Call the insert_at_left function
break;
case 3:
display(); // Call the display function
break;
case 4:
exit(0); // Exit the program
default:
printf("Invalid choice\n"); // Handle invalid input
break;
}
}
return 0;
}
10. Write a C program to convert an expression given in “infix” form to “postfix” form
using stack concept.
#include <ctype.h> // For isalnum() function
#include <stdio.h>
push('('); // Push an opening parenthesis onto the stack to indicate the start
while ((ch = infix[i++]) != '\0') { // Traverse the infix expression
if (ch == '(') {
push(ch); // Push an opening parenthesis onto the stack
} else if (isalnum(ch)) {
pofx[k++] = ch; // If the character is an operand, add it to the postfix
expression
} else if (ch == ')') {
// If a closing parenthesis is encountered
while (S[top] != '(') {
pofx[k++] = pop(); // Pop and add operators to the postfix expression
until '(' is found
}
elem = pop(); // Remove the '(' from the stack
} else {
// If the character is an operator
while (pr(S[top]) >= pr(ch)) {
// Pop operators from the stack with precedence >= current operator
pofx[k++] = pop();
}
push(ch); // Push the current operator onto the stack
}
}
// Pop any remaining operators from the stack and add to postfix expression
while (S[top] != '(') {
pofx[k++] = pop();
}
pofx[k] = '\0'; // Null-terminate the postfix expression
11. Write a C program to simulate the working of a queues using an array provide and
implement the following operations:
i) Insert
ii) Delete
iii) Display
Assume that the size of the queue is 5.
#include<stdio.h>
#include<stdlib.h>
// Function prototypes
void insert(int); // Function to insert an element into the queue
void delete(); // Function to delete an element from the queue
void display(); // Function to display the elements of the queue
int main()
{
int choice, num;
while(1) // Infinite loop to display menu and take user input
{
printf(" \n1.INSERT\n2.DELETE \n3.DISPLAY \n4.EXIT"); // Display menu options
printf("\nEnter the Choice:");
scanf("%d", &choice); // Take user input for the choice
switch(choice)
{
case 1:
printf("Enter value to insert: ");
scanf("%d", &num); // Take input value for insertion
insert(num); // Call the insert function
break;
case 2:
delete(); // Call the delete function
break;
case 3:
display(); // Call the display function
break;
case 4:
printf("\n EXIT POINT\n"); // Exit message
exit(0); // Exit the program
default:
printf ("\nEnter Valid Choice\n"); // Handle invalid choice
}
}
return 0;
}
// Function declarations
void insert_rear(int); // Insert a node at the rear of the queue
void delete_front(); // Delete a node from the front of the queue
void display(); // Display all nodes in the queue
void main()
{
int num, ch;
while(1) // Infinite loop for user to choose operations
{
printf("\nEnter your choice\n");
printf("1. Insert at rear\n");
printf("2. Delete at front\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &ch); // Take user input for menu choice
case 2:
delete_front(); // Call function to delete the front node
break;
case 3:
display(); // Call function to display the queue
break;
case 4:
exit(0); // Exit the program
}
}
}
// Case when the queue is empty (both front and rear are NULL)
if ((front == NULL) && (rear == NULL))
{
front = rear = new_node; // Set both front and rear to the new node
front->next = front; // Circular link: the node points to itself
}
else
{
// Case when the queue already has nodes
rear->next = new_node; // Link the current rear node to the new node
new_node->next = front; // Circular link: new node points to the front
rear = new_node; // Move rear pointer to the newly added node
}
return;
}
// Case when the queue is empty (both front and rear are NULL)
if ((front == NULL) && (rear == NULL))
{
printf("Queue Empty\n"); // Print message and return if the queue is empty
}
// Case when there is only one node in the queue (front points to rear)
else if (front->next == front)
{
printf("Item deleted: %d\n", front->num); // Print the value of the node
being deleted
free(front); // Free the memory occupied by the front node
front = rear = NULL; // Set both front and rear to NULL (queue is empty)
}
else
{
// Case when there are multiple nodes in the queue
ptr = front; // Save the front node to a temporary pointer
front = front->next; // Move the front pointer to the next node
rear->next = front; // Update the rear's next pointer to the new front
(maintain circular link)
printf("Item deleted: %d\n", ptr->num); // Print the value of the node being
deleted
free(ptr); // Free the memory occupied by the old front node
}
return;
}
9. Write a C program to support the following operations on doubly linked list where
each node consists of integers.
(i)
Delete the node of a given data, if it is found, otherwise display appropriate
message.
(ii)
Searching a node based on specified value.
(iii)
Display all the contents of the list.
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node in the doubly linked list
struct node {
int data; // Data part of the node
struct node *next; // Pointer to the next node
struct node *prev; // Pointer to the previous node
} *first = NULL, *last = NULL, *temp = NULL, *temp1, *nn, *ptr;
while (1) {
// Display menu options
printf("\n1. Insert");
printf("\n2. Find Position");
printf("\n3. Delete");
printf("\n4. Display");
printf("\n5. Exit\n");
switch (ch) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &val); // Read the value to be inserted
insert(val); // Call the insert function
break;
case 2:
printf("Enter the value to find position: ");
scanf("%d", &key); // Read the value to find
position(key); // Call the position function
break;
case 3:
printf("Enter the value to delete: ");
scanf("%d", &elem); // Read the value to delete
delete(elem); // Call the delete function
break;
case 4:
display(); // Call the display function
break;
case 5:
exit(0); // Exit the program
default:
printf("Invalid choice\n"); // Handle invalid input
break;
}
}
return 0;
}
13.Write a C program to construct a binary search tree of integers and also display the elements in the tree using
Inorder, Preorder and Postorder traversals.
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>