Question

In: Computer Science

/* Array List Modified from CS2 Software Design & Data Structures by Cliff Shaffer. OpenDSA */...

/*
Array List

Modified from CS2 Software Design & Data Structures by Cliff Shaffer. OpenDSA

*/
#ifndef ALIST_H_RVC
#define ALIST_H_RVC
#include "List.h"

template // Array-based list implementation

class AList : public List {

private:
   ListItemType* listArray; //Dynamic Array (pointer)
   int listSize; //Current number of list items
   int curr; //Position of current element
   int MAX_SIZE;


public:

   //Constructors

   AList() {
       MAX_SIZE = 1000; // arbitrary
       listArray = new ListItemType[MAX_SIZE];
       clear();
   } //end constructor

   // Create a new list element with maximum size "MAX_SIZE" as a parameter
   AList(int m) {
       MAX_SIZE = m;
       listArray = new ListItemType[MAX_SIZE];
       clear();
   } //end constructor

   bool isEmpty() const {
       return listSize == 0;
   }

   void clear() { // Reinitialize the list
       listSize = curr = 0; // Simply reinitialize values
   }

   // Insert "it" at current position
   // return value indicates success

   bool insert(const ListItemType& it) {
       if (listSize >= MAX_SIZE) return false;

       for (int i = listSize; i > curr; i--) //Shift elements up
           listArray[i] = listArray[i - 1]; //to make room
       listArray[curr] = it;
       listSize++; //Increment list size
       return true;
   }

   // Append "it" to list
   bool append(const ListItemType& it) {
       cout << "\n\n******* This is to be completed for the lab\n\n";
       return true;
   }

   // Remove and return the current element
   ListItemType remove() {
       if (curr == listSize) curr--;

       ListItemType it = listArray[curr]; // Copy the element
       for (int i = curr; i < listSize - 1; i++) // Shift them down
           listArray[i] = listArray[i + 1];
       listSize--; // Decrement size
       if (curr > listSize) {
           curr = listSize;
       }
       return it;
   }

   void moveToStart() { curr = 0; } // Set to front
   void moveToEnd() { curr = listSize; } // Set to end
   void prev() { if (curr != 0) curr--; } // Move left
   void next() { if (curr < listSize) curr++; } // Move right
   int length() const { return listSize; } // Return list size
   int currPos() const { return curr; } // Return current position

                                           // Set current list position to "pos"
   bool moveToPos(int pos) {
       if ((pos < 0) || (pos > listSize)) return false;
       curr = pos;
       return true;
   }

   // Return true if current position is at end of the list5
   bool isAtEnd() const { return curr == listSize; }

   // Return the current element
   ListItemType getValue() const {
       return listArray[curr];
   }

   bool find(const ListItemType& it) {
       ListItemType curr =
       cout << "\n\n******* This is to be completed for the assignment\n\n";
       return true;
   }

   bool findNext(const ListItemType& it) {
       cout << "\n\n******* This is to be completed for the assignment\n\n";
       return true;
   }

   int count(const ListItemType& it) {
       cout << "\n\n******* This is to be completed for the assignment\n\n";
       return 0;
   }

};

#endif

Write the code for the find, findNext and count method in AList.h. This is an Array based list implementation problem in ADT, where we discuss about stacks


Solutions

Expert Solution

/*
Array List

Modified from CS2 Software Design & Data Structures by Cliff Shaffer. OpenDSA

*/
#ifndef ALIST_H_RVC
#define ALIST_H_RVC
#include "List.h"

template <class ListItemType>

class AList : public List

{

private:

   ListItemType* listArray; //Dynamic Array (pointer)

   int listSize; //Current number of list items

   int curr; //Position of current element

   int MAX_SIZE;

public:

   //Constructors

   AList() {

          MAX_SIZE = 1000; // arbitrary

          listArray = new ListItemType[MAX_SIZE];

          clear();

   } //end constructor

   // Create a new list element with maximum size "MAX_SIZE" as a parameter

   AList(int m) {

          MAX_SIZE = m;

          listArray = new ListItemType[MAX_SIZE];

          clear();

   } //end constructor

   bool isEmpty() const {

          return listSize == 0;

   }

   void clear() { // Reinitialize the list

          listSize = curr = 0; // Simply reinitialize values

   }

   // Insert "it" at current position

     // return value indicates success

   bool insert(const ListItemType& it) {

          if (listSize >= MAX_SIZE) return false;

          for (int i = listSize; i > curr; i--) //Shift elements up

                listArray[i] = listArray[i - 1]; //to make room

          listArray[curr] = it;

          listSize++; //Increment list size

          return true;

   }

   // Append "it" to list

   bool append(const ListItemType& it) {

          if (listSize >= MAX_SIZE) return false;

          listArray[listSize] = it;

          listSize++;

          return true;

   }

