Question

In: Computer Science

List.h template class List { // List class ADT              public:    virtual void...

List.h
template
class List { // List class ADT
             public:
   virtual void clear() = 0;

// Inserts an item into the list at position index
   virtual bool insert(const ListItemType& newItem) = 0;

   // Append "it" at the end of the list
   // The client must ensure that the list's capacity is not exceeded
   virtual bool append(const ListItemType& newItem) = 0;

   // Deletes an item from the list at a given position
   virtual ListItemType remove() = 0;

   // Set the current position to the start of the list
   virtual void moveToStart() = 0;

   // Set the current position to the end of the list
   virtual void moveToEnd() = 0;

   // Move the current position one step left, no change if already at beginning
   virtual void prev() = 0;

   // Move the current position one step right, no change if already at end
   virtual void next() = 0;

   //Return the number of items stored in the list
   virtual int length() const = 0;

   // Return the position of the current element
   virtual int currPos() const = 0;

   // Set the current position to "pos"
   virtual bool moveToPos(int pos) = 0;

   // Return true if current position is at end of the list
   virtual bool isAtEnd() const = 0;

   // Return the current element
   virtual ListItemType getValue() const = 0;

   // Note: The fololwing methods assume that ListItemType is comparable: that is to say the relational operators such as == exist.

   //search for the first occurance in the list of an item. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   virtual bool find(const ListItemType& newItem) = 0;

   //search for the next occurance in the list of an item. Start with the next position after curr. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   virtual bool findNext(const ListItemType& newItem) = 0;

   // count the number of times a value is found in the list
   // curr should be set back to its current location after the count.
   virtual int count(const ListItemType& newItem) = 0;

};

LList.h

#include "list.h"

// Linked list implementation

template
class Node
{
public:
   ListItemType element;
   Node* next;

   Node()
   {
       next = nullptr;
   }

   Node(const ListItemType& e)
   {
       this->element = e;
       next = nullptr;
   }
};


template
class LList : public List {

private:
   Node* head;   
   Node* tail;   
   Node* curr;   
   int listSize;

public:
   // Constructor
   LList() {
       head = tail = curr = nullptr;
       listSize = 0;
   }

   //Destructor
   ~LList() {
       clear();
   }

   // Remove all elements
   void clear() {
       if (listSize > 0) {
           Node* temp;
           curr = head;
           do {
               temp = curr -> next;
               delete curr;
               curr = temp;
           } while (curr != nullptr);
       }
       curr = tail = head = nullptr;
       listSize = 0;
   }

  
   bool insert(const ListItemType& newItem) {
       if (listSize == 0) {
           head = new Node(newItem);
           tail = head;
           curr = head;
       }
       else if (curr == head) {
           Node* temp = new Node(newItem);
           temp->next = head;
           head = temp;
           curr = head;
       }
       else if (curr == nullptr) {
           return false;
       }
       else {
           Node* temp = head;
           while (temp->next != curr)
               temp = temp->next;
           temp->next = new Node(newItem);
           temp->next->next = curr;
           curr = temp->next;
       }
       listSize++;
       return true;
   }

   // Append "it" to list
   bool append(const ListItemType& newItem) {
      
       // hint: handle an empty list and a non-empty list separately
       cout << "***** complete this method ...\n";
       return false;
   }

   // Remove and return current element
   ListItemType remove() {
       ListItemType it = curr->element;
       if (curr == head) {
           head = head->next;
           delete curr;
           curr = head;
           listSize--;
           if (head == nullptr)
               tail = nullptr;
       }
       else if (curr == tail) {
           moveToPos(listSize - 2);
           delete curr->next;
           tail = curr;
           tail->next = nullptr;
           curr = nullptr;
           listSize--;
       }
       else {
           Node* temp = head;
           while (temp->next != curr)
               temp = temp->next;
           temp->next = curr->next;
           delete curr;
           curr = temp->next;
           listSize--;
       }
       return it;
   }

   void moveToStart() { curr = head; }
   void moveToEnd() { curr = nullptr; }

                                                  
   void prev() {
       if (head == curr) return;
       Node* temp = head;
      
       while (temp -> next != curr) temp = temp -> next;
       curr = temp;
   }

  
   void next() { if (curr != nullptr) curr = curr -> next; }

   int length() const { return listSize; }


                                             
   int currPos() const {
       if (curr == nullptr) return listSize;
       Node* temp = head;
       int i;
       for (i = 0; curr != temp; i++)
           temp = temp -> next;
       return i;
   }

   // Move down list to "pos" position
   bool moveToPos(int pos) {
       if ((pos < 0) || (pos > listSize)) return false;
       curr = head;
       for (int i = 0; i next;
       return true;
   }

   // Return true if current position is at end of the list
   bool isAtEnd() const { return curr == nullptr; }

   // Return current element value.
   ListItemType getValue() const { return curr -> element; }

   // Check if the list is empty
   bool isEmpty() const { return listSize == 0; }

   bool find(const ListItemType& it) {
       cout << "***** complete this method ...\n";
       // hint: if list is empty return false
       // set curr to head and traverse list until curr becomes nullptr or "it"is found
       return false;
   }

   //search for the next occurance in the list of an item. Start with the next position after curr. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   bool findNext(const ListItemType& it) {
       cout << "***** complete this method ..\n";
       // hint: if curr is nullptr return false
       // set curr to curr->next and traverse list until curr becomes nullptr or "it" is found

       return false;
   }
   // count the number of times a value is found in the list
   // curr should remain where it is
   int count(const ListItemType& it) {
       cout << "***** complete this method ...\n";
       // hint: do not use curr, but a temp pointer to traverse the list.
       // set temp to head, and traverse list until temp is nullptr counting elements that are equal
       int count = 0;

       return count;
   }

};
1- Complete the append method in LList.h( Bold )

2- Complete the find, findNext and count in LList.h(Bold)

Solutions

Expert Solution

Append Method

bool append(const ListItemType& newItem) {

// hint: handle an empty list and a non-empty list separately

if (listSize == 0) {

head = new Node(newItem);

tail = head;

curr = head;

} else {

Node* temp = head;

while (temp->next != nullptr)

temp = temp->next;

temp->next = new Node(newItem);

temp->next->next = nullptr;

tail = temp->next;

}

listSize++;

return true;

}

-------------------------------------------------------------------------------------------------

bool find(const ListItemType& it) {

if (listSize == 0) {

return false;

} else {

Node* temp = head;

while (temp!= nullptr){

if(temp->element == it)

return true;

temp = temp -> next;

}

}

return false;

}

----------------------------------------------------------------------------------------------------------------------------------------------------

bool findNext(const ListItemType& it) {

// hint: if curr is nullptr return false

// set curr to curr->next and traverse list until curr becomes nullptr or "it" is found


if(curr == nullptr)

return false;

Node* temp = curr->next;

while (temp!= nullptr){

if(temp->element == it)

return true;

temp = temp -> next;

}


return false;

}

----------------------------------------------------------------------------------------------------------------------------------------------

int count(const ListItemType& it) {

// hint: do not use curr, but a temp pointer to traverse the list.

// set temp to head, and traverse list until temp is nullptr counting elements that are equal

int count = 0;

if (listSize == 0) {

return count;

} else {

Node* temp = head;

while (temp!= nullptr){

++count;

temp = temp -> next;

}

}

return count;

}

--------------------------------------------------------------------------------

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
C++: FramedI is an interface and appears as: class FramedI { public: virtual string getText()=0; virtual...
C++: FramedI is an interface and appears as: class FramedI { public: virtual string getText()=0; virtual char getFrameChar() =0; }; You must implement the interface and call the class FramedText. It must implement the FramedI interface. Any attributes used in the FramedText class MUST be private. Create a function called void showFramedText(ostream&, FramedI&); If the text to display is "Here's my text here" and the framing character is '*', the function must output *********************** * Here's my text here *...
ADT SORTED LIST Fill in the missing code in the following code segment.          void SortedType::PutItem(ItemType...
ADT SORTED LIST Fill in the missing code in the following code segment.          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             // Prepare node for insertion             // Insert node into list }
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT