Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END,

PLEASE DO NOT SPAM THE QUESTION I WILL AUTOMATICALLY DISLIKE IT (IF YOU SOLVE IT PLEASE POST A PICTURE OF IT WORKING ON YOUR END, THANKS)

In C++ Fix: Add a new method to the List ADT program that will search the list for a specified value and return its index in the list (or -1 if not found). Also, implement the Insert and Print methods. Please follow the comments in the code. There is a .cpp and .h file.

// File : List.h // Class: COP 3530 Digital Sys & Architecture // Dev : <> // Desc : Linked List ADT implementation // -------------------------------------------------------- #include "List.h" /** * Default constructor - initialize empty list */ List::List() : head {nullptr}, size{0} { } // default constructor /** * Destructor - clean up nodes */ List::~List() { // if list is not empty if (head) { // repeat until head moved past end of list while (head->next) { Node* temp = head; // hold reference to current head Node head = head->next; // move head pointer to next node in list delete temp; // delete old head Node } } // is list empty } // destructor /** * Accessor for the list size property * @return int size (empty = 0) */ int List::getSize() { return size; } // getSize /** * Signifies if the list is empty or not * @return bool True if empty (size==0) */ bool List::isEmpty() { return size == 0; } // isEmpty /** * Add a new value into the list at the head, a * specified position, or at the tail if position * is -1 or invalid position * @param value - int value to store * @param position - position in list to store value (default -1)

*/ void List::insert(int value, int position) { Node* temp = new Node{value, nullptr}; if (position < LIST_HEAD || position >= size) position = size; // determine where in list to insert if( size == 0 || position == LIST_HEAD) { temp->next = head; // new node points to head head = temp; // move head pointer to new node } else { // middle or tail of list // traverse list to node - 1 Node* pNode = traverse(position - 1); // insert node between current and next node temp->next = pNode->next; // new node points to next node pNode->next = temp; // current node points to new node } // where in list // increment size of the list size++; } // insert /** * Remove a value from the list at a specified position and * return it to the caller * @param position - position in the list or tail if -1 * or invalid position * @return - value removed from the list */ int List::remove(int position) { int value = -1; return value; } // remove /** * Return a value from the list at a specified position without * removing it * @param position - position in the list * @return - value found at position (-1 if not found or invalid position) */ int List::read(int position) { int value = -1; if (size > 0) { Node* pNode = traverse(position); value = pNode->value; } return value; } // read

/** * Modify a value at a specified position in the list * @param value - new value * @param position - position in the list */ void List::modify(int value, int position) { } // modify // Private methods // -------------------------------------------------------- /** * Iterate node pointer to specified position in the list * @param position - 0 to size-1 * @return pointer to Node at List[position] (or null if empty) */ List::Node* List::traverse(int position) { Node* pNode = head; // start at head of list (null if empty) int counter = 0; // zero indexed list // (don't move if position == 0) while(counter < position && pNode->next) { pNode = pNode->next; counter++; } // traversal return pNode;

// File : List.h // Class: COP 3530 Digital Sys & Architecture // Dev : <> // Desc : Linked List ADT interface // -------------------------------------------------------- #ifndef LINKEDLIST_LIST_H #define LINKEDLIST_LIST_H // constants // -------------------------------------------------------- const int LIST_HEAD = 0; // list position of head node const int LIST_TAIL = -1; // specify current tail position class List { private: // internal storage structure for a Node struct Node { int value; Node* next; }; Node* head; // hold reference to head of the list (Node[0]) int size; // current size of the list (empty==0) public: List(); ~List(); int getSize(); bool isEmpty(); void insert(int value, int position=LIST_TAIL); int remove(int position=LIST_TAIL); int read(int position); void modify(int value, int position); private: Node* traverse(int position); }; #endif //LINKEDLIST_LIST_H

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

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

2 The role of economic theory in economics.

Answered: 1 week ago