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...
- 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.          ...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
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...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
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;...
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
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...
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.”....