Question

In: Computer Science

LinkedList.h: /*-- LinkedList.h -------------------------------------------------------------- This header file defines the data type List for processing lists. Operations...

LinkedList.h:

/*-- LinkedList.h --------------------------------------------------------------

This header file defines the data type List for processing lists.
Operations are:
Constructor
Destructor
Copy constructor
Assignment operator
insert:   Insert an item
erase:    Remove an item
getListElement: get a range of list elements

-------------------------------------------------------------------------*/

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

class LinkedList
{
public:
typedef int ListElement;
private:
    /******** Data Members ********/
    class Node
    {
    public:
        ListElement data;

        Node * next;
    };
    Node *first;         // pointer to first element in linked list
    int getListSize() const;

public:
    /******** Error Codes ********/
    static const int ILLEGAL_LIST_POSITION = -1;
    static const int NO_ERROR = 0;

    LinkedList();
    ~LinkedList();
    LinkedList(const LinkedList & origList);
    const LinkedList & operator=(const LinkedList & rightHandSide);
    int insert(ListElement item, int pos);
    int erase(int pos);
    int getListElement(int posStart, int posEnd, ListElement rv[]) const;

}; //--- end of List class

#endif

LinkedList.cpp:

/*-- LinkedList.cpp-----------------------------------------------------------

This file implements List member functions.
-------------------------------------------------------------------------*/

#include <new>
#include <iostream>
#include "LinkedList.h"

//--- Definition of class constructor
// Construct a List object.
// Precondition: None.
// Postcondition : An empty List object is constructed; first == nullptr


//--- Definition of class destructor
// Destroys a List object.
// Precondition : The life of a List object is over.
// Postcondition : The memory dynamically allocated to the linked list is now deallocated


//--- Definition of copy constructor
// Construct a copy of a List object.
// Precondition: A copy of origList is needed; origList is a const
// reference parameter.
// Postcondition: A copy of origList has been constructed.


//--- Definition of assignment operator
// Assign a copy of a List object to the current object.
// Precondition: rightHandSide List is required.
// Postcondition : A copy of rightHandSide has been assigned to this
// object. A const reference to this list is returned.

//--- Definition of insert()
// Insert item at pos position.pos 0 is the first element position in the list
// Precondition : A constructed list, either empty or with elements
// Postcondition : inserted item into list at pos position
// Returns ILLEGAL_LIST_POSITION for insert that is out of range of the current list,
// Otherwise return a NO_ERROR.


//--- Definition of erase()
// Erase item at pos position.pos 0 is the first element position in the list.
// Precondition: A constructed list, either empty or with elements
// Postcondition : erased item at pos position
// Returns ILLEGAL_LIST_POSITION for erase that is out of range of the current list,
// Otherwise return a NO_ERROR.

//--- Definition of getListElement()
// Returns list values
// Precondition : A constructed list, either empty or with elements.
// The rv[] array must be large enough to hold the returned contents.
// Postcondition : Fills array rv with the list elements specified
// Returns ILLEGAL_LIST_POSITION for move that is out of range of the current list,
// Otherwise return a NO_ERROR. Both posStart and posEnd must be valid positions
// and posStart <= posEnd.posStart is an index to the start of the data and
// posEnd is an index to the end of the data.To retieve one element
// posStart and posEnd will be the same value.
int LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const
{
    if (posStart < 0 || posStart >= getListSize() || posEnd < 0 || posEnd >= getListSize() || posStart > posEnd)
    { // check for valid paramenters
        return ILLEGAL_LIST_POSITION;
    }
    Node *current = first;   // point to zero node
    for (int i = 0; i < posStart; i++)
    { // loop to find the node
        current = current->next;
    }
    for (int i = 0; i < ((posEnd - posStart) + 1); i++)
    { // put returned elements in the beginning of the list
        rv[i] = current->data;
        current = current->next;
    }
    return NO_ERROR;
}

//--- Definition of getSize()
// returns the size of the list. Do not change this.
int LinkedList::getListSize() const
{
    int rv = 0;
    Node *current = first;
    for (; current != nullptr;)
    {
        rv++;
        current = current->next;
    }
    return rv;
}

Solutions

Expert Solution

LinkedList.h:

#ifndef LINKED_LIST_H

#define LINKED_LIST_H

class LinkedList

{

public:

typedef int ListElement;

private:

/******** Data Members ********/

class Node

{

public:

ListElement data;

Node * next;

};

Node *first; // pointer to first element in linked list

int getListSize() const;

public:

/******** Error Codes ********/

static const int ILLEGAL_LIST_POSITION = -1;

static const int NO_ERROR = 0;

LinkedList();

~LinkedList();

LinkedList(const LinkedList & origList);

const LinkedList & operator=(const LinkedList & rightHandSide);

int insert(ListElement item, int pos);

int erase(int pos);

int getListElement(int posStart, int posEnd, ListElement rv[]) const;

}; //--- end of List class

#endif

LinkedList.cpp:


#include <new>

#include <iostream>

#include "LinkedList.h"

int LinkedList::getListElement(int posStart, int posEnd, ListElement rv[]) const

{

if (posStart < 0 || posStart >= getListSize() || posEnd < 0 || posEnd >= getListSize() || posStart > posEnd)

{ // check for valid paramenters

return ILLEGAL_LIST_POSITION;

}

Node *current = first; // point to zero node

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

{ // loop to find the node

current = current->next;

}

for (int i = 0; i < ((posEnd - posStart) + 1); i++)

{ // put returned elements in the beginning of the list

rv[i] = current->data;

current = current->next;

}

return NO_ERROR;

}

int LinkedList::getListSize() const

{

int rv = 0;

Node *current = first;

for (; current != nullptr;)

{

rv++;

current = current->next;

}

return rv;

}



Related Solutions

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
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...
Using C++ In a separate header file: Create a new type called "Patient" - you must...
Using C++ In a separate header file: Create a new type called "Patient" - you must use a  class. Give your "Patient" type at least five (5) member elements of your choosing, and at least one member function. You should have member elements to hold patient name, and visitReason (which can change), and other items of your choosing. In your cpp file: Create at least one instance of Patient type (example: CurrentPatient ). Create a menu-driven program OF YOUR OWN DESIGN...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
For each of the following file processing operations, indicate whether a sequential file, indexed random file, virtual storage access method, hashing,
For each of the following file processing operations, indicate whether a sequential file, indexed random file, virtual storage access method, hashing, or pointer structure would work best. You may choose as many as you wish for each step. Also, indicate which would perform the least optimally.a. Retrieve a record from the file based on its primary key value.b. Update a record in the file. c. Read a complete file of records. d. Find the next record in a file. e....
Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. (ex. structType *arr[size];) Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program...
Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program will then print...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
Discuss the historical development and of the evolution of file system data processing starting from the...
Discuss the historical development and of the evolution of file system data processing starting from the file-based system of the past to today. Make sure you explain why file systems were developed. Identify which event you believe is the most important. Briefly describe how your file system works. Be sure to respond to at least one of your classmates’ posts.
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT