473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Insertion and delection of linked list

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
3 1856
DeMan
1,806 1GB
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
But, the screen shows the strange charcters...............how do i get rid of the strange characters??
Feb 23 '07 #3
DeMan
1,806 1GB
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

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

Similar topics

3
by: Jochus | last post by:
Hi! Today we saw the information about lists. We have an assignment about CGI. That's all going well, but we're stuck at the insertion sorft of linked lists -- void voeg_in_lijst(const char*...
7
by: OMouse | last post by:
Hi, I just switched to using STL for my linked lists and obviously I need a way to insert. I have all the necessary includes (list & algorithm) and the other functions that I've used (erase & find)...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
3
by: chai | last post by:
I am trying out a program to insert an element to a sorted list(singly linked list)without using the temporary variable.Is there a solution for this problem?
3
by: Franco Perilli | last post by:
I've compiled this code and no problems, but when I run the program, it prints only the last entry i've inserted. Looks like a problem in the sorted insertion algorithm. Can u help me plz? ...
6
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: ...
2
by: shahrukh | last post by:
Plz If Anyone Can Tell Me Insertion And Deletion In Linked List And In Array Then Reply Here There Programs. Even Reply Any One Program Either For Linked List And Either For Array.
3
by: Suyash Upadhyay | last post by:
Hello All, I am a beginner of C Programming, I am working on linked list now-a-days, i have successfully created a linked list and displayed, but when i tried to insert an element in mid of linked...
5
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.