1. Engineering
  2. Computer Science
  3. implement a linkedlist class that creates and manipulates a linked...

Question: implement a linkedlist class that creates and manipulates a linked...

Question details

Implement a LinkedList class that creates and manipulates a linked list of Items, defined as

class Item {
private:
    const char *l;
    int key;
    
public:
        Item(int k, const char*s){
            key = k;
            l = s;
        }
        int getKey(){
            return key;
        };
        const char * getl(){
            return l;
        };
        Item(const Item &other){
            key = other.key;
            l = other.l;
        };

. Implement the class in a file named LinkedList.cpp. Also copy the code into this file, as insurance.

Your LinkedList class must include the following:

  • [3 pts] Public default constructor that creates an empty list.

  • [3 pts] Public copy constructor that creates a copy of the list. (NOTE: The copy must include new list nodes -- it can't just point to the same nodes in this list.)

  • [4 pts] Public destructor that deletes all the nodes of the list.

  • [4 pts each] Public methods:

    • int length() -- returns the number items in the list (int)

    • void append(const Item&) -- adds a copy of the Item to the end of the list (no return value)

    • bool remove(Item&) -- removes the first item of the list and stores it into the reference passed in; returns true if item is removed, false if list was empty

    • bool empty() -- returns true if list is empty, false if not

    • bool insert(const Item&, int) -- adds a copy of the Item to the list at the specified position; returns true if successfully added, false if list was too small (e.g., insert at position 5, but the list only has 2 items)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution