Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Execute the main function given to you and sort the lists, print the final and sorted list. Start with an empty list as given in

Execute the main function given to you and sort the lists, print the final and sorted list. Start with an empty list as given in your main program, fill in the linked list (you can push extra items if you want to).
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
THE MAIN
//This program tests various operation of a linked list
//34 62 21 90 66 53 88 24 10 -999
#include
#include "unorderedLinkedList.h"
using namespace std;
int main()
{
/* Start with the empty list */
Node* res = NULL;
Node* a = NULL;
/* Let us create a unsorted linked lists to test the functions
Created lists shall be a: 2->3->20->5->10->15 */
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 2);
/* Sort the above created Linked List */
MergeSort(&a);
cout
printList(a);
/*You can implement/change the list types in your preference as long
as your algorithm holds true*/
return 0;
}
- linkedListh Ce main.cpp - unordered Linked List.h #ifndef H_UnorderedLinkedList #define H_UnorderedLinkedList #include "linked List.h using namespace std template class unorderedLinkedList: public linkedListTypeType> public: bool search(const Type& searchItem) const; V/Function to deterwine whether searchiten is in the list. V/Postcondition: Returns true i searchitem as in the list 27 otherwise the value folse is returned void insertFirst(const Type& newitem); /Function to sert newItem at the beginning of the list. V/Postcondition: first points to the new List, newitem is Z inserted at the beginning of the list, last points to The Last node, and count is incremented by 1. void insertLast(const Type8 newItem); V/Function to insert nemitem at the end of the list. V/Postcondition: first points to the new list, newitem is 7 inserted at the end of the list, cost points to the Last node, and count is incremented by 1. void deleteNode(const Type& deleteItem); V/Function to delete deleteltent from the list. V/Postcondition: If found, the node containing deleteItem 12 is deleted from the list. first points to the first V/ nade, Last points to the last node of the updated 2 List, and count is decremented by 1. template bool unorderedLinkedList current; //pointer to troverse the List bool found = false; current = linkedListTypeType: :first; //set current to point to the first ode in the list while (current != NULL 88 !found V/search the list if (current->info == searchitem) // searchitem is found found = true; else current = current->link: //mare current point to 1/the next node return found }//end search template void unordered LinkedList::insertFirst (const Types newItem) nodeType *newlode //pointer to create the new node newMode = new nodeTypeType>;//create the new node newNode->info = newItem; // store the new item in the node newMode-link - linkedListTypeType>:: first //insert newode before first linkedListTypeType> : :first = newlode make first point to the //octual first node linkedListTypeType>::count++; // increment count if (linked ListTypeType>last == NULL) //f the list was empty newWode is also V/the Lost node in the list linked ListTypeType>: last = newNode }//end insertFirst template class Tune template void unorderedLinkedList *newlode; //pointer to create the new node 3. newMode = new nodeTypeType>; //create the new node newNode->info = newItem/store the new item in the node newlode->link = NULL V/set the link field of new X/to NULL if (linked ListTypeType>:first == NULL) //if the list is empty, newlode is Aboth the first and Last node linked ListType: first = newlode linked ListType Type>last = newlode linked ListTypecType>::count++; //increment count 3 else // the list is not empty, insert newlode after Last { linked ListTypeType>tlast->link = newlode //insert newWode after Last linked ListTypeType>: last = newlode; //make tast point to the actual // Last node in the list linkedListType Type> count++; \/increment count 3 Bend insertlast template void unorderedLinkedList deleteNode(const Type& deleteItem nodeType "current pointer to traverse the list nodeType *trailCurrent, pointer just before current bool found if (linkedListType Type: :first == NULL) //Case 1: the list is empty. cout : :first->info == deleteItem //case 2 { current = linkedListTypeIccount --> if (linkedListTypeType>: first == NULL) //the list has only one node linkedListTypeType>: last = NULL; delete current } else // search the list for the node with the given info { found = false; trailCurrent = linkedListTypeType>: first; //set trailCurrent to point Wto the first node current = linkedListTypeType>: first->link //set current to point to the second node while (current != NULL && found { if (current-info != deleteItem trailCurrent = current current = current-> link 3 else found = true; }//end while if (found) //Cose 3; if found, delete the node 4.2 trailCurrent->link = current->link linkedListTypeType>::count-- if (linked ListType void unorderedLinkedList dividelist(node Type Type>* firsti nodeType * &first2) 5 nodeTypeType>* middle nodeTypelink=NULL) first2 NULL else if(first1= NULL) first2=NULL; else { middle = first1 current first->link: if (current != NULL) //list has more than two modes current = current->link while (current != NULL) { middle middle-link; current-current->link; if (current!=NULL) current = current->link }//end while first2 = middle->link; first points to the first /ode of the second sublist middle->link=NULL; //****set the Link of the last hode here //of the first sublist to NULL }//end else } //end dividelist template node Type Type>* unordered LinkedListEmergelist(nodeType* firsti nodeType* first2) 2.1 nodeType Type lastSmall pointer to the last node of the merged List nodeType Type> newhead; //pointer to the merged list if (firsti == NULL) //the first sublist is empty return first2 else if (first2 == NULL) // the second sublist is anpty return first1 else [ if (first1- info link; lastSmall - newHead } else newHead = first2 first2 = first2-link lastSmall - newhead } while (first1 != NULL I first2 != NULL) if (first1-infolink = firsti lastSmall - lastSmall->link; first1 - first1-link ] else { lastSmall->link = first2 lastSmall = lastSmall-> link first2 = first2-link; ] } // end while if (firsti == NULL) //first sublist is exhausted first last Small->link = first2 2.2 else //second sublist is exhausted first last Small->link = firsti; return new lead }//end mergetist template void unorderedLinkedList* Shead) nodeTypeType> *other Head: if (head 1= NULL) //1f the list is not empty if (head-link 1= NULL) { divideList (head, otherHead); rechergeSort (head); rechergeSort (otherHead); head = mergelist(head otherHead) } } //end recMergesort template void unorderedLinkedList Type: mergesort) recMergesort (first); if (first == NULL) last = NULL; else last = first; while (last->link != NULL) last = last->link } } //end mergesort

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

Step: 3

blur-text-image

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

What does this key public know about this issue?

Answered: 1 week ago

Question

What is the nature and type of each key public?

Answered: 1 week ago

Question

What does this public need on this issue?

Answered: 1 week ago