Question: Fuller 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

Fuller 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; } 1. Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1) and l2 = (4, 5), after return from concatenate(l1,l2) the list l1 should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2 and should not directly link nodes from l1 to l2 (i.e. the nodes inserted into l1 should be copies of the nodes from l2.) void concatenate(Node*& h1, Node* h2); // Precondition: h1 and h2 are head pointers of linked lists. The lists may be empty or non-empty. // Postcondition: A copy of list h2 is concatenated (added to the end) of list h1. List h2 should be unchanged by the function. // NOTE: The nodes added to the list h1 must be copies of the nodes in list h2. 2. Write a function to insert a number as the new ith node of a linked list. Nodes initially in positions i, i+1, ..., n should be shifted to positions void insertith(Node*& head_ptr, int value, size_t i); void insertith(Node*& head_ptr, int value, size_t i); // Precondition: head_ptr is the head pointer of a linked list. The list may be empty or non-empty. // Postcondition: The number value is inserted as the ith member of the list head_ptr. If the list head_ptr has fewer than i-1 nodes in it, then value is inserted as the last node in the list. 3. Write a function to remove duplicate entries in a linked list. For example, given the list (5, 2, 2, 5, 3, 9, 2) as input, your function should change the list so that on return from the function it contains (5, 2, 3, 9). void removeDups(Node*& head_ptr); // Precondition: head_ptr is the head pointer of a linked list. The list may be empty or non-empty. // Postcondition: Each node in the list must have a unique element value.

This is the code i have to where i have to implement the three plus later down i have to wirte the code for append

#include "List.h"

#include "ListNode.h"

#include "Settings.h"

namespace cs52 {

List::List() {

head = NULL;

listSize = 0;

}

List::~List() {

#ifdef SHOW_DESTRUCTOR_CALLS

std::cout << "about to--> makeEmpty();" << std::endl;

#endif

makeEmpty();

#ifdef SHOW_DESTRUCTOR_CALLS

std::cout << "about to--> delete( head );" << std::endl;

#endif

delete( head );

}

/*need to implement these 3*/

void List::concatenate(const List& B)

void List::insertith(const int& data, const size_t& i)

void List::removeDups()

bool List::isEmpty() const {

return( head == NULL );

}

void List::makeEmpty() {

while (head != NULL) {

remove( head->getElement() );

}

}

int List::size() const {

return( listSize );

}

void List::insert( const int& data ) {

// place data into a ListNode at the front of the list

// it will move the head node to behind the node

// we create dynamically

ListNode* temp = head;

ListNode* newnode = new ListNode( data );

head = newnode;

newnode->setNext( temp );

listSize++;

}

void List::append( const int& data ) {

// Insert code here...

}

void List::remove( const int& data ) {

ListNode* current = head;

ListNode* previous = NULL;

ListNode* nodeToRemove = NULL;

while (current != NULL) {

// have we found it at the current node???

if (current->getElement() == data) {

// found it at head node

if (previous == NULL) {

nodeToRemove = head;

head = head->getNext();

}

// found it inside the list somewhere

else {

nodeToRemove = current;

// skip the current node

previous->setNext( current->getNext() );

}

delete( nodeToRemove );

listSize--;

break;

}

// keep looking

else {

previous = current;

current = current->getNext();

}

}

}

std::ostream& operator << ( std::ostream& outs, const List& l) {

return( l.printList( outs ) );

}

std::ostream& operator << ( std::ostream& outs, const List* l) {

return( l->printList( outs ) );

}

std::ostream& List::printList( std::ostream& outs ) const {

if (isEmpty())

outs << "Empty List" << std::endl;

else {

outs << "List has " << size() << " elements: " << std::endl;

ListNode* current = head;

while (current != NULL) {

outs << current->getElement() << " -> ";

current = current->getNext();

}

outs << " NULL";

outs << std::endl;

}

return( outs );

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!