Question

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 member function Merge() that has the following precondition and postcondition

1) Precondition: list2 that has been initialized and not empty

2) Postcondition: Insert all the elements to the current list from list2 . Analyze its time complexity in terms of n which is the number of elements in the list2

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

Sourcecode :

class ExtendedSortedList:public SortedList {

public:

bool isThere(Itemtype item) {

Nodetype temp = listData;

while (temp->next != NULL) {

if (temp->getValue() == item) {

return true;

}

}

return false;

}

};

Time Complexity:

In the worst case (when the element is present at the end), this function takes O time.

Add a comment
Answer #2

1.

Method to be Copied:

//Implement the function
void SortedType::Merge(SortedType list2)
{
   //define the tempp node
   SortedType tempnode();
   //initialize the temp nodes
   NodeType* list1temp = listData;
   //Initalized the list2
   NodeType* list2temp = list2.listData;
   //declare local variable
   int i = 0;
   //check wether the nodes are null or not
   //Repeat the loop until nodes are null
   while (list1temp != NULL && list2.list2temp != NULL)
   {
       //check wether the value is greater than or not
       //if the element is greater in the list1
       if (list1temp->info.getValue() > list2.tmepPtr2->info.getValue())
       {
           //if index is 0
           //merge tge nodes and set the index to next index
           //to add the element
           if (i == 0)
           {
               tempnode.listData = list2.listData;
               tempnode.currentPos = tempnode.listData;
               list2temp = list2temp->next;
               //increment the index
               i++
           }
           //Otherwise the increment the index
           else
           {
               //set the next current node to the second list
               tempnode.currentPos->next = list2temp;
               tempnode.currentPos = tempnode.currentPos->next;
               list2temp = list2temp->next;
               i++;
           }
       }
       //if the element is lesser than the element in the element in the
       //second list then set the data to the end of the list
       else
       {
           if (i == 0)
           {
               tempnode.listData = listData;
               tempnode.currentPos = tempnode.listData;
               list1temp = list1temp->next;
               i++
           }
           else {
               tempnode.currentPos->next = list1temp;
               tempnode.currentPos = tempnode.currentPos->next;
               list1temp = list1temp->next;
               i++;
           }
       }
   }
   //If the list1 elements have more elements of list 2
   while (list1temp != NULL)
   {
       //IF list1 has more element than list2
       tempnode.currentPos->next = list1temp;
       tempnode.currentPos = tempnode.currentPos->next;
       list1temp = list1temp->next;
       i++;
   }
   //If the list2 elements have more elements of list1
   while (list2temp != NULL)
   {
       //IF list2 has more element than list2
       tempnode.currentPos->next = list2temp;
       //set the pointer to the next element
       tempnode.currentPos = tempnode.currentPos->next;
       list2temp = list2temp->next;
       //increment the index
       i++;
   }
   //decrement the length of the temp node
   tempnode.length = --i;
   *this = tempnode;
}

Add a comment
Know the answer?
Add Answer to:
Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts...
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
  • 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;...

  • 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)...

  • MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...

    MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as defined by the following skeleton: #define MAX_ITEMS 10 typedef char ItemType; class UnsortedList {        private:             int length; ItemType values[MAX_ITEMS]; int currentPos;        public:             SortedList( ); // default constructor: lenght=0, currentPos=-1             void MakeEmpty;    // let length=0             void InsertItem(ItemType x);   // insert x into the list                 void DeleteItem(ItemType x); // delete x from the list bool IsFull( );   // test...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • 5. Given the following definition of class Node: class Node { public int item; public Node...

    5. Given the following definition of class Node: class Node { public int item; public Node next; public Node ( int i, Node n ) { item = i; next = n; }; } and the following declarations: Node list, p, q; Assume the structure starts as below. Draw the structure created by executing the following statements. Each part is independent. list 1 2 3 4 5   [2] a) p = list; q = null; while ( p != null...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

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
ADVERTISEMENT