Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone please help me fill out the last .cpp code file (MERGELL.CPP)? the first portion is the main .cpp and then the header file,

can someone please help me fill out the last .cpp code file (MERGELL.CPP)? the first portion is the main .cpp and then the header file, we are to only work/edit/complete the MERGELL.CPP section. I divided each section by asterisks.

/////////////////////////MAIN CPP////////////////////////////

#include "MergeLL.h" int main() { //initialize our pointer to NULL LinkedList LL; int input; cout << "Please input some numbers." << endl; //get positive numbers do { cout << "#"; cin >> input; while( !cin ) { cin.clear(); cin.ignore(2000, ' '); cout << "Not a legal integer" << endl; cout << "#: "; cin >> input; } //if they're positive, add them to the linked list if(input > 0) LL.Add(input); } while( input > 0 ); cout<<"The numbers you entered are:"< 

************************************************************************************************************************************************************************************************

/////////////////////////HEADER FILE/////////////////////////

 #include using namespace std; //a basic node for a single linked list with data struct Node { int data; Node *next; }; //a linked list class we will use class LinkedList { public: LinkedList(); ~LinkedList(); void Add(int); void PrintList(); void Sort(); private: void DeleteList(); void MergeSort(Node*&); Node* Split(Node*); Node* Merge(Node*,Node*); Node* Head; }; 

****************************************************************************************************************************************************************************************************

/////////////////////MERGELL.CPP////////////////////////

#include "MergeLL.h" //constructor LinkedList::LinkedList() { //should set head pointer Head = NULL; } //destructor LinkedList::~LinkedList() { //should call delete list DeleteList(); } /* This function adds a new node to the end of the single linked list. If it is the first node, then head will still be NULL. head -> [2|] -> [8|] -> [6|X] */ void LinkedList::Add(int newdata) { //create a new node to hold the data with a terminal (NULL) next pointer Node* temp = new Node; temp->data = newdata; temp->next = NULL; //check whether head has been initialized (is NULL) //if not, make the new node head and set prev if(Head == NULL) { Head = temp; } else { //if head has been initialized //find the end of the chain with a pointer Node *cur = Head; while(cur->next != NULL) cur = cur->next; //add the new node on to the last node in the list //set pointer cur->next = temp; } } //This function walks through the list and prints the integer for each of the nodes //We are using a copy of head, so changing it's value does not alter the //variable in main void LinkedList::PrintList() { //walk through the list Node *cur = Head; while(cur != NULL) { //print the data for the current node cout<data<<" "; cur = cur->next; } cout << endl; } //This function walks through the list and deletes each node void LinkedList::DeleteList() { //make sure the list has data if(Head != NULL) { //delete first node Node *next = Head->next; delete Head; //loop through list deleting each node while (next != NULL) { Head = next; next = Head->next; delete Head; } //set pointer to null Head = NULL; } } //Call mergesort passing the entire list void LinkedList::Sort() { MergeSort(Head); } //implement Merge sort for a single linked list implementation void LinkedList::MergeSort(Node* &head) { //base case //find the midway point to split in two Node* midpoint = Split(head); //call two sublists - these are now sorted MergeSort(head); MergeSort(midpoint); //now merge the two lists head = Merge(head, midpoint); } //split the the list into two lists Node* LinkedList::Split(Node* head) { //find the midway point to split in two Node* midpoint; //add nullpointer to end of first (split them) (check special cases) return midpoint; } //merge two sorted lists Node* LinkedList::Merge(Node* head1, Node* head2) { //now the two sublists are sorted //loop through each and put them in the right order Node *newhead; //always points at the front of the new list Node *tail; //always points at the back of the new list //set up the first node in the new list //while we still have two lists, keep adding the next item to the end //of our new list, move the pointer, and set the tail //attach the rest of the list that hasn't been processed to our combined list. //at the end you have a single sorted list return newhead; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

Predict the geometry and bond angles of SOCl 2 .

Answered: 1 week ago

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago