Question

Fill in the missing code in the following code segment to insert node into list.   ...

Fill in the missing code in the following code segment to insert node into list.   

void SortedType::PutItem(ItemType newItem)

{

                           NodePtr* newNode;                        // pointer to node being inserted

                           NodePtr* predLoc;              // trailing pointer

                           NodePtr* location;               // traveling pointer

                           boolean moreToSearch;

                           location = listData;

                           predLoc = NULL;

                           moreToSearch = (location != NULL);

                           length++;

                           // Find insertion point

while (moreToSearch)

                           {

if (location->info < newItem)

{

predLoc = location;

location = location->next;

moreToSearch = (location != NULL);

}

                                       else

                                                        moreToSearch = false;

                           }

                           // Prepare node for insertion

                           newNode = new NodeType;

                           newNode->info = newItem;

// Insert node into list.

// add your code here ...

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the completed code.

void SortedType::PutItem(ItemType newItem)
{
   NodePtr* newNode;   // pointer to node being inserted
   NodePtr* predLoc;   // trailing pointer
   NodePtr* location;   // traveling pointer
   boolean moreToSearch;
   location = listData;
   predLoc = NULL;
   moreToSearch = (location != NULL);
   length++;
   // Find insertion point

   while (moreToSearch)
   {
       if (location->info < newItem)
       {
           predLoc = location;
           location = location->next;
           moreToSearch = (location != NULL);
       }
       else
           moreToSearch = false;
   }

   // Prepare node for insertion
   newNode = new NodeType;
   newNode->info = newItem;

   // Insert node into list.

   // add your code here ...
   newNode->next = location;
   if(predLoc == NULL) //adding to beginning of list
       listData = newNode;
   else
       predLoc->next = newNode;
}

Add a comment
Know the answer?
Add Answer to:
Fill in the missing code in the following code segment to insert node into list.   ...
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
  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Class UnsortedType{ public: //all the prototypes go here. private: int length; NodeType* listData; };    void...

    Class UnsortedType{ public: //all the prototypes go here. private: int length; NodeType* listData; };    void UnsortedType::DeleteItem(ItemType item) // Pre:Item is in list NodeType* tempPtr;// pointer delete NodeType* predLoc;// trailing pointer NodeType* location; // traveling pointer bool found = false; location = listData; predLoc = _____________; // 20 length--; // Find node to delete. while (____________) ; // 21 { switch (__________________) ; // 22 { case GREATER: case LESS   : predLoc = location; location = ___________; // 23 break;...

  • Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts...

    Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • ) Given the following unorderedLinkedList class, provide an implementation for the member function reverse. When the...

    ) Given the following unorderedLinkedList class, provide an implementation for the member function reverse. When the reverse function is called on an unorderedLinkedList object called mylist (e.g. mylist.reverse()), it will create a new unorderedLinkedList and insert the elements of mylist into the new list in reverse order and then return the new list. struct nodeType {                int info;                nodeType *link; }; class unorderedLinkedList { protected:                nodeType *first; //pointer to the first node of the list public:               ...

  • CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a...

    CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a ItemType and pointer // (to a NodeType) field struct NodeType {    ItemType value;    NodeType * next; }; bool Delete (NodeType * & firstPtr, ItemType value) {    NodeType * prev, * cur; cur = Search (firstPtr, value, prev); if (cur==NULL) return false; else { // remove cur node from the linked list if (prev!=NULL) { //not the first one prev->next = cur->next;...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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