#include "list.h" using namespace std; // Constructor for Class List List::List() {last = first = 0; cout << "allocated List " << this << endl; } List::~List() {cout << "deallocated List " << this << endl;} void List::Printout() // Printing out the contents of a List { ListNode *ptr; ptr = first; while(ptr) { cout << ptr->GetData() << " Listode " << ptr << endl; ptr = ptr->GetNext(); } return; } void List::Readin() // Reading in the contents of a List { int contents; cout << "Input a List. one element per line and ending with a line with just a zero" << endl; cin >> contents; while (contents) {this->InsertSorted(contents); cin >> contents; } } void List::InsertAfterNode(ListNode *p, int x) // Insert a ListNode after // certain ListNode in a List { ListNode *t = new ListNode(x); // create and initialize new node // insert after p t->SetNext( p->GetNext() ); p->SetNext( t ); if (last == p) last=t; } void List::InsertFront(int x) // Insert a ListNode at the // beginning of a List { ListNode *t = new ListNode(x); // create and initialize new node t->SetNext( first ); if (last == 0) last = t; first = t; } void List::InsertSorted(int x) { // Insert a ListNode with data value x ListNode *p = first; if (p == 0) {InsertFront(x); return;} if (x == p->GetData()) return; if (x < p->GetData()) {InsertFront(x); return;} while (p->GetNext()) { if (p->GetNext()->GetData() == x) return; if (p->GetNext()->GetData() > x) {InsertAfterNode(p,x); return;} p = p->GetNext(); } InsertAfterNode(p,x); } void List::Erase() { ListNode *ptr, *tempptr; ptr = first; while(ptr) { tempptr=ptr->GetNext(); delete(ptr); ptr=tempptr; } last = first = 0; }