Question

In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we
Sum lists: Given list A and list B of the same length, traverse them to create a new list, where the first new node on A plus
Can you make simple code of this using C++
In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we have: struct Node int data; Node 'pNext; and in main we have: Node "pHead = NULL; // pointer to the head of the list Node·pTemp: // Used to get new nodes Node "pHeadA NULL: Node "pHeadB = NULL; We will also provide for you the code that is used to create the list and the code to traverse and display the list elements. You can also assume that lists being processed already exist, and that the pointers for new lists are initialized to NULL. For the examples below, unless specified otherwise assume the list we are starting with is the list:
Sum lists: Given list A and list B of the same length, traverse them to create a new list, where the first new node on A plus the first node on B, the second new list node contains the sum of the second node on A plus the second node on B, 1. and so on. Assume list A and list B contain the following pListA is: 1->3->4-7 pListB is: 2->4->6->10 Calling combineLists/...from within main would look like the following: combineLists( Node "&pHead, Node pListA, Node "pListB) displayList( pHead); and it would result in the following output 3->7-10->17
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

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

struct node* addNode(struct node*list,int data){
   struct node*newnode = new(struct node);
   newnode->data = data;
   newnode->next=NULL;
  
   if(!list)return newnode;
  
   struct node*current = list;
   while(current->next)current=current->next;
  
   current->next = newnode;
   return list;
}

void print(struct node*list){
   while(list){
       cout<<list->data<<" ";
       list=list->next;
   }
   cout<<"\n";
}

void combineLists(node*&pHead,node*pHeadA,node*pHeadB){
   while(pHeadA&&pHeadB){
       pHead = addNode(pHead,pHeadA->data+pHeadB->data);
       pHeadA = pHeadA->next;
       pHeadB = pHeadB->next;
   }
   while(pHeadA){
       pHead = addNode(pHead,pHeadA->data);
       pHeadA = pHeadA->next;
   }
   while(pHeadB){
       pHead = addNode(pHead,pHeadB->data);
       pHeadB = pHeadB->next;
   }
}

int main(){
   node* pHead = NULL;
   node* pHeadA = NULL;
   node* pHeadB = NULL;
  
   pHead = addNode(pHead,2);
   pHead = addNode(pHead,3);
   pHead = addNode(pHead,5);
   pHead = addNode(pHead,6);
   pHead = addNode(pHead,8);
   pHead = addNode(pHead,9);
   pHead = addNode(pHead,11);
   cout<<"List is : \n";
   print(pHead);
  
   pHeadA = addNode(pHeadA,1);
   pHeadA = addNode(pHeadA,3);
   pHeadA = addNode(pHeadA,4);
   pHeadA = addNode(pHeadA,7);
   cout<<"\nList A is : \n";
   print(pHeadA);
  
   pHeadB = addNode(pHeadB,2);
   pHeadB = addNode(pHeadB,4);
   pHeadB = addNode(pHeadB,6);
   pHeadB = addNode(pHeadB,10);
   cout<<"\nList B is : \n";
   print(pHeadB);
  
   pHead = NULL;
   combineLists(pHead,pHeadA,pHeadB);
   cout<<"\nCombine List is : \n";
   print(pHead);
   return 0;
}

//sample output

CAUsers IshuManish\Documentsl combinelisy.exe List is : 2 3 5 6 89 11 List A is 1 3 4 7 List B is: 2 4 6 10 Combine List is 3

Add a comment
Know the answer?
Add Answer to:
Can you make simple code of this using C++ In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we have: struct Node int data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You need to complete a function called insertList. Given list A, list B and an integer...

    You need to complete a function called insertList. Given list A, list B and an integer n, insertList inserts list B into the middle of list A, after the list value n. NOTE: 1) All positions and indices start from 0. 2) Write your code in the empty function of insertList, DO NOT change anything in the main. Example Assume we are starting with two lists: List A is: 2->3->9->11 List B is: 5->6->8 Insert after node value: 3 Resulting...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT