Code:#include <iostream> #include <vector> //#include <list> using namespace std; struct Node { int data; Node *next; Node(int d, Node *n) : data(d), next(n) {} }; class List { public: List() { head = new Node(int(), NULL); //cout << "int() is: " << int() << endl; } void Add(int dataX) { Node *it = head; while(it->next != 0)// && it->next->data < dataX) { it = it->next; //cout << it->data << endl; } //cout << "it->data is: " << it->data << endl; it->next = new Node(dataX, it->next); head = it; } bool Count(int y) { //cout << vTail[-1] << endl; return 1; } void RemoveAll(int z) { Node *it = head; cout << "head->next is right now: " << head->data << endl; cout << "it->next is right now: " << it->data << endl; while(it->next != NULL) // && it->next->data < z) { // cout << "howdy" << endl; it = it->next; // cout << "howdy " << it->next << endl; } //if(head->next != 0) //{ //Node *it = head->next; // Node *hold = it->next; // it->next = hold->next; // delete hold; // while(it != 0) // { //cout << "now it's: " << it->data << "->" << endl; // it = it->next; // } //} if(it->next != NULL) { cout << "yes this works" << endl; Node *hold = it->next; it->next = hold->next; //cout << "hold->next is: don't expect to print->: " << hold->next << endl; delete hold; //cout << "hold->next is now: " << hold->data << endl; //cout << "while remove, it->data is: " << it->data << endl; } //cout << "after remove, it->data is: " << it->data << endl; } int Contains(int b) { return 1; } void walk() { //cout << "head->next in walk is: " << head->data << endl; if(head->next != 0) { Node *it = head->next; while(it != 0) { cout << it->data << "->"; it = it->next; //cout << it->data << "->"; } } } private: Node *head; Node *tail; //List *pNext; vector<int> vHead; vector<int> vTail; }; int main() { List list; //Node *lptr; //int match; //int search; //Node *list_start = new Node = list.Add(search); //list.FindEntry(lptr, match); //cout << list.Count(9) << endl; //this should print 0 list.Add(7); list.Add(8); list.Add(9); list.Add(10); //list.walk(); list.RemoveAll(7); list.RemoveAll(8); list.RemoveAll(9); //list.walk(); //list.Add(7)->head; //lptr = list.FindEntry(list_start, match); /*if(lptr != 0) cout << "Found: " << lptr->data << endl; else cout << "Not found!" << endl;*/ //cout << list.Add() << endl; /*if(list.Contains(12)) cout << "Found 12" << endl; //this should print else cout << "Error" << endl; if(!list.Contains(15)) cout << "Did not find 15" << endl; //this should print else cout << "Error" << endl;*/ //for(int dataX = 0; dataX < 10; dataX++) // list.Add(dataX); //cout << list.Count(7) << endl; //should print 2, since there are 2 7's in the list list.RemoveAll(0); //cout << list.Count(7) << endl; //should print 0 return 0; }
in class "List", and member function "RemoveAll"- it seems that I'm Nulling out it->next for some reason, but I'm not sure exactly why.
How could I modify this code to delete the integers in the list?
Thanks



LinkBack URL
About LinkBacks


