Question

In: Computer Science

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below.

=========================================================

// listmain.cpp

#include "Linked_List.h"

int main(int argc, char **argv)
{
     float           f;
     Linked_List *theList;

     cout << "Simple List Demonstration\n";
     cout << "(List implemented as an Array - Do not try this at home)\n\n";
     cout << "Create a list and add a few tasks to the list";

     theList = new Linked_List(); // Instantiate a list object

     theList->Insert(5, 3.1f); // Note: The argument to the funtion should be a float
     theList->Insert(1, 5.6f); // A constant real number like 3.1 is interpreted as
     theList->Insert(3, 8.3f); // a double unless it is explicitly defined as a float
     theList->Insert(2, 7.4f); // by adding an 'f' to the end of the number.
     theList->Insert(4, 2.5f);

     // Show what is in the list
     theList->PrintList();

     // Test the list length function
     cout << "\nList now contains " << theList->ListLength() << "items.\n\n";

     // Test delete function
     cout << "Testing delete of last item in list.\n";
     theList->Delete(4);
     theList->PrintList();

     // Test delete function
     cout << "Testing delete of first item in list.\n";
     theList->Delete(5);
     theList->PrintList();

     // Test delete function
     cout << "Testing delete of a middle item in list.\n";
     theList->Delete(3);
     theList->PrintList();

     // Test delete function with a known failure argument
     cout << "Testing failure in delete function.\n";
     if(theList->Delete(4))
          cout << "Oops! Should not have been able to delete.\n";
     else
          cout << "Unable to locate item to delete.\n";

     // Test search (known failure)
     cout << "Testing Search function. Search for key 3\n";
     if(theList->Search(3, &f))
          cout << "Search result: theData = %f\n", f;
     else
          cout << "Search result: Unable to locate item in list\n";

     // Test search (known success)
     cout << "Testing Search function. Search for key 2\n";
     if(theList->Search(2, &f))
          cout << "Search result: theData = " << f << "\n";
     else
          cout << "Search result: Unable to locate item in list\n";

     cout << "\n\nEnd list demonstration...";

     return 0;
}

========================================================================================

// linked_list.h functions

#include
using namespace std;

// Define a structure to use as the list item
struct ListItem
{
     int      key;         
     float    theData;
      ListItem *next;
};

class Linked_List
{
     private:
          ListItem *head;               // Pointer to head of the list

     public:
          Linked_List();               // Class constructor
          ~Linked_List();              // Class destuctor
          void ClearList();             // Remove all items from the list
          bool Insert(int key, float f);// Add an item to the list
          bool Delete(int key);         // Delete an item from the list
          bool Search(int key, float *retVal); // Search for an item in the list
          int ListLength();             // Return number of items in list
          bool isEmpty();               // Return true if list is empty
          bool isFull();                // Return true if list is full
          void PrintList();             // Print all items in the list
};

#endif // End of list header

Solutions

Expert Solution

// linked_list.h functions

#include
using namespace std;

// Define a structure to use as the list item
struct ListItem
{
     int      key;         
     float    theData;
      ListItem *next;
};

class Linked_List
{
     private:

// Pointer to head of the list
          ListItem *head;

     public:

// Class constructor
          Linked_List()   

{

}

// Class destuctor
          ~Linked_List()

{

}

  // Remove all items from the list
          void ClearList()

{

/* deref href to get real head */

   struct node* current = *href;

   struct node* next;

   while (current != NULL)

   {

       next = current->next;

       free(current);

       current = next;

   }

   

   /* deref href to affect real head back

      in the caller. */

   *href = NULL;

}

// Add an item to the list
          bool Insert(int key, float f)

{

int i;

    struct ListItem *temp,*left,*right;

    right=head;

    for(i=1;i<f;i++)

    {

    left=right;

    right=right->next;

    }

    temp=(struct ListItem *)malloc(sizeof(struct ListItem));

    temp->data=f;

    left->next=temp;

    left=temp;

    left->next=right;

    return bool;

}

  // Delete an item from the list
          bool Delete(int key)

{

struct ListItem *temp, *prev;

    temp=head;

    while(temp!=NULL)

    {

    if(temp->f==f)

    {

        if(temp==head)

        {

        head=temp->next;

        free(temp);

        return bool;

        }

        else

        {

        prev->next=temp->next;

        free(temp);

        return bool;

        }

    }

    else

    {

        prev=temp;

        temp= temp->next;

    }

    }

    return bool;

}

// Search for an item in the list
          bool Search(int key, float *retVal)

{

}

// Return number of items in list
          int ListLength();

{

    struct ListItem *n;

    int c=0;

    n=head;

    while(n!=NULL)

    {

    n=n->next;

    c++;

    }

    return c;

}

          bool isEmpty();               // Return true if list is empty

{

    struct node *n;

    int c=0;

    n=head;

    while(n!=NULL)

    {

    n=n->next;

    c++;

    }

if(c==0) return 1;

else 0;

}

          bool isFull();                // Return true if list is full
          void PrintList() // Print all items in the list

{

while(key!=NULL)
    {
    printf("%d",key->theData);
    theData=theData->next;
    }

}
};

#endif // End of list header


Related Solutions

Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp,...
C++ MAIN remains same Given the complete main() function, partial playlist class header playlist.h, and playlist.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor copy constructor destructor addSong(song tune) adds a single node to the front of the linked list no return value displayList() displays the linked list as formatted in the example below no return value overloaded assignment operator A description of all of these functions is available in the textbook's...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h. // ************************************ // ************* main.cpp ************ // ************************************ #include "Schedule.h" void main() { Schedule s1(10); s1.addEntry(1,20,"Feed Cat"); s1.addEntry(2,40,"Feed Dog"); s1.addEntry(2,50,"Walk Dog"); s1.addEntry(4,0, "Fix Dinner"); s1.addEntry(5,15,"Eat Dinner"); s1.printSchedule(); Schedule s2(s1); // Note this uses the copy constructor cout << endl << "Output from s2 " << endl; s2.printSchedule(); } // ********************************************** // ************* Schedule.h ******************...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT