Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 //Solution.cpp #pragma once #include 6 typedef int ListItem; 7 #define INVALID

ID: 3699233 • Letter: 1

Question

1 //Solution.cpp #pragma once #include 6 typedef int ListItem; 7 #define INVALID-LIST-ITEM (-1) 10 11 class List 13 TODO !1 15 16 17 18 19 20 21 This is the abstract class from which our list implementations will derive You should add a constructor, destructor, and the following functions unsigned int Getsize)- returns the size of the list bool AppendItem(ListItem)adds the passed-in item to the end of the list. returns whether the append succeeded bool PrependItem(ListItem)-adds the passed-in item to the beginning of the list. returns whether the prepend succeeded ListItem GetItemAt (unsigned int) returns the list item at the passed-in index or INVALID LIST ITEM for a bad index void DeleteltemAt (unsigned int) - deletes the list item at the passed-in index void Print) - prints out each item in the list from start to finish 23 24 25 26 - this is the only function in List with an actual implementation -It should print out "I'm a list. My size is #", where ?. is the size of the list, followed by a newline Note that your function signatures might require certain other keywords such as const, virtual (hint, hint), etc. In this and the following classes, make things public, protected, or private as appropriate.* 28 29 30 31

Explanation / Answer

//List.h

#pragma once

#include <stdio.h>

#include <vector>

typedef int ListItem;

#define INVALID_LIST_ITEM (-1)

class List

{

public:

List();

~List();

unsigned int GetSize();

virtual bool AppendItem(ListItem item);

virtual bool PrependItem(ListItem item);

virtual ListItem GetItemAt(unsigned int i);

virtual void DeleteItemAt(unsigned int i);

virtual void Print();

private:

std::vector<ListItem> items_;

};

class ArrayList : public List {

public:

ArrayList(unsigned int capacity);

bool AppendItem(ListItem item) override;

bool PrependItem(ListItem item) override;

ListItem GetItemAt(unsigned int i) override;

void DeleteItemAt(unsigned int i) override;

void Print() override;

private:

static unsigned int CAPACITY;

static unsigned int ITEMS_COUNT;

static ListItem* ITEMS;

};

struct Node {

ListItem data_;

Node* next_;

Node(ListItem data)

{

data_ = data;

}

};

class LinkedList : public List {

public:

Node* head;

Node* tail;

bool AppendItem(ListItem item) override;

bool PrependItem(ListItem item) override;

ListItem GetItemAt(unsigned int i) override;

void DeleteItemAt(unsigned int i) override;

void Print() override;

private:

unsigned int node_count_=0;

};

//List.cpp

#include "List.h"

#include <iostream>

#include <array>

using ::List;

using ::ArrayList;

using ::LinkedList;

List::List()

{}

List::~List()

{}

unsigned int List::GetSize()

{

return items_.size();

}

bool List::AppendItem(ListItem item)

{

auto old_size = items_.size();

items_.emplace_back(item);

return items_.size() > old_size;

}

bool List::PrependItem(ListItem item)

{

auto old_size = items_.size();

items_.insert(items_.begin(), item);

return items_.size() > old_size;

}

ListItem List::GetItemAt(unsigned int i)

{

return items_.at(i);

}

void List::DeleteItemAt(unsigned int i)

{

items_.erase(items_.begin() + i);

}

void List::Print()

{

std::cout << "I'm a list. My size is " << items_.size() << std::endl;

std::cout << "[ ";

for (const unsigned int item : items_)

{

std::cout << item + " ";

}

std::cout << " ]";

}

ArrayList::ArrayList(unsigned int capacity)

{

CAPACITY = capacity;

ITEMS = new ListItem[CAPACITY];

ITEMS_COUNT = 0;

}

bool ArrayList::AppendItem(ListItem item)

{

if (ITEMS_COUNT == CAPACITY)

return false;

ITEMS[ITEMS_COUNT] = item;

ITEMS_COUNT++;

return true;

}

bool ArrayList::PrependItem(ListItem item)

{

if (ITEMS_COUNT == CAPACITY)

return false;

for (unsigned int i = ITEMS_COUNT; i > 0; i++)

{

ITEMS[i] = ITEMS[i - 1];

}

ITEMS[0] = item;

return true;

}

ListItem ArrayList::GetItemAt(unsigned int i)

{

return ITEMS[i];

}

void ArrayList::DeleteItemAt(unsigned int i)

{

for (unsigned int start = i; start < ITEMS_COUNT-1; start++)

{

ITEMS[i] = ITEMS[i + 1];

}

ITEMS_COUNT--;

}

void ArrayList::Print()

{

std::cout << "I'm a list. My size is " << ITEMS_COUNT << std::endl;

std::cout << "[ ";

for (unsigned int i = 0; i < ITEMS_COUNT; i++)

{

std::cout << ITEMS[i] << " ";

}

std::cout << " ]";

}

bool LinkedList::AppendItem(ListItem item)

{

Node* temp = new Node(item);

if (!temp)

return false;

if (!head)

{

head = temp;

tail = temp;

}

else

{

tail->next_ = temp;

tail = temp;

}

node_count_++;

return true;

}

bool LinkedList::PrependItem(ListItem item)

{

Node* temp = new Node(item);

if (!temp)

return false;

if (!head)

{

head = temp;

tail = temp;

}

{

temp->next_ = head;

head = temp;

}

node_count_++;

return true;

}

ListItem LinkedList::GetItemAt(unsigned int i)

{

Node* tmp = head;

for (unsigned int index = 1; index <= i; index++)

{

tmp = tmp->next_;

}

return tmp->data_;

}

void LinkedList::DeleteItemAt(unsigned int i)

{

Node* tmp = head;

for (unsigned int index = 0; index < i; index++)

{

tmp = tmp->next_;

}

tmp->next_ = tmp->next_->next_;

}

void LinkedList::Print()

{

if (!head)

return;

Node* tmp = head;

std::cout << "I'm a list. My size is " << node_count_++ << std::endl;

std::cout << "[ ";

while (tmp)

{

std::cout << tmp->data_ << " ";

}

std::cout << " ]";

}