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

Student Ice Cream Preference Program

The document describes a C program that manages two sets of students based on their ice-cream preferences: vanilla and butterscotch. It allows users to input the total number of students and their preferences, then computes and displays the students who like both flavors, those who like either but not both, and the number of students who like neither. The program utilizes a linked list to store the student data and includes functions for inserting, searching, and displaying the lists.

Uploaded by

yagamisofy.s
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 views2 pages

Student Ice Cream Preference Program

The document describes a C program that manages two sets of students based on their ice-cream preferences: vanilla and butterscotch. It allows users to input the total number of students and their preferences, then computes and displays the students who like both flavors, those who like either but not both, and the number of students who like neither. The program utilizes a linked list to store the student data and includes functions for inserting, searching, and displaying the lists.

Uploaded by

yagamisofy.s
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

program 5: Second year Computer Engineering class, set A of students like Vanilla

Ice-cream and set B of students like butterscotch ice-cream. Write a program to


store two sets using a linked list. compute and display) Set of students who like
both vanilla and butterscotch b) Set of students who like either vanilla or
butterscotch or not both c) Number of students who like neither vanilla nor
butterscotch.

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

struct Node {
int data;
struct Node *next;
};

// Insert at end
struct Node* insert(struct Node* head, int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) return newNode;
struct Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
return head;
}

// Search element
int search(struct Node* head, int val) {
while (head) {
if (head->data == val) return 1;
head = head->next;
}
return 0;
}

// Display list
void display(struct Node* head) {
if (!head) { printf("{}"); return; }
printf("{ ");
while (head) {
printf("%d ", head->data);
head = head->next;
}
printf("}");
}

int main() {
struct Node *A = NULL, *B = NULL;
int total, n1, n2, val;

printf("Enter total students: ");


scanf("%d", &total);

printf("Enter number of students who like Vanilla: ");


scanf("%d", &n1);
printf("Enter roll numbers: ");
for (int i = 0; i < n1; i++) {
scanf("%d", &val);
A = insert(A, val);
}

printf("Enter number of students who like Butterscotch: ");


scanf("%d", &n2);
printf("Enter roll numbers: ");
for (int i = 0; i < n2; i++) {
scanf("%d", &val);
B = insert(B, val);
}

// a) Both
printf("\n(a) Students who like both: ");
struct Node* t = A;
int found;
while (t) {
if (search(B, t->data)) printf("%d ", t->data);
t = t->next;
}

// b) Either but not both


printf("\n(b) Students who like either but not both: ");
t = A;
while (t) {
if (!search(B, t->data)) printf("%d ", t->data);
t = t->next;
}
t = B;
while (t) {
if (!search(A, t->data)) printf("%d ", t->data);
t = t->next;
}

// c) Neither
int count = 0;
for (int i = 1; i <= total; i++) {
if (!search(A, i) && !search(B, i))
count++;
}
printf("\n(c) Number of students who like neither: %d\n", count);

return 0;
}

INPUT:
Enter total students: 10
Enter number of students who like Vanilla: 4
Enter roll numbers: 1 2 3 4
Enter number of students who like Butterscotch: 5
Enter roll numbers: 3 4 5 6 7

OUTPUT:
(a) Students who like both: 3 4
(b) Students who like either but not both: 1 2 5 6 7
(c) Number of students who like neither: 3

You might also like