Question

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 list A is:
2->3->5->6->8->9->11

CODE BELOW

#include <iostream>
using namespace std;

// The node structure to be used in the linked list
struct Node {
int data;
Node *pNext;
};

/*
* insertList function
* 1) Given list A, list B and an integer n, insertList inserts
* list B into the middle of list A, after the list value n.
* 2) Assume list A and B are not empty.
* 3) Assume n is contained exactly once in list A.
* parameter(s):
* pHeadA: the head of list A.
* pHeadB: the head of list B.
* n: the node value to insert after.
* return: None
*/
void insertList( Node *pHeadA, Node *pHeadB, int n)
{
// insert your code here
}

//---------------------------------------
// You do not need to change anything below this line
// However, you will need to read it to understand how the Linked List
// is pushing data elements to the list.
//---------------------------------------
void display(Node *pHead){
while( pHead != NULL) {
cout << pHead->data << " ";
pHead = pHead->pNext;
}
cout << endl;
}

void buildList(Node *&pHead){
Node *pTemp;
int userInput;
cin >> userInput;  
// Keep looping until end of input flag of -1 is given
while( userInput != -1) {
// Store this number on the list
pTemp = new Node;
pTemp->data = userInput;
pTemp->pNext = pHead;
pHead = pTemp;
cin >> userInput;
}
}

int main()
{
// Build list A and list B
Node *pHeadA = NULL, *pHeadB = NULL;
cout<<"Enter numbers for list A: (separate by space, enter -1 to indicate end of list)" <<endl;
buildList(pHeadA);
cout<<"Enter numbers for list B: (separate by space, enter -1 to indicate end of list)" <<endl;
buildList(pHeadB);

// Display list A and list B
cout << "We are starting with two lists: " << endl;
cout << "List A is: " << endl;
display(pHeadA);
cout << "List B is: " << endl;
display(pHeadB);

// Get the node value to insert after
int insertValue;
cout << "Insert after node value: " << endl;
cin >> insertValue;
  
// insert listB into the middle of listA, after the designated node value.
insertList( pHeadA, pHeadB, insertValue);
  
// Display the resulting list
cout << "Resulting list A is: " << endl;
display(pHeadA);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
You need to complete a function called insertList. Given list A, list B and an integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • You are given the code for a singly linked list-based implementation of the List ADT. You...

    You are given the code for a singly linked list-based implementation of the List ADT. You will add a function called isUnique( ) to the List class such that the function will return true if all the integer elements in the List are unique and return false otherwise. The main function is written in such a way that it will create 1000 Lists (each of size listSize) of random integers in a range [1...maxValue] and will call the isUnique function...

  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

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