Hey all,
I'm including my telephone linked list code.
I am having major problems trying to insert (or add)
my new node in the appropriate alphabetical order.
I tried to overload the > op to compare
the first letters of the names -enterd last,first.
but i keep crashing when the print function calls.
(I know the menu can look better, I am just trying to get func-tionality down first).
I suspect what I am trying to do by inserting alphabetically
will also have something to do with my searchfor function?
any comment on that would be helpful.
thanks very much!
Michele
Code:#include<iostream> using namespace std; #define MAX 40 struct TeleEntry { char EntryName[40]; char EntryNumber[40]; bool TeleEntry::operator > (const char* &TeleEntryA) const { if (strcmp(this->EntryName, TeleEntryA) > 0) {return true;} else {return false;} } TeleEntry* Next; }; class ListofTeles { public: ListofTeles(); int count(); TeleEntry* Head; int Add(TeleEntry* Item); TeleEntry *Retrieve(int pos); bool Delete(); bool Delete(int pos); void Print(); private: int size; }; //constructor__________________________________ ListofTeles::ListofTeles():size(0), Head(NULL) { } //initialize count by size_____________________ int ListofTeles::count() { return size; } //ADD_____________________________________________ int ListofTeles::Add(TeleEntry *NewItem) { TeleEntry *Sample = new TeleEntry; Sample = NewItem; if (Head==NULL) { Head = Sample; }//if else { while((Sample->EntryName) > (Head->EntryName)) { Head = Sample->Next; }//while Head = Sample; } return size++; }//add //ITEM RETRIEVAL__________________________________ TeleEntry *ListofTeles::Retrieve(int Position) { TeleEntry *Current = Head; for (int i=0; i<Position && Current !=NULL; i++) { Current=Current->Next; } return Current; } //ITEM DELETION______________________________________ bool ListofTeles::Delete() { if (Head == NULL) { std::cout<<"The list is empty\n"<< endl; return false; } else { TeleEntry *Current; Current = Head->Next; Head->Next=Current->Next; size--; return true; } } //ITEM DELETION WITH ARGUEMENT__________________________ bool ListofTeles::Delete(int position) { if (Retrieve(position) == NULL) return false; else { Retrieve(position -1)->Next = Retrieve (position +1); size--; return true; } } //PRINT LIST void ListofTeles::Print() { cout << "Print Telephone List" << endl; if (Head == NULL) { std::cout<<"The list is empty\n"<< endl; } else { TeleEntry *Current; Current = Head; while (Head!=NULL) { cout<<"\nName: "<<Current->EntryName << " " <<"Tele #: "<< Current->EntryNumber << endl;; Current = Current->Next; Head = Current; }//while }//else }//Print //___________________________________________________ int main() { ListofTeles *Teles= new ListofTeles(); TeleEntry *Tele; char Name [MAX]; char Number[MAX]; bool b; cout << "would you like to enter a name/number?"<< endl; cout << "enter 1 for yes 0 for no" << endl; cin >> b; while(b==1){ cout << "Enter Name (Last,First) :"<< endl; cin >> Name; cout << "Enter Number :"<< endl; cin >> Number; Tele = new TeleEntry; strcpy (Tele->EntryNumber, Number); strcpy (Tele->EntryName, Name); Teles->Add(Tele); cout << "would you like to enter a name/number?"<< endl; cout << "enter 1 for yes 0 for no" << endl; cin >> b; } cout<<"Qty Telephone Numbers: " << Teles->count() << endl; Teles->Print(); return 0; }



LinkBack URL
About LinkBacks


