Build a List class, using a linked-list implementation. For basic operations it
ID: 3562068 • Letter: B
Question
Build a List class, using a linked-list implementation.
For basic operations it should have a default constructor, destructor, and the basic operations: print, insert, and delete. The list should be maintained as an ordered list, i.e., list elements are arranged in ascending order. Give the class a name List. The operations should have the following prototypes:
List(); // Default constructor
~List(); // Destructor
void Print() const; // The function that prints out all list elements, in the order they appear in the list.
void Insert(ElementType); // Insert a new element into the list. The order of elements should be maintained.
void Delete(ElementType); // Delete the first occurrence of the given element, if the element is present in the list;
// and do nothing, otherwise.
Write a driver program that tests all the functions.
Explanation / Answer
#include "ListNode.h" #include #include using namespace std; ListNode::ListNode() { } ListNode::ListNode(int value, ListNode* node){ this->value = value; this->next = node; } void ListNode::setNext(ListNode* node){ this->next = node; } void ListNode::setValue(int value){ this->value = value; } ListNode* ListNode::getNext(){ return this->next; } int ListNode::getValue(){ return this->value; } int ListNode::getLength(){ if (this != NULL){//if this is not NULL if ((*this).getNext() == 0) return 1; else{ ListNode* temp = this; int count = 0; while (temp != 0){ count++; temp = (*temp).getNext(); }//end while temp = NULL; return count; }//end else }//end if return 0; } void ListNode::replace(int position, int value){ ListNode* temp = this; if (position > -1 && positionRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.