week 1Solution
week 1Solution
Problem Statement
As part of a programming assignment in a data structures course, students are required to create a
program to construct a singly linked list by inserting elements at the beginning.
You are an evaluator of the course and guide the students to complete the task.
Input format :
The first line of input consists of an integer N, which is the number of elements.
Output format :
The output prints the singly linked list elements, after inserting them at the beginning.
Code constraints :
1 ≤ N ≤ 10
1 ≤ elements ≤ 100
Input 1 :
78 89 34 51 67
Output 1 :
67 51 34 89 78
Input 2 :
10 5 20 35 30 15 18
Output 2 :
18 15 30 35 20 5 10
void insertAtFront(struct Node** head_ref, int new_data){
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
node = node->next;
printf("\n");
2. Problem Statement
John is tasked with creating a program to manage student roll numbers using a singly linked list.
Write a program for John that accepts students' roll numbers, inserts them at the end of the linked
list, and displays the numbers.
Input format :
The first line of input consists of an integer N, representing the number of students.
The second line consists of N space-separated integers, representing the roll numbers of students.
Output format :
The output prints the space-separated integers singly linked list, after inserting the roll numbers of
students at the end.
Code constraints :
1 ≤ N ≤ 15
Input 1 :
23 85 47 62 31
Output 1 :
23 85 47 62 31
Input 2 :
14 52 36 98 46 82 78 94
Output 2 :
14 52 36 98 46 82 78 94
newNode->rollNumber = rollNumber;
newNode->next = NULL;
if (head == NULL) {
return newNode;
current = current->next;
current->next = newNode;
return head;
current = current->next;
3.
Problem Statement
Dev is tasked with creating a program that efficiently finds the middle element of a linked list. The
program should take user input to populate the linked list by inserting each element into the front of
the list and then determining the middle element.
Assist Dev, as he needs to ensure that the middle element is accurately identified from the
constructed singly linked list:
2. If it's an even-length linked list, return the second middle element of the two elements.
Input format :
The first line of input consists of an integer n, representing the number of elements in the linked list.
The second line consists of n space-separated integers, representing the elements of the list.
Output format :
The first line of output displays the linked list after inserting elements at the front.
The second line displays "Middle Element: " followed by the middle element of the linked list.
Code constraints :
The given test cases fall under the following specifications:
1 ≤ n ≤ 30
1 ≤ elements ≤ 100
Input 1 :
10 20 30 40 50
Output 1 :
50 40 30 20 10
Middle Element: 30
Input 2 :
10 20 30 40
Output 2 :
40 30 20 10
Middle Element: 20
newNode->data = data;
newNode->next = NULL;
return newNode;
new_node->next = head;
head = new_node;
return head;
}
if (head == NULL) {
return -1;
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next->next;
return slow_ptr->data;
Problem Statement
Imagine you are working on a text processing tool and need to implement a feature that allows users
to insert characters at a specific position.
Implement a program that takes user inputs to create a singly linked list of characters and inserts a
new character after a given index in the list.
Input format :
The first line of input consists of an integer N, representing the number of characters in the linked
list.
The second line consists of a sequence of N characters, representing the linked list.
The third line consists of an integer index, representing the index(0-based) after which the new
character node needs to be inserted.
The fourth line consists of a character value representing the character to be inserted after the given
index.
Output format :
If the provided index is out of bounds (larger than the list size):
2. The second line prints "Updated list: " followed by the unchanged linked list values.
Otherwise, the output prints "Updated list: " followed by the updated linked list after inserting the
new character after the given index.
Code constraints :
Input 1 :
abcde
Output 1 :
Updated list: a b c X d e
Input 2 :
xyz
Output 2 :
Updated list: x A y z
Input 3 :
ABCD
Output 3 :
Invalid index
Updated list: A B C D
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
};
newNode->data = value;
newNode->next = NULL;
return newNode;
if (index == -1) {
newNode->next = head;
return newNode;
}
struct Node* current = head;
if (current == NULL) {
printf("Invalid index\n");
return head;
current = current->next;
if (current != NULL) {
newNode->next = current->next;
current->next = newNode;
} else {
printf("Invalid index\n");
return head;
current = current->next;
printf("\n");
int main() {
int n;
char value;
scanf("%d", &n);
if (head == NULL) {
head = newNode;
} else {
current = current->next;
current->next = newNode;
int index;
scanf("%d", &index);
displayList(head);
return 0;
}
Problem Statement
Arun is learning about data structures and algorithms. He needs your help in solving a specific
problem related to a singly linked list.
Your task is to implement a program to delete a node at a given position. If the position is valid, the
program should perform the deletion; otherwise, it should display an appropriate message.
Input format :
The first line of input consists of an integer N, representing the number of elements in the linked list.
The third line consists of an integer x, representing the position to delete. Position starts from 1.
Output format :
The output prints space-separated integers, representing the updated linked list after deleting the
element at the given position.
If the position is not valid, print "Invalid position. Deletion not possible."
Code constraints :
1 ≤ N ≤ 15
1 ≤ elements ≤ 100
1 ≤ x ≤ 15
Input 1 :
82317
Output 1 :
8317
Input 2 :
12 14 15 27 29 37 48 96
8
Output 2 :
12 14 15 27 29 37 48
Input 3 :
1859
Output 3 :
if (pos <= 0) {
return;
int i;
prev = temp;
temp = temp->next;
if (temp == NULL) {
return;
if (prev == NULL) {
head = head->next;
free(temp);
} else {
prev->next = temp->next;
free(temp);
display_List();
return;
newnode->data = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
tail = newnode;
} else {
tail->next = newnode;
tail = newnode;
return;
void display_List() {
temp = head;
if (temp->next == NULL) {
printf("%d ", temp->data);
} else {
temp = temp->next;
return;
Problem Statement
Imagine you are tasked with developing a simple GPA management system using a singly linked list.
The system allows users to input student GPA values, insertion should happen at the front of the
linked list, delete record by position, and display the updated list of student GPAs.
Input format :
The first line of input contains an integer n, representing the number of students.
The next n lines contain a single floating-point value representing the GPA of each student.
The last line contains an integer position, indicating the position at which a student record should be
deleted. Position starts from 1.
Output format :
After deleting the data in the given position, display the output in the format "GPA: " followed by the
GPA value, rounded off to one decimal place.
Code constraints :
1 ≤ n ≤ 10
position < n
Input 1 :
3.8
3.2
3.5
4.1
Output 1 :
GPA: 4.1
GPA: 3.2
GPA: 3.8
Input 2 :
3.7
3.2
3.9
3.5
3.8
3.6
Output 2 :
GPA: 3.6
GPA: 3.8
GPA: 3.9
GPA: 3.2
GPA: 3.7
#include <stdio.h>
#include <stdlib.h>
struct Node {
float gpa;
};
struct Node* createNode(float gpa) {
newNode->gpa = gpa;
newNode->next = NULL;
return newNode;
if (head == NULL) {
return NULL;
if (position == 1) {
head = head->next;
free(temp);
} else {
int currentPosition = 1;
prev = current;
current = current->next;
currentPosition++;
if (current != NULL) {
prev->next = current->next;
free(current);
}
}
return head;
current = current->next;
int main() {
int n;
scanf("%d", &n);
float gpa;
scanf("%f", &gpa);
if (head == NULL) {
head = createNode(gpa);
} else {
newNode->next = head;
head = newNode;
}
int position;
scanf("%d", &position);
printStudentList(head);
return 0;
Problem Statement
Janani is a tech enthusiast who loves working with polynomials. She wants to create a program that
can add polynomial coefficients and provide the sum of their coefficients.
The polynomials will be represented as a linked list, where each node of the linked list contains a
coefficient and an exponent. The polynomial is represented in the standard form with descending
order of exponents.
Input format :
The first line of input consists of an integer n, representing the number of terms in the first
polynomial.
The following n lines of input consist of two integers each: the coefficient and the exponent of the
term in the first polynomial.
The next line of input consists of an integer m, representing the number of terms in the second
polynomial.
The following m lines of input consist of two integers each: the coefficient and the exponent of the
term in the second polynomial.
Output format :
Code constraints :
1 ≤ n, m ≤ 100
22
31
40
22
31
40
Output 1 :
18
Input 2 :
10 5
-10 5
Output 2 :
C (17)
#include <stdio.h>
#include <stdlib.h>
int coefficient;
int exponent;
} Node;
newNode->coefficient = coef;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
if (*head == NULL) {
*head = newNode;
} else {
current = current->next;
current->next = newNode;
if (poly1->exponent == poly2->exponent) {
int sumCoefficients = poly1->coefficient + poly2->coefficient;
if (sumCoefficients != 0) {
if (current == NULL) {
current = result;
} else {
current = current->next;
poly1 = poly1->next;
poly2 = poly2->next;
if (current == NULL) {
current = result;
} else {
current = current->next;
poly1 = poly1->next;
} else {
if (current == NULL) {
current = result;
} else {
current = current->next;
poly2 = poly2->next;
if (current == NULL) {
current = result;
} else {
current = current->next;
poly1 = poly1->next;
if (current == NULL) {
current = result;
} else {
current = current->next;
poly2 = poly2->next;
return result;
if (poly == NULL) {
printf("0\n");
return;
if (poly->coefficient != 0) {
if (poly->exponent > 1) {
printf("%dx^%d", poly->coefficient, poly->exponent);
} else if (poly->exponent == 1) {
printf("%dx", poly->coefficient);
} else {
printf("%d", poly->coefficient);
printf(" + ");
poly = poly->next;
printf("\n");
current = current->next;
free(temp);
int main() {
scanf("%d", &num_terms_poly2);
int sum = 0;
sum += current->coefficient;
current = current->next;
printf("%d", sum);
freeLinkedList(poly1);
freeLinkedList(poly2);
freeLinkedList(result);
return 0;