0% found this document useful (0 votes)
5 views3 pages

112

The first document is a C program that reads a list of integers and determines if Alice can win by summing single or double-digit numbers compared to Bob's sums. The second document is a C program that implements a linked list, including functions to create nodes, print the list, find the middle, reverse the list, and reorder it. Both programs demonstrate fundamental programming concepts in C, such as data structures and control flow.

Uploaded by

Deepak Sathis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

112

The first document is a C program that reads a list of integers and determines if Alice can win by summing single or double-digit numbers compared to Bob's sums. The second document is a C program that implements a linked list, including functions to create nodes, print the list, find the middle, reverse the list, and reorder it. Both programs demonstrate fundamental programming concepts in C, such as data structures and control flow.

Uploaded by

Deepak Sathis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.#include <stdio.

h>

int main() {
int n;
scanf("%d", &n);

int nums[n];
int sum_single = 0, sum_double = 0;
int sum_other_single = 0, sum_other_double = 0;

// Read the numbers


for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}

// Split the numbers into single-digit and double-digit numbers


for (int i = 0; i < n; i++) {
if (nums[i] < 10) {
sum_single += nums[i]; // Alice's sum if she takes single-digit numbers
} else {
sum_double += nums[i]; // Alice's sum if she takes double-digit numbers
}
}

// Calculate Bob's sum for both cases


sum_other_single = 0;
sum_other_double = 0;

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


if (nums[i] < 10) {
sum_other_double += nums[i]; // Bob gets the single-digit numbers
} else {
sum_other_single += nums[i]; // Bob gets the double-digit numbers
}
}

// Check if Alice can win in either case


if (sum_single > sum_other_single || sum_double > sum_other_double) {
printf("true\n");
} else {
printf("false\n");
}

return 0;
}

2.#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the linked list


struct ListNode {
int val;
struct ListNode* next;
};

// Function to create a new node


struct ListNode* createNode(int val) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}

// Function to print the linked list


void printList(struct ListNode* head) {
struct ListNode* temp = head;
while (temp != NULL) {
printf("%d ", temp->val);
temp = temp->next;
}
printf("\n");
}

// Function to find the middle of the linked list


struct ListNode* findMiddle(struct ListNode* head) {
struct ListNode* slow = head;
struct ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}

// Function to reverse the linked list


struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* prev = NULL;
struct ListNode* curr = head;
struct ListNode* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}

// Function to reorder the linked list as required


void reorderList(struct ListNode* head) {
if (head == NULL || head->next == NULL) return;

// Step 1: Find the middle of the linked list


struct ListNode* middle = findMiddle(head);

// Step 2: Reverse the second half of the list


struct ListNode* secondHalf = reverseList(middle->next);
middle->next = NULL; // Split the list into two halves

// Step 3: Merge the two halves


struct ListNode* firstHalf = head;
while (secondHalf != NULL) {
struct ListNode* tmp1 = firstHalf->next;
struct ListNode* tmp2 = secondHalf->next;

// Reorder nodes: firstHalf -> secondHalf -> firstHalf -> secondHalf -> ...
firstHalf->next = secondHalf;
secondHalf->next = tmp1;

firstHalf = tmp1;
secondHalf = tmp2;
}
}

// Function to create a linked list from an array


struct ListNode* createList(int arr[], int n) {
if (n == 0) return NULL;
struct ListNode* head = createNode(arr[0]);
struct ListNode* temp = head;
for (int i = 1; i < n; i++) {
temp->next = createNode(arr[i]);
temp = temp->next;
}
return head;
}

int main() {
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);

struct ListNode* head = createList(arr, n);

printf("Original list: ");


printList(head);

// Reorder the list


reorderList(head);

printf("Reordered list: ");


printList(head);

return 0;
}

You might also like