Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,807 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,785 people online right now. Registration is fast and FREE... Join Now!




Insertion and delection of linked list

 
Reply to this topicStart new topic

Insertion and delection of linked list

dickylau
22 Feb, 2007 - 11:26 PM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 2


My Contributions
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
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Insertion And Delection Of Linked List
23 Feb, 2007 - 01:27 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
I don't really understand. Your code seems to work upon inspection. Is there a problem with it?
User is offlineProfile CardPM
+Quote Post

dickylau
RE: Insertion And Delection Of Linked List
23 Feb, 2007 - 02:03 AM
Post #3

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 2


My Contributions
QUOTE(NickDMax @ 23 Feb, 2007 - 02:27 AM) *

I don't really understand. Your code seems to work upon inspection. Is there a problem with it?



For the implementation of the insertion function, the new record's just only inserted after the position that the users want to.............not before Record#............
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Insertion And Delection Of Linked List
23 Feb, 2007 - 10:20 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
OH! Well that can't be much more than a logic step or two away from the code that you have.

I think that:
CODE
bool Database::insert(int pos, Record *rec)
{
    Record *pC;
    pC = pFirst;
    int i = numRecord;
//Insert a new if statment to see if pos <= 0    
   if (pos <= 0)
   {
        cout << "Cannot insert before " << pos << ".\n";
   } else
   {
        if(pos>= numRecord)
        {
            cout << "Cannot insert!!!\n";
        }
        else
        {
            //Tada....
            while (i<pos-1)
            {
                pC = pC->getNext();
                i--;
            }
            rec = new Record;
            rec->setNext(pC->getNext());
            pC->setNext(rec);
        }
    }
    return false;
}


All we need to do is change the pos to pos -1 I think... that should back it up onel.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 08:07AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month