   // Remove and return the current element

   ListItemType remove() {

          if (curr == listSize) curr--;

          ListItemType it = listArray[curr]; // Copy the element

          for (int i = curr; i < listSize - 1; i++) // Shift them down

                listArray[i] = listArray[i + 1];

          listSize--; // Decrement size

          if (curr > listSize) {

                curr = listSize;

          }

          return it;

   }

   void moveToStart() { curr = 0; } // Set to front

   void moveToEnd() { curr = listSize; } // Set to end

   void prev() { if (curr != 0) curr--; } // Move left

   void next() { if (curr < listSize) curr++; } // Move right

   int length() const { return listSize; } // Return list size

   int currPos() const { return curr; } // Return current position

   // Set current list position to "pos"

   bool moveToPos(int pos) {

          if ((pos < 0) || (pos > listSize)) return false;

          curr = pos;

          return true;

   }

   // Return true if current position is at end of the list5

   bool isAtEnd() const { return curr == listSize; }

   // Return the current element

   ListItemType getValue() const {

          return listArray[curr];

   }

   //search for first occurrence in the list of an item. If found return true and set curr to the location.

   // if not found, return false and set curr to the end of the list.

   //virtual bool find(const ListItemType& newItem) = 0;

   // since it is a virtual function in the base class

virtual bool find(const ListItemType& it) {

          if(isEmpty())// list is empty

          {

                curr = 0;

                return false;

          }

          // loop over the list

          for(int i=0;i<listSize;i++)

                if(listArray[i] == it) // if ith element is it, return true

                {

                       curr = i; // set curr to index i

                       return true;

                }

          curr = listSize; // item not present, set curr to end of list

          return false;

   }

   //search for the next occurrence in the list of an item. Start with the next position after curr. If found return true and set curr to the location.

   // if not found, return false and set curr to the end of the list.

   //virtual bool findNext(const ListItemType& newItem) = 0;

// since it is a virtual function in the base class

   virtual bool findNext(const ListItemType& it) {

          if(isEmpty())// list is empty

          {

                curr = 0;

                return false;

          }

          // if current element is at the end return false

          if(isAtEnd())

                return false;

          // loop that starts from the location next to curr till the end of list

          for(int i=curr+1;i<listSize;i++)

          {

                if(listArray[i] == it) // if ith element is it, return true

                {

                       curr = i; // set current to i

                       return true;

                }

          }

          curr = listSize; // item not found, set curr to end of list

          return false;

   }

   // count the number of times a value is found in the list

   // curr should be set back to its current location after the count.

   // virtual int count(const ListItemType& newItem) = 0;

   // since it is a virtual function in the base class

   virtual int count(const ListItemType& it) {

          int freq = 0; // set freq to 0 at the start

       // loop over the list

          for(int i=0;i<listSize;i++)

                if(listArray[i] == it) // if ith element is it, increment freq

                       freq++;

          return freq;               //return freq

   }

};

#endif

//end of AList


Related Solutions

Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
        What are the goals to design a software? What is software elegance? List four (4)...
        What are the goals to design a software? What is software elegance? List four (4) software elegance criteria and simple explain them.                                                          
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular...
Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. Design microstrip circular array antenna in HFSS software and MATLAB CODE FOR IT. IF YOU KNOW THEN ONLY DO OR ELSE LEAVE FOR OTHER ANTENNA ,ELECTRICAL ENGINEERING
2. What are data elements and records? 3. List three control structures in modular design. 4....
2. What are data elements and records? 3. List three control structures in modular design. 4. Draw a picture which shows an example of iteration structure. 5. Provide an example of structured English? 6. Provide an example of decision tables with three conditions. 7. Provide an example of the decision tree.
What are the data structures and algorithms? Why are they important to software developers? Give an...
What are the data structures and algorithms? Why are they important to software developers? Give an example of using a data structure. Give an example of using algorithm. Use Google to search for your answers. Minimum 400 words and a maximum of 1000 words.
Design Rectangular array antenna in HFSS software or 5Ghz frequency and MATLAB CODE FOR IT. need...
Design Rectangular array antenna in HFSS software or 5Ghz frequency and MATLAB CODE FOR IT. need all the output waveform regarding that Design microstrip array antenna in HFSS software and MATLAB CODE FOR IT. IF YOU KNOW THEN ONLY DO OR ELSE LEAVE FOR OTHER ANTENNA ,ELECTRICAL ENGINEERING
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data...
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data structure. Your queue should fully implemnted the following methods: first, push_back (enqueue), pop_front (dequeue), size, and isEmpty. Make sure to include a driver to test your newly implemented queue
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
What's Orthogonal Array and how to design Perfect Local Randomiser from this
What's Orthogonal Array and how to design Perfect Local Randomiser from this
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT