Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Show transcribed image textOverview A set is a collection of distinct (unique) items. Using a modified version of the Doubly Linked List we discussed in

image text in transcribed

image text in transcribed

image text in transcribed

Show transcribed image textOverview A set is a collection of distinct (unique) items. Using a modified version of the Doubly Linked List we discussed in class, you will implement a Set class, and provide an interactive command loop to test the Set class. A partially implement List class is provided to you in the list h file. Some of the functions need to still be defined. Once you define those functions, you will use the list.h file to implement your Set class. You should be able to create all of the functions of the Set class by leveraging public member functions of the List class You do not need to use all of the List member functions.

set.cpp

#include

#include "list.h"

using namespace std;

class Set

{

private:

List* list;

int set_size;

public:

Set();

~Set();

bool contains(int value);

bool add(int value);

bool remove(int value);

void clear();

Set* set_union(Set&);

Set* intersection(Set&);

Set* difference(Set&);

void print();

int size() { return set_size; }

};

int main()

{

}

list.h

class ListNode

{

private:

int data;

ListNode* prev;

ListNode* next;

public:

ListNode() { prev = next = NULL; }

ListNode(int d, ListNode* p, ListNode* n) { data = d; prev = p; next

= n; }

friend class List;

};

class List

{

private:

ListNode* head;

ListNode* tail;

public:

List() { head = tail = NULL; }

~List();

bool isEmpty() { return head == NULL; }

bool contains(int value);

void addToHead(int value);

void addToTail(int value);

int removeHead();

int removeTail();

int removeAt(int index);

bool remove(int value);

int at(int index);

int valueOf(const ListNode* elem);

const ListNode* getNext(const ListNode* node);

const ListNode* getPrevious(const ListNode* node);

const ListNode* getHead() { return head; }

const ListNode* getTail() { return tail; }

};

List::~List()

{

while (!isEmpty())

removeTail();

}

bool List::contains(int value)

{

ListNode *temp = head;

while (temp != NULL && temp->data != value)

temp = temp->next;

return temp != NULL;

}

void List::addToHead(int value)

{

if (isEmpty())

{

head = tail = new ListNode(value, NULL, NULL);

}

else

{

head = new ListNode(value, NULL, head);

head->next->prev = head;

}

}

void List::addToTail(int value)

{

if (isEmpty())

{

head = tail = new ListNode(value, NULL, NULL);

}

else

{

tail = new ListNode(value, tail, NULL);

tail->prev->next = tail;

}

}

int List::removeHead()

{

int value = head->data;

if (head == tail)

{

delete tail;

head = tail = NULL;

}

else

{

head = head->next;

delete head->prev;

head->prev = NULL;

}

return value;

}

int List::removeTail()

{

int value = head->data;

if (head == tail)

{

delete tail;

head = tail = NULL;

}

else

{

tail = tail->prev;

delete tail->next;

tail->next = NULL;

}

return value;

}

int List::removeAt(int index)

{

// TODO: implement removeAt

}

bool List::remove(int value)

{

// TODO: implement remove

}

int List::at(int index)

{

// TODO: implement at

}

int valueOf(const ListNode* elem)

{

// TODO: implement valueOf

}

const ListNode* List::getNext(const ListNode* node)

{

// TODO: imoplement getNext

}

const ListNode* List::getPrevious(const ListNode* node)

{

// TODO: implement getPrevious

}

Overview A set is a collection of distinct (unique) items. Using a modified version of the Doubly Linked List we discussed in class, you will implement a Set class, and provide an interactive command loop to test the Set class. A partially implement List class is provided to you in the list h file. Some of the functions need to still be defined. Once you define those functions, you will use the list.h file to implement your Set class. You should be able to create all of the functions of the Set class by leveraging public member functions of the List class You do not need to use all of the List memtber funactions. Part of the assignment is determining Program 1 (20 points) List Finish implementing the List class (in list h) Implement a following member functions . removeAt Remove the node at the specified index. Return the int value contained at .remove : Remove the provided value if it is contained in the list. Return true if the .at: Returns the int value contained at the node at the provided index. Exit program if the now removed node. Exit program if an invalid index is provided. value was found and remove, return false if no changes were made to the list. an invalid index is provided. valueOf: Given a ListNode pointer, return the given value getNext: Given a ListNode pointer, return a pointer to its next ListNode getPrevious Given a ListNode pointer, return a pointer to its previous ListNode In the list.h file, do not modify any of the contents of the List class unless they are marked with a TODO comment. Program 2. (80 points) Set Using the list class, implement a Set class that does the following: 1. Implements the following functions: contains Retums a boolean value representing if the provided int value is contained in the list. add Returns a boolean value representing if the provided int value was successfully . remove Retuns a boolean value representing if the provided int value was clear : No return value. This function should assign 0 to setsize, and delete list. added to the list. Sets are intended to contain distinct value, so do not allow duplicate values to be added. If the list already contains the supplied value, return false. successfully removed from the list. If the value is not contained in the list, the function should return false - set union: Creates and returns a Set pointer that contains the set union of the intoking Set object and a second Set object passed into the function. Note: union is a reserved keyword in C++, which is why the identifier for this function is difference from the other two set functions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions