By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Insertion and delection of linked list

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. bool Database::remove(char *recName)
  2. {
  3.     Record *pCurr, *pPrev, *rec;
  4.     pCurr = pFirst;
  5.     int i = 0;
  6.     do
  7.     {
  8.         pPrev = pCurr;
  9.         pCurr = pPrev->getNext();
  10.         i++;
  11.     }
  12.     while(pCurr!=0);
  13.     rec = pCurr->getNext();
  14.     pPrev->setNext(rec);
  15.     delete pCurr;
  16.     return true;
  17. }
  18. bool Database::insert(int pos, Record *rec)
  19. {
  20.     Record *pC;
  21.     pC = pFirst;
  22.     int i = numRecord;
  23.     if(pos>= numRecord)
  24.     {
  25.         cout << "Cannot insert!!!\n";
  26.     }
  27.     else
  28.     {
  29.         while (i<pos)
  30.         {
  31.             pC = pC->getNext();
  32.             i--;
  33.         }
  34.         rec = new Record;
  35.         rec->setNext(pC->getNext());
  36.         pC->setNext(rec);
  37.     }
  38.     return false;
  39. }
From main.cpp
Expand|Select|Wrap|Line Numbers
  1. switch (choice)
  2.         {
  3.         case 'i': case 'I':
  4.             {
  5.                 char name1[6];
  6.                 int pos1;
  7.                 Record *r = new Record;
  8.                 cout << "Name of Record? ";
  9.                 cin >> name1;
  10.                 cout << "Insert before Record#? ";
  11.                 cin >> pos1;
  12.                 char *NameInHeap = new char[6];
  13.                 NameInHeap[0] = '\0';
  14.                 strcpy(NameInHeap, name1);
  15.                 r->setName(NameInHeap);
  16.                 db.insert(pos1, r);
  17.             }
  18.             break;
  19.         case 'r': case 'R':
  20.             {
  21.                 char name2[6];
  22.                 cout << "Name of Record to be deleted?\t";
  23.                 cin >> name2;
  24.                 cout << endl;
  25.                 char *nameInHeap = new char[6];
  26.                 nameInHeap[0] = '\0';
  27.                 strcpy(nameInHeap, name2);
  28.                 db.remove(nameInHeap);
  29.             }
  30.             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
Feb 23 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
Basically, assuming the list isn't sorted in any way, if the person selects n you would start at the first node and jump to the next element n times. Create a new node and set it's nextNode to point to the currentNode's nextNode. then Set the currentNode's nextNode to point to the newly created node:

a-b-c-d-e

we want to insert f after b, so we skip to b and create f.
f.next = b.next (which will be c)
b.next =f;

a-b-f-c-d-e
Feb 23 '07 #2

P: 2
But, the screen shows the strange charcters...............how do i get rid of the strange characters??
Feb 23 '07 #3

DeMan
100+
P: 1,806
If you try to print a structure it may not print exactly what you want unless you print each element explicitly. Furthermore, if you try to print a pointer you will print a pointer, make sure you are printing the value at thepointer
Feb 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.