Question
2. NOTE: THE BASIC CODE OF LINED LIST IS GIVEN BELLOW IN TWO WAYS (WITH CLASS (2) AND WITHOUT CLASS (1)). YOU SHOULD WRITE THE
2. NOTE: THE BASIC CODE OF LINED LIST IS GIVEN BELLOW IN TWO WAYS (WITH CLASS (2) AND WITHOUT CLASS (1)). YOU SHOULD WRITE THE MAIN FUNCTION AND THE ADDITIONAL FUNCTIONS NEEDED TO COMPLETE THE PROMLEM EITHER AT CODE 1 OR CODE 2. Given two singly linked lists (List1 and List2) of any size, the task is to create a new linked list using those linked lists. Take an input x from user. The condition to create new linked list is- the nodes greater than x from List1 and the nodes smaller than x at List2 will be added to the new linked list. Example if the two linked lists are: List1: 5->2->3->8->9 List2: 1->7->4->2 And if x=4 Then the nodes greater than X at List1 are 5,8,9 and the nodes smaller than X at List2 are 1,2. So after the execution of your function the new list would look like this: newList: 5- > 8- >9- >1- >2
1) struct node{ int data; node* next; }; void add(node* &sll, int data){ if(sll==NULL){ node* newNode = new node; newNode->data = data; newNode->next = sll; sll = newNode; } else{ node* current = sll; while(current->next!=NULL){ current = current->next; } node* newNode = new node; newNode->data = data; newNode->next = current->next; current->next = newNode; } } int main() { node* List1 = NULL; node* List2 = NULL; int n1,n2; cout << Enter how many nodes you want to enter at list1: cin >> n1; // n=10 for int (int i = 0; i < n1; i++) { int newData = rand()%100; add(List1, newData); } cout << Enter how many nodes you want to enter at list2: cin >> n2; // n=10 for int (int i = 0; i < n2; i++) { int newData = rand()%100; add(List2, newData); }
return 0; }
2) struct node { int data; node* next; }; class linked_list { private: node* head= NULL; node* tail= NULL; public: void add_node(int n) { node* tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } }; int main() { linked_list a; linked_list b; for int (int i = 0; i < 11; i++) { int newData = rand()%100; a.add_node(newData); }
for int (int i = 0; i < 11; i++) { int newData = rand()%100; b.add_node(newData); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started