Question

Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...

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

};

Homework Answers

Answer #1

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;
}
Know the answer?
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
Similar Questions
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them. #include<iostream> using namespace std; /*Define the class for BST*/ class BST_TREE {    /*Structure of the tree*/    struct tree    {        int value;        tree* left;        tree* right;    };    tree* root;    /*Method to clear the tree*/    tree* Empty(tree* t)    {        if (t == NULL)       ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT