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...
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....
1. Write a program that will do file processing. 2. First, create a data file (A7_in.txt)....
1. Write a program that will do file processing. 2. First, create a data file (A7_in.txt). It can contain any number (no fewer than 3) integers, each integer on it’s own line. 3. Your program will read and sum each integer until the EOF. 4. Your program will then find the average of the integers and output the average to another file (A7_out.txt). c++
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...
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.
The file SAT Excel data lists the average high school student scores on the SAT exam...
The file SAT Excel data lists the average high school student scores on the SAT exam by state. There are three components of the SAT: Critical reading, math and writing. These components are listed in Excel. Also, the sum of all 3 components (The Combined column) is listed. The percentage of all potential students who took the SAT is listed by state. Use Excel to help you answer the following questions. Find the mean, median, and mode for each of...
List ALL the different type of network connections involved in the operations for each of the...
List ALL the different type of network connections involved in the operations for each of the scenarios below. Provide an illustration for each of the scenarios. Scenarios You have just bought a new mobile phone from an online shop. You have decided to take a few snapshots of your best friend and send them to the email account of a mutual friend across the country. You are driving around in an unfamiliar city and have just gotten lost. By using...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name:...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name: Andy, classes: ET580 MA471 ET574 name: Tim, classes: ET574 MA441 ET575 name: Diane, classes: MA441 MA471 ET574 name: Lucy, classes: ET574 ET575 MA471 name: Steven, classes: ET574 MA441 ET580 b) Implement a dictionary of courses and set each courses enrollment to 0: ET580: 0 ET574: 0 ET575: 0 MA441: 0 MA471: 0 c) Use a loop and if statements to read class data from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT