473,725 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need a help in linked list

4 New Member
Hello,

am a new member and this is my first posting. C++ is the first progrsaming language am taking and is just for 3 months. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the coding for my program




Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>        //Creating a dynamic linked list 
  2. #include <iomanip.h>    
  3.  
  4. #include <string.h>    
  5. #include <stdlib.h>    
  6.  
  7. struct node {
  8.  
  9.     char name[20];
  10.     int id_num;
  11.     node *nextadd;
  12. };
  13.  
  14. void insert(node **);            //function prototype
  15. void display( node * );    
  16. void del(node **);                //function prototype
  17.  
  18.  
  19. node *list;
  20.  
  21. main()
  22. {
  23.     int i,option;
  24.     char ans;
  25.     bool cont = true;
  26.                 //two pointers to structure
  27.  
  28.  
  29.  
  30.          //get a pointer to the first structure
  31.  
  32.     list = NULL;
  33.  
  34.  
  35.  
  36.  
  37.  
  38.     //insert the current structure and create the remaining structures
  39.     do {
  40.     system("cls");
  41.     cout<<"\n\n1 : Add record";
  42.     cout<<"\n\n2 : Delete record";
  43.     cout<<"\n\n3 : Display record";
  44.     cout<<"\n\n4  : Exit";
  45.  
  46.     cout<<"\n\n\nEnter your option: ";
  47.     cin>>option;
  48.     cin.get();
  49.  
  50.     switch (option) {
  51.  
  52.     case 1:  insert(&list);
  53.              cont = true;
  54.              break;
  55.     case 2:  del(&list);
  56.              cont = true;
  57.                 break;
  58.     case 3: display(list);
  59.              cont = true;
  60.                 break;
  61.     case 4: exit(1);
  62.  
  63.             cont = false;
  64.                 };
  65.  
  66.     }while(true);
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  void insert(node **list)                    //record is pointer to a structure//modify the add function to insert at the back.)
  73.  {
  74.  
  75.      node *newrec = new node;
  76.  
  77.  
  78.      cout<<"Enter the a name: ";        //inserting infronth of a list
  79.      cin.getline(newrec->name,20);
  80.      cout<<"Enter the id  number: ";
  81.      cin>>newrec->id_num;
  82.      cin.get();
  83.  
  84.         newrec->nextadd = NULL;
  85.  
  86.  
  87.         if( list== NULL)
  88.  
  89.             *list = newrec;
  90.  
  91.         else
  92.         {
  93.         newrec->nextadd = *list;
  94.         *list = newrec;
  95.         }  
  96.   }
  97.  
  98.  
  99.  
  100. void display(node *contents)
  101.  
  102. {
  103.  
  104.    if (contents == NULL)
  105.        cout<<"\n\nList is empty"<<endl;
  106.    else
  107.    {
  108.  
  109.     while(contents != NULL)
  110.     {
  111.         cout<<setw(30)<<contents->name
  112.             <<setw(20)<<contents->id_num<<endl;
  113.         contents = contents->nextadd;
  114.     }
  115.    }
  116.  
  117.    cin.get();
  118. }
  119.  
  120.  
  121.  
  122. void del(node **record) // correct the function to stop hanging when an item that is not in the list is deleted
  123. {
  124.     char name[20];
  125.  
  126.     node *current, *previous;
  127.  
  128.     current = *record;
  129.  
  130.  
  131.     cout<<"\n\nEnter name to delete: ";
  132.     cin.getline(name,20);
  133.  
  134.  
  135.          if ( strcmp(current->name,name) == 0)    //if the target is the first node
  136.     {
  137.             *record = current->nextadd;
  138.  
  139.             delete(current);
  140.         }
  141.  
  142.     else 
  143.     {
  144.  
  145.  
  146.     while ( strcmp(current->name,name) != 0)
  147.     {
  148.         previous = current;
  149.         current  = current->nextadd;
  150.  
  151.     }
  152.  
  153.        previous->nextadd = current->nextadd;
  154.        cout<<"\n\nDeleted";
  155.        delete(current);
  156.     }       
  157. }
Mar 22 '07 #1
1 1627
Ganon11
3,652 Recognized Expert Specialist
In your del() function, you are looping while the item has not been found by using strcmp() - you can add the condition

Expand|Select|Wrap|Line Numbers
  1. && current node isn't NULL
to keep it from trying to check past the end of the list.
Mar 22 '07 #2

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

Similar topics

6
4601
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any potential pitfalls I'd be extremely grateful. Cheers Steve
4
2931
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows traversal only in one direction, I would say that using appropriate loops/recursion, traversal in opposite direction is also possible. Then why the need for doubly linked list? --
0
1647
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes that carry the same type of data in some of their node fields and some don't. In the various nodes , the struct datatype, there is a flag set to 1 for items that I need to remove. In the removing condition I have to add another match with an...
13
2189
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
19
2079
by: ash | last post by:
hi friends, i have some questions whch is in my last year question papers.i need some help to get logic of these questions. 1) write a C function, that takes two strings as arguments and returns a pointer to the first occurrence of 1st string in 2nd string or NULL if it is not present. -- i tried to solve it but it seems that i am not understanding this question at all.i am taking this question as:
5
3370
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
1
3332
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells). Here is the exact problem specifications: Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
6
5778
by: sandy | last post by:
I am creating a class (or so I hope) which is to be a JobCollection, linked list of 'Job'. Job is another class already created. JobCollection is just the linked list manager. I have to have this separated into a .h and .cpp file. The code I am attempting to put into the class is code that I have used before, but simply put into the same file/class with the rest of the application. My header looks like this: <code>
4
1159
by: teddarr | last post by:
OK, I'm wrking on this program. I need to create a linked list using pointers and dynamic variables. I have to read integers from a file...traverse the list...input an integers from the keyboard...then delete every number that is larger than the one from the keyboard....Then display all the remaining integers. Here's what I have: #include <iostream> #include <iomanip> #include <fstream> using namespace std;
0
8749
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9398
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8079
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4776
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2626
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2153
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.