0% found this document useful (0 votes)
22 views

week 1Solution

The document outlines several programming assignments involving singly linked lists, including tasks such as inserting elements at the beginning or end, finding the middle element, inserting characters at specific positions, deleting nodes, and managing student GPAs. Each problem includes input and output formats, constraints, and sample test cases to guide implementation. The document also provides code snippets for each task to assist students in completing their assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

week 1Solution

The document outlines several programming assignments involving singly linked lists, including tasks such as inserting elements at the beginning or end, finding the middle element, inserting characters at specific positions, deleting nodes, and managing student GPAs. Each problem includes input and output formats, constraints, and sample test cases to guide implementation. The document also provides code snippets for each task to assist students in completing their assignments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Solution:

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.

The second line consists of N space-separated integers.

Output format :

The output prints the singly linked list elements, after inserting them at the beginning.

Refer to the sample output for formatting specifications.

Code constraints :

1 ≤ N ≤ 10

1 ≤ elements ≤ 100

Sample test cases :

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){

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

void printList(struct Node* node){

while (node != NULL) {

printf("%d ", node->data);

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.

Refer to the sample output for formatting specifications.

Code constraints :
1 ≤ N ≤ 15

1 ≤ roll numbers ≤ 1000

Sample test cases :

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

struct Node* insertAtEnd(struct Node* head, int rollNumber) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->rollNumber = rollNumber;

newNode->next = NULL;

if (head == NULL) {

return newNode;

struct Node* current = head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;
return head;

void display(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%d ", current->rollNumber);

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:

1. If it's an odd-length linked list, return the middle element.

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.

Refer to the sample output for formatting specifications.

Code constraints :
The given test cases fall under the following specifications:

1 ≤ n ≤ 30

1 ≤ elements ≤ 100

Sample test cases :

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

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

struct Node* push(struct Node* head, int new_data) {

struct Node* new_node = createNode(new_data);

new_node->next = head;

head = new_node;

return head;
}

int printMiddle(struct Node* head) {

if (head == NULL) {

return -1;

struct Node* slow_ptr = head;

struct Node* fast_ptr = head;

while (fast_ptr != NULL && fast_ptr->next != NULL) {

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):

1. The first line of output prints "Invalid index".

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.

Refer to the sample output for formatting specifications.

Code constraints :

The initial linked list can contain up to 50 characters.

The index is a non-negative integer.

The character value can be any printable ASCII character.

Sample test cases :

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;

struct Node* next;

};

struct Node* createNode(char value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

return newNode;

struct Node* insertAfterIndex(struct Node* head, int index, char value) {

struct Node* newNode = createNode(value);

if (index == -1) {

newNode->next = head;

return newNode;

}
struct Node* current = head;

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

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;

void displayList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%c ", current->data);

current = current->next;

printf("\n");

int main() {

struct Node* head = NULL;

int n;
char value;

scanf("%d", &n);

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

scanf(" %c", &value);

struct Node* newNode = createNode(value);

if (head == NULL) {

head = newNode;

} else {

struct Node* current = head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

int index;

scanf("%d", &index);

scanf(" %c", &value);

head = insertAfterIndex(head, index, value);

printf("Updated list: ");

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 second line consists of N space-separated elements of 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."

Refer to the sample output for formatting specifications.

Code constraints :

The given test cases fall under the following constraints:

1 ≤ N ≤ 15

1 ≤ elements ≤ 100

1 ≤ x ≤ 15

Sample test cases :

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 :

Invalid position. Deletion not possible.

void deleteNode(int pos) {

if (pos <= 0) {

printf("Invalid position. Deletion not possible.");

return;

struct node* temp = head;

struct node* prev = NULL;

int i;

for (i = 1; i < pos && temp != NULL; i++) {

prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Invalid position. Deletion not possible.");

return;

if (prev == NULL) {

head = head->next;
free(temp);

} else {

prev->next = temp->next;

free(temp);

display_List();

return;

void insert(int value) {

struct node* newnode;

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

newnode->data = value;

newnode->next = NULL;

if (head == NULL) {

head = newnode;

tail = newnode;

} else {

tail->next = newnode;

tail = newnode;

return;

void display_List() {

struct node* temp;

temp = head;

while (temp != NULL) {

if (temp->next == NULL) {
printf("%d ", temp->data);

} else {

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

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.

Refer to the sample output for formatting specifications.

Code constraints :

The given test cases fall under the following constraints:

1 ≤ n ≤ 10

1.0 ≤ GPA ≤ 5.5

position < n

Sample test cases :

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* next;

};
struct Node* createNode(float gpa) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->gpa = gpa;

newNode->next = NULL;

return newNode;

struct Node* deleteStudentAtPosition(struct Node* head, int position) {

if (head == NULL) {

return NULL;

if (position == 1) {

struct Node* temp = head;

head = head->next;

free(temp);

} else {

struct Node* prev = NULL;

struct Node* current = head;

int currentPosition = 1;

while (currentPosition < position && current != NULL) {

prev = current;

current = current->next;

currentPosition++;

if (current != NULL) {

prev->next = current->next;

free(current);

}
}

return head;

void printStudentList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("GPA: %.1f\n", current->gpa);

current = current->next;

int main() {

struct Node* head = NULL;

int n;

scanf("%d", &n);

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

float gpa;

scanf("%f", &gpa);

if (head == NULL) {

head = createNode(gpa);

} else {

struct Node* newNode = createNode(gpa);

newNode->next = head;

head = newNode;

}
int position;

scanf("%d", &position);

head = deleteStudentAtPosition(head, 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 :

The output prints the sum of the coefficients of the polynomials.

Code constraints :

1 ≤ n, m ≤ 100

Sample test cases :


Input 1 :

22

31

40

22

31

40

Output 1 :

18

Input 2 :

10 5

-10 5

Output 2 :

Fill your code here

C (17)

// You are using GCC

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int coefficient;
int exponent;

struct Node* next;

} Node;

Node* createNode(int coef, int exp) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->coefficient = coef;

newNode->exponent = exp;

newNode->next = NULL;

return newNode;

void insertTerm(Node** head, int coef, int exp) {

Node* newNode = createNode(coef, exp);

if (*head == NULL) {

*head = newNode;

} else {

Node* current = *head;

while (current->next != NULL) {

current = current->next;

current->next = newNode;

Node* addPolynomials(Node* poly1, Node* poly2) {

Node* result = NULL;

Node* current = NULL;

while (poly1 != NULL && poly2 != NULL) {

if (poly1->exponent == poly2->exponent) {
int sumCoefficients = poly1->coefficient + poly2->coefficient;

if (sumCoefficients != 0) {

insertTerm(&result, sumCoefficients, poly1->exponent);

if (current == NULL) {

current = result;

} else {

current = current->next;

poly1 = poly1->next;

poly2 = poly2->next;

} else if (poly1->exponent > poly2->exponent) {

insertTerm(&result, poly1->coefficient, poly1->exponent);

if (current == NULL) {

current = result;

} else {

current = current->next;

poly1 = poly1->next;

} else {

insertTerm(&result, poly2->coefficient, poly2->exponent);

if (current == NULL) {

current = result;

} else {

current = current->next;

poly2 = poly2->next;

while (poly1 != NULL) {


insertTerm(&result, poly1->coefficient, poly1->exponent);

if (current == NULL) {

current = result;

} else {

current = current->next;

poly1 = poly1->next;

while (poly2 != NULL) {

insertTerm(&result, poly2->coefficient, poly2->exponent);

if (current == NULL) {

current = result;

} else {

current = current->next;

poly2 = poly2->next;

return result;

void printPolynomial(Node* poly) {

if (poly == NULL) {

printf("0\n");

return;

while (poly != NULL) {

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);

if (poly->next != NULL && poly->next->coefficient > 0) {

printf(" + ");

poly = poly->next;

printf("\n");

void freeLinkedList(Node* head) {

Node* current = head;

while (current != NULL) {

Node* temp = current;

current = current->next;

free(temp);

int main() {

Node* poly1 = NULL;

Node* poly2 = NULL;

int num_terms_poly1, num_terms_poly2;


scanf("%d", &num_terms_poly1);

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

int coef, exp;

scanf("%d%d", &coef, &exp);

insertTerm(&poly1, coef, exp);

scanf("%d", &num_terms_poly2);

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

int coef, exp;

scanf("%d%d", &coef, &exp);

insertTerm(&poly2, coef, exp);

Node* result = addPolynomials(poly1, poly2);

int sum = 0;

Node* current = result;

while (current != NULL) {

sum += current->coefficient;

current = current->next;

printf("%d", sum);

freeLinkedList(poly1);

freeLinkedList(poly2);

freeLinkedList(result);
return 0;

You might also like