In: Computer Science
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
// 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