hi-i hope someone can help me..
I am creating a linked list of nodes,
I am trying to insert a new node alphabetically
by strcmp the name field.
I keep crashing at this point...
you will see in my code below, I can add to the front of the list
but my alpha insert is bad for some reason.
I've overloaded the > operator for the struct and have been trying different things for days to no avail-
any help is apreciated.
thanks
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
#define MAX 40
struct TeleEntry
{
char EntryName[40];
char EntryNumber[40];
TeleEntry* Next;
int index;
bool TeleEntry::operator> (TeleEntry* T) const {
if(T == NULL) { return false; } /* generally, its a good idea to have error checking */
if (strcmp(this->EntryName, T->EntryName) > 0)
{ return true; }
else
{ return false; }
}
};
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;
Sample->Next = Head;
Head = Sample;
return size++;
}
/*
int ListofTeles::Add(TeleEntry *NewItem)
{
TeleEntry *New, *CurrentNode, *lastNode = new TeleEntry;
New = NewItem;
if(Head == NULL){
Head = NewItem;
return size++;
}else{
CurrentNode = Head;
lastNode = NULL;
while(1){
if(strcmp(CurrentNode->EntryName,NewItem->EntryName) > 0){
if(lastNode == NULL)
Head = NewItem;
else
lastNode->Next = NewItem;
NewItem->Next = CurrentNode;
return size++;
}else{
lastNode = CurrentNode;
CurrentNode = CurrentNode->Next;
if(CurrentNode == NULL){
lastNode->Next = NewItem;
return size++;
}
}
}
}
}
*/
//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<<"\nIndex:"<< Current->index<<" "<<"Name:"<<Current->EntryName
<< " " <<"Tele #:"<< Current->EntryNumber << endl;
Current = Current->Next;
Head = Current;
}//while
}//else
}//Print
//----Utility functions----------------
//Fx Bool GetInput:client function
bool GetInput(char & cInput)
//in: none; out:cInput,return true if input 't'
{
cin >> cInput;
if ((cInput == 'Q') || (cInput == 'q'))//quit case
{
return false;
}//if
if ((cInput == 'A') || (cInput == 'a'))
return true;
if ((cInput == 'P') || (cInput == 'p'))
return true;
if ((cInput == 'S') || (cInput == 's'))
return true;
if ((cInput == 'D') || (cInput == 'd'))
return true;
else
{
cout << "Invalid Option" << endl;
return true;
}//else
}//Fx
void MenuHeader()
{
cout << "\n\n\n";
cout << "---TELEPHONE LINKED LIST---";
cout << "\n\n\n";
cout <<setw(10) << "To choose a function, enter it's letter: \n";
}
//Fx Show Menu:client function
void ShowMenu()
//in: none; out: none
{
cout << "a) Add a Phone Number to the List"<< endl;
cout << "p) Print the Entire List "<< endl;
cout << "s) Search List for an Entry " << endl;
cout << "d) Delete and Entry " << endl;
cout << "q) Q u i t " << endl;
}//Fx
//___________________________________________________
int main()
{
ListofTeles *Teles= new ListofTeles();
char Name [MAX];
char Number[MAX];
int ind = 0;
//---------------------------
// create a pointer (on the stack ) to an object of
//TeleEntry, & allocate memory dynamically to heap
TeleEntry *newEntry= new TeleEntry;
char cInputOption;
MenuHeader();
ShowMenu();
while (GetInput(cInputOption))
{
switch(cInputOption)
{
case'a':
case'A':
TeleEntry *Tele;
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);
Tele->index = ++ind;
Teles->Add(Tele);
break;
case'p':
case'P':
cout<<"Qty Telephone Numbers: " << Teles->count() << endl;
Teles->Print();
break;
case 's':
case 'S':
//cout << "\nSearching List....: \n\n";
break;
case 'd':
case 'D':
cout << "Which Entry would you like to delete?"<< endl;
cin >> Tele->index;
ind=Tele->index;
Teles->Delete(ind);
break;
case 'q':
case 'Q':
default :
break;
}//switch
cout << "\n\n";
ShowMenu();
}//while
//delete heap memory allocation
//prevent memory leaks
delete newEntry;
return 0;
}