Professional Documents
Culture Documents
Suraj Dsa
Suraj Dsa
Suraj Dsa
Q1. Write a C program to create a single linked list, then write another function to swap
nodes pairwise.
Input: 11->12->13->14->15->16->NULL
Output : 12->11->14->13->16->15->NULL
Source-Code
#include <stdio.h>
#include <stdlib.h>
}
printf("NULL\n");
}
// Swap the first two nodes to make the second node the new head
*head = curr;
while (1)
{
Node *next = curr->next;
curr->next = prev;
prev->next = next->next;
prev = next;
curr = prev->next;
}
}
while (1)
{
printf("\nMenu:\n");
printf("1. Insert node at end\n");
printf("2. Display list\n");
printf("3. Swap nodes pairwise\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
{
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 2:
printf("Linked list: ");
displayList(head);
break;
case 3:
swapNodesPairwise(&head);
printf("Nodes swapped pairwise.\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q2. Write a C program to create a single linked list, then write another function InsertNth()
which can insert a new node after any node given by the user in that linked list.
Source-Code
#include <stdio.h>
#include <stdlib.h>
printf("List is empty\n");
return;
}
struct node *temp = head;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
while (1)
{
printf("\nMenu:\n");
printf("1. Insert node at end\n");
printf("2. Display list\n");
printf("3. Insert node after specified node\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
{
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 2:
printf("Linked list: ");
displayList(head);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter the node value after which to insert: ");
scanf("%d", &n);
InsertNth(&head, data, n);
break;
case 4:
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q3. Write a C program to create a double linked list, write another C function which takes
the address of the
linked list and two integers M and N.
Traverse the linked list such that you retain M nodes then delete next N nodes of the
linked list.
Input:
M = 2, N = 2
Linked List: 11<-->12<->13<->14<->15<->16<->17<->18
Output:
Linked List: 11->12->15->16<->17<->18
Source-Code
#include <stdio.h>
#include <stdlib.h>
free(temp);
temp = next;
}
int main()
{
struct node *head = NULL;
int choice, data, M, N;
while (1)
{
printf("\nMenu:\n");
printf("1. Insert node at end\n");
printf("2. Display list\n");
printf("3. Retain M nodes and delete N nodes\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertEnd(&head, data);
break;
case 2:
printf("Linked list: ");
displayList(head);
break;
case 3:
printf("Enter the value of M: ");
scanf("%d", &M);
printf("Enter the value of N: ");
scanf("%d", &N);
retainAndDelete(&head, M, N);
break;
case 4:
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q4. Write a C program to create a linked list P, then write a ‘C’ function named split
to create two linked lists Q & R from P So that Q contains all elements in odd
positions of P and R contains the remaining elements. Finally print both linked lists i.e.
Q and R.
Source-Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct node *P = NULL;
struct node *Q = NULL;
struct node *R = NULL;
int choice, data;
while (1)
{
printf("\nMenu:\n");
printf("1. Insert node at end\n");
printf("2. Display list P\n");
printf("3. Split list into Q and R\n");
printf("4. Display lists Q and R\n");
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertEnd(&P, data);
break;
case 2:
printf("Linked list P: ");
displayList(P);
break;
case 3:
split(P, &Q, &R);
printf("List P splited into Q and R.\n");
break;
case 4:
printf("Linked list Q (odd positions): ");
displayList(Q);
printf("Linked list R (even positions): ");
displayList(R);
break;
case 5:
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q5. W.A.P. to create a binary search tree and perform following operations:
1) Count nodes having left child only in the binary search tree
2) Print node having smallest information in the binary search tree
3) Count total number of leaf nodes
4) Count nodes having both children in the binary search tree
5) Count total numbers of nodes from right hand side of root node
Source-Code
#include <stdio.h>
#include <stdlib.h>
// Function to count total number of nodes from the right-hand side of the root node
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
int main()
{
struct Node *root = NULL;
int choice, value;
while (1)
{
printf("\n1. Insert\n2. Count nodes with left child only\n3. Print node with smallest
value\n4. Count leaf nodes\n5. Count nodes with both children\n6. Count nodes from right-
hand side of root\n7. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insertNode(root, value);
break;
case 2:
printf("Nodes with left child only: %d\n", countLeftChildOnly(root));
break;
case 3:
{ struct Node *minNode = findMinNode(root);
if (minNode != NULL)
{ printf("Node with smallest value: %d\n", minNode->data);
}
else
{ printf("Tree is empty.\n");}}
break;
case 4: printf("Total number of leaf nodes: %d\n", countLeafNodes(root));
break;
case 5: printf("Nodes with both children: %d\n", countBothChildren(root));
break;
case 6: printf("Nodes from right-hand side of root: %d\n", countRightNodes(root-
>right));
break; case 7:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q6. Write a program to add of two polynomials of degree n, using linked list For example
p1=anxn+an-1xn-1 + an-2xn-2 + a0x0
P2=bnxn+bn-1xn-1 + bn-2xn-2 +........b0x0
p1 = first polynomial
p2 = second polynomial
Source-Code
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node into the linked list in decreasing order of exponents
struct Node *insertNode(struct Node *head, int coefficient, int exponent)
{
struct Node *newNode = createNode(coefficient, exponent);
if (head == NULL || head->exponent < exponent)
{
newNode->next = head;
return newNode;
}
}
newNode->next = current->next;
current->next = newNode;
return head;
}
return result;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
int main()
{
struct Node *p1 = NULL;
struct Node *p2 = NULL;
struct Node *p3 = NULL;
int choice, degree, coefficient;
while (1)
{
printf("\nMenu:\n");
printf("1. Enter first polynomial\n");
printf("2. Enter second polynomial\n");
printf("3. Add polynomials\n");
printf("4. Display polynomials\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
p1 = NULL;
printf("Enter the degree of the first polynomial: ");
scanf("%d", °ree);
printf("Enter the coefficients of the first polynomial (highest to lowest degree):\n");
for (int i = degree; i >= 0; i--)
{
printf("Coefficient of x^%d: ", i);
scanf("%d", &coefficient);
p1 = insertNode(p1, coefficient, i);
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
}
break;
case 2:
p2 = NULL;
printf("Enter the degree of the second polynomial: ");
scanf("%d", °ree);
printf("Enter the coefficients of the second polynomial (highest to lowest degree):\n");
for (int i = degree; i >= 0; i--)
{
printf("Coefficient of x^%d: ", i);
scanf("%d", &coefficient);
p2 = insertNode(p2, coefficient, i);
}
break;
case 3:
p3 = addPolynomials(p1, p2);
printf("Polynomials added.\n");
break;
case 4:
printf("First polynomial: ");
printPolynomial(p1);
printf("Second polynomial: ");
printPolynomial(p2);
if (p3 != NULL)
{
printf("Resultant polynomial (p1 + p2): ");
printPolynomial(p3);
}
else
{
printf("Resultant polynomial not yet computed.\n");
}
break;
case 5:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q7. Write a C program to sort a sequence of characters given by user in an array, using
Quick sort technique.
Source-Code
#include <stdio.h>
#include <stdlib.h>
int main() {
char* arr = NULL;
int n = 0;
int choice;
do {
printf("\nMenu:\n");
printf("1. Enter characters\n");
printf("2. Sort characters using Quick Sort\n");
printf("3. Display characters\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of characters: ");
scanf("%d", &n);
arr = (char*)malloc(n * sizeof(char));
printf("Enter the characters: ");
for (int i = 0; i < n; i++) {
scanf(" %c", &arr[i]);
}
break;
case 2:
if (arr != NULL) {
quickSort(arr, 0, n - 1);
printf("Characters sorted successfully.\n");
} else {
printf("No characters to sort. Please enter characters first.\n");
}
break;
case 3:
if (arr != NULL) {
printf("Characters: ");
printArray(arr, n);
} else {
printf("No characters to display. Please enter characters first.\n");}
break;
case 4:
free(arr);
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
} } while (choice != 4);
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Q8. Using circular linked list allocate time slots of 10ms for given processes in
time sharing Environment and then print, which process will be completed
in how much time.
Source-Code
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
*head = ptr;
(*head)->next = ptr;
} else {
Process *temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = ptr;
ptr->next = *head;
}
}
do {
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
flag = 0;
do {
if (current->remaining_time > 0) {
if (current->remaining_time > TIME_SLOT) {
current->remaining_time -= TIME_SLOT;
total_time += TIME_SLOT;
} else {
total_time += current->remaining_time;
current->remaining_time = 0;
current->completion_time = total_time;
}
}
current = current->next;
} while (current != head);
tmp = head;
while (tmp != NULL) {
if (tmp->remaining_time > 0) {
flag = 1;
break;
}
tmp = tmp->next;
if (tmp == head) break;
}
} while (flag);
int main() {
Process *head = NULL;
int time_slot, choice, id = 1;
printf("Menu:\n");
printf("1. Allocate time\n");
printf("2. Perform multiprogramming\n");
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
do {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
Allocate(&head, id);
id++;
break;
case 2:
if (head == NULL) {
printf("\nNo processes are running\n");
} else {
printf("Enter time slot: ");
scanf("%d", &time_slot);
MultiProgramming(head, time_slot);
}
break;
case 3:
Display(head);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Source-Code
#include <stdio.h>
#include <stdlib.h>
struct Edge {
int src, dest, weight;
};
// If including this edge does not cause a cycle, include it in the result and increment the
index of the result for the next edge
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
// Else discard the next_edge
}
free(subsets);
}
int main() {
int V, E;
printf("Enter number of vertices: ");
scanf("%d", &V);
printf("Enter number of edges: ");
scanf("%d", &E);
KruskalMST(graph);
free(graph->edges);
free(graph);
return 0;
}
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
Source-Code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};
return graph;
}
graph->adjLists[dest] = newNode;
}
graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);
int main() {
int vertices, choice, src, dest, startVertex;
while (1) {
printf("\nMenu\n");
printf("1. Add edge\n");
printf("2. Perform DFS\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter source and destination vertices: ");
scanf("%d %d", &src, &dest);
addEdge(graph, src, dest);
break;
case 2:
printf("Enter the starting vertex for DFS: ");
scanf("%d", &startVertex);
NAME: - SURAJ SINGH NEGI COURSE/SEM-MCA/2 ROLL NO/SEC- 2301400(63)/B
resetVisited(graph);
printf("Depth First Traversal starting from vertex %d:\n", startVertex);
DFS(graph, startVertex);
break;
case 3:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}