hey all,
i'm a newbie to C++ programming and i'm having some difficulties implementing this singly linked list.
what it has to do is accept:
nickname
email address
number of kills
and store them in a node of the linked list in order according to number of kills.
here's what i've got, it seems to accept the number of kills, but i run into problems when i try to accept anything else.
#include<iostream.h>
#include<process.h>
class Soldier
{
public :
Soldier * Sort(Soldier *);
void Show(Soldier *);
private:
Soldier * ptrNext;
int kills;
int sorter;
char nickname[25];
};
Soldier * Soldier::Sort(Soldier* temp)
{
Soldier * ptrNew;
Soldier* dummy1;
ptrNew = new Soldier;
cout << "\nEnter soldiers nickname\n";
cin >> nickname;
cout << "\nEnter number of kills for the month:\n";
cin >> sorter;
dummy1 = temp;
if (temp == NULL || dummy1->kills > sorter)
{
ptrNew->kills = sorter;
ptrNew->ptrNext=temp;
temp=ptrNew;
return temp;
}
else
{
Soldier * ptrCurrent;
Soldier * dummy;
ptrCurrent = temp;
dummy = ptrCurrent->ptrNext ;
while(ptrCurrent->ptrNext!=NULL)
{
if(dummy->kills > sorter)
{
ptrNew->ptrNext = ptrCurrent->ptrNext;
ptrNew->kills = sorter;
ptrCurrent->ptrNext = ptrNew;
return temp;
}
dummy = dummy->ptrNext ;
ptrCurrent = ptrCurrent->ptrNext;
}
ptrNew->kills = sorter;
ptrNew->ptrNext=NULL;
ptrCurrent->ptrNext=ptrNew;
}
return temp;
}
void Soldier::Show(Soldier *temp)
{
while(temp!=NULL)
{
cout << nickname << temp->kills << endl;
temp = temp->ptrNext;
}
}
void main()
{
Soldier *first=NULL,l1;
int choice;
while(1)
{
cout<<"-> [1] Insert Soldier \n-> [2] View Soldiers \n-> [3] Exit\n";
cin>>choice;
switch (choice)
{
case 1:
first=l1.Sort(first);
break;
case 2:
l1.Show(first);
break;
case 3:
exit(0);
}
}
}