Build a List class, using a linked-list implementation. For basic operations it
ID: 3561440 • 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 #include using namespace std; class Node { int data; Node* next; public: Node() {}; void SetData(int aData) { data = aData; }; void SetNext(Node* aNext) { next = aNext; }; int Data() { return data; }; Node* Next() { return next; }; }; class List { Node *head; public: List() { head = NULL; }; void Display(); void Append(int data); void Remove(int data); }; void List::Display() { Node *tmp = head; if ( tmp == NULL ) { cout SetNext(tmp->Next()); delete tmp; } } int main() { List list; int n[100], i, size; char choice; for(i=1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.