All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Solution : //Preprocessor section/header files#include //Standard input\output Library#include //Standard Library //Define a structurestruct Node{ int num; //Declare integer variable struct Node* nextptr; //Pointer to the next Node}; //Function prototypesstruct Node* createNodeList(int num); //Create a node listvoid…
Rishabh Bhojankar
updated on 02 Sep 2023
Solution :
//Preprocessor section/header files
#include
#include
//Define a structure
struct Node
{
int num; //Declare integer variable
struct Node* nextptr; //Pointer to the next Node
};
//Function prototypes
struct Node* createNodeList(int num); //Create a node list
void insertNode(struct Node** tmp, int num); //Insert a node
void displayList(struct Node* tmp); //Display the elements of a list
struct Node* intersection(struct Node* list1, struct Node* list2); //Find the intersection of two lists
struct Node* unionSet(struct Node* list1, struct Node* list2); //Find the union of two lists
int search(struct Node* tmp, int num); //Search for an element in a list
//Main function
int main()
{
struct Node* list1 = NULL;
struct Node* list2 = NULL;
//Input for List 1
int num1;
printf("Enter the number of elements for List 1 : ");
fflush(stdout);
scanf("%d", #1);
for (int i = 0; i < num1> {
int element;
printf("Enter element %d : ", i);
fflush(stdout);
scanf("%d", &element);
insertNode(&list1, element);
}
//Input for List 2
int num2;
printf("Enter the number of elements for List 2 : ");
fflush(stdout);
scanf("%d", #2);
for (int i = 0; i < num2> {
int element;
printf("Enter element %d : ", i);
fflush(stdout);
scanf("%d", &element);
insertNode(&list2, element);
}
printf("List 1 : ");
displayList(list1);
printf("List 2 : ");
displayList(list2);
struct Node* intersectionResult = intersection(list1, list2); //Find the intersection of List 1 and List 2.
printf("Intersection : "); //Print the intersection between list of elements
displayList(intersectionResult); //display the intersection of List 1 and List 2
struct Node* unionResult = unionSet(list1, list2); //Find the union of List 1 and List 2
printf("Union : ");//Print the union of list of elements
displayList(unionResult); //display the union of List 1 and List 2
return 0;
}
//Function to create a node list
struct Node* createNodeList(int num)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->num = num;
newNode->nextptr = NULL;
return newNode;
}
//Function to insert a node
void insertNode(struct Node* *tmp, int num)
{
struct Node* newNode = createNodeList(num);
if (*tmp == NULL)
{
*tmp = newNode;
}
else
{
struct Node* current = *tmp;
while (current->nextptr != NULL)
{
current = current->nextptr;
}
current->nextptr = newNode;
}
}
//Function to display the elements of a list
void displayList(struct Node* tmp)
{
struct Node* current = tmp;
while (current != NULL)
{
printf("%d ", current->num);
current = current->nextptr;
}
printf("\n");
}
//Function to find the intersection of two lists
struct Node* intersection(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
while (list1 != NULL && list2 != NULL)
{
if (list1->num == list2->num)
{
insertNode(&result, list1->num);
list1 = list1->nextptr;
list2 = list2->nextptr;
}
else if (list1->num < list2>num)
{
list1 = list1->nextptr;
}
else
{
list2 = list2->nextptr;
}
}
return result;
}
//Function to find the union of two lists
struct Node* unionSet(struct Node* list1, struct Node* list2)
{
struct Node* result = NULL;
while (list1 != NULL)
{
insertNode(&result, list1->num);
list1 = list1->nextptr;
}
while (list2 != NULL)
{
if (!search(result, list2->num))
{
insertNode(&result, list2->num);
}
list2 = list2->nextptr;
}
return result;
}
//Function to search for an element in a list
int search(struct Node* tmp, int num)
{
struct Node* current = tmp;
while (current != NULL)
{
if (current->num == num)
{
return 1; //Element found
}
current = current->nextptr;
}
return 0; //Element not found
}
Output :
Enter the number of elements for List 1 : 4
Enter element 0 : 1
Enter element 1 : 2
Enter element 2 : 3
Enter element 3 : 4
Enter the number of elements for List 2 : 4
Enter element 0 : 3
Enter element 1 : 4
Enter element 2 : 5
Enter element 3 : 6
List 1 : 1 2 3 4
List 2 : 3 4 5 6
Intersection : 3 4
Union : 1 2 3 4 5 6
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Project 2 - Design and develop the web based Temperature control system using Beagle Bone Black.
Overview of the Project: Device Driver Write the device driver for MCP9808 Temperature Sensor. Probe function: Register the Platform device under driver/misc. Read function: It should send the i2c message to read the data from sensor and copy it to the user space. Alert pin connected to BBB as gpio interrupt. The…
02 Dec 2023 10:59 PM IST
Project 1 - Develop the full featured char driver as a loaded module
Overview of the Project: • Device Driver o Write the device driver for MCP9808 Temperature Sensor. Probe function: Register the Platform device under driver/misc. Read function: It should send the i2c message to read the data from sensor and copy it to the user space. Alert pin connected to BBB as gpio…
01 Dec 2023 10:07 AM IST
Project 3
Que. Program an attack terminal 1. The user should be able to select from the following CAN based attacks a. Full Bus DoS - The program should allow the user to set a duration for the attack b. Partial DoS - The program should ask the user “what priority should I DoS at?” - The program should allow the…
01 Dec 2023 02:28 AM IST
Project 2
Que. Write a program that performs a binary search for any given signal (The instrument cluster can be used as a source for CAN traffic) - It should record a log, assisted by user prompt, and replay all other subsequent logs according to a prompt that the user checks (Did the signal occur again? Y/N) - After every…
30 Nov 2023 10:50 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.