Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations.
// Define a structure to use as the list item
struct ListItem {
int key;
int Data;
};
#define MAX_SIZE 50 // Define maximum length of the list class
UnsortedArray {
private:
int head; // Index to head of the list
ListItem theList[MAX_SIZE]; // The list
public: UnsortedArray(); // Class constructor
~ UnsortedArray(); // Class destructor
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
};
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
#include <iostream>
struct ListItem
{
int key;
float Data;
};
using namespace std;
#define MAX_SIZE 50
class UnsortedArray
{
private:
int head;
ListItem theList[MAX_SIZE];
public:
UnsortedArray()
{
head = -1; //initially list is empty
}
~UnsortedArray();
void ClearList()
{
head = -1; //initilasing head to -1 for showing list empty
}
bool insert(int key, float f)
{
if (isFull())
{ //if list is full then false
return false;
}
head = head + 1; //else increase head counter and store value
theList[head].key = key;
theList[head].Data = f;
return true;
}
bool Delete(int key)
{
if (isEmpty())
{
return false;
}
int i = 0;
while (i < head && key != (theList[i].key)) //checking for key
i++;
if (i > head) //check if key is not found then i will pass over the head value
{
return false;
}
else
{
for (int j = i; j < head; j++) //shifting the array element to the left
{ //after removing the particular element
theList[j] = theList[j + 1];
}
head = head - 1;
}
return true;
}
bool Search(int key, float *retVal)
{
int i = 0;
while (i < head && key != (theList[i].key)) //checking for key
i++;
if (i > head) //check if key is not found then i will pass over the head value
{
return false;
}
else
{
*retVal = theList[i].Data; //if found store it in retVal
}
return true;
}
int ListLength()
{
return head + 1;
}
bool isEmpty()
{
if (head == -1)
return true;
return false;
}
bool isFull()
{
if (head >= MAX_SIZE)
return true;
return false;
}
void PrintList()
{
int flag = 0;
for (int i = 0; i <= head; i++)
{ //displaying the list
cout << theList[i].key << " " << theList[i].Data << "\n";
flag = 1;
}
if (flag == 0)
cout << "No item is present";
cout << "\n";
}
};
int main()
{
float f = 9.3;
UnsortedArray *theList;
theList = new UnsortedArray();
theList->insert(2, 1.2f);
theList->insert(6, 5.5f);
theList->insert(8, 9.3f);
cout << "the list item are below\n";
theList->PrintList();
theList->Delete(6);
cout << "the list item after deletion are below\n";
theList->PrintList();
cout << "for checking ehter element found: ";
cout << theList->Search(8, &f);
cout << "\ntell whether list is full: ";
cout << theList->isFull() << "\n";
cout << "\ntell whether list is empty: ";
cout << theList->isEmpty() << "\n";
cout << "\nthe length of list: ";
cout << theList->ListLength() << "\n";
cout << "\nClearing the list";
theList->ClearList();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.