I got a question about the singly linked list for (1) inserting the new record before the users type the number of the position & (2) removing the name of the record which users type the name.
CODE
bool Database::remove(char *recName)
{
Record *pCurr, *pPrev, *rec;
pCurr = pFirst;
int i = 0;
do
{
pPrev = pCurr;
pCurr = pPrev->getNext();
i++;
}
while(pCurr!=0);
rec = pCurr->getNext();
pPrev->setNext(rec);
delete pCurr;
return true;
}
bool Database::insert(int pos, Record *rec)
{
Record *pC;
pC = pFirst;
int i = numRecord;
if(pos>= numRecord)
{
cout << "Cannot insert!!!\n";
}
else
{
while (i<pos)
{
pC = pC->getNext();
i--;
}
rec = new Record;
rec->setNext(pC->getNext());
pC->setNext(rec);
}
return false;
}
From main.cpp
CODE
switch (choice)
{
case 'i': case 'I':
{
char name1[6];
int pos1;
Record *r = new Record;
cout << "Name of Record? ";
cin >> name1;
cout << "Insert before Record#? ";
cin >> pos1;
char *NameInHeap = new char[6];
NameInHeap[0] = '\0';
strcpy(NameInHeap, name1);
r->setName(NameInHeap);
db.insert(pos1, r);
}
break;
case 'r': case 'R':
{
char name2[6];
cout << "Name of Record to be deleted?\t";
cin >> name2;
cout << endl;
char *nameInHeap = new char[6];
nameInHeap[0] = '\0';
strcpy(nameInHeap, name2);
db.remove(nameInHeap);
}
break;
How do i implement those functions?? For insertion, the users type the number for the position he/she wants to insert the new record before the position he/ahe wants.
Thank you!!
Dicky