In div c++ // please with comments and check output thanks Q.complete miss 4 fun
ID: 3776146 • Letter: I
Question
In div c++ // please with comments and check output thanks
Q.complete miss 4 functions in code
- You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLists(L,P) will print the elements in L that are in positions specied by P.
- . Swap two adjacent elements by adjusting only the links (and not the data) using singly linked list.
- . Given two sorted lists, L1 and L2, write a procedure to compute L1 L2 using only the basic list operations.
- . Given two sorted lists, L1 and L2, write a procedure to compute L1 L2 using only the basic list operations.
-------------------------------------------------------------
Explanation / Answer
using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = NULL, *P = NULL; appendNode(L, 1); appendNode(L, 2); appendNode(L, 3); appendNode(L, 4); appendNode(L, 6); appendNode(L, 8); displayList(L); appendNode(P, 1); appendNode(P, 2); appendNode(P, 4); appendNode(P, 6); displayList(P); printLists(L, P); //complete this function swapNodes(L, 30); //It should swap the links of nodes 30 and 40 in L. findIntersection(L, P); //This method should print 1, 2, 4 ,6 findUnion(L, P); //It should print 1,2,3,4,6,8 return 0; } void printLists(Node* L, Node* P) { //The output of the function should be: // 1, 2, 4, 8 } //swap the node containing item s with the next node in the list void swapNodes(Node* &head, int s) { //write your code here } void findIntersection(Node* first, Node* P) { Node* newList; //this list should contain intersection of L and P //write your code here //display the new list after union displayList(newList); } void findUnion(Node* first, Node* P) { Node* newList; //this list should contain Union of L and P //write your code here //display the new list after union displayList(newList); } Node* createNode(int value) { Node *node = new Node; node->item = value; node->next = NULL; return node; } void appendNode(Node* &head, int value) { Node *newNode, *currNode; newNode = createNode(value); if(head == NULL) //the list is empty head = newNode; //make a new list else { //list is not empty currNode = head; //current node points to the list head while (currNode->next != NULL) //move current node in the list currNode = currNode->next; currNode->next = newNode; //add new node at the end } } void displayList(Node* head) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.