473,408 Members | 2,444 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,408 software developers and data experts.

Deleteing Duplicate Nodes in a Linked List

twillie
Hello,

I have a linked list in C++ and I am trying to remove all duplicate nodes from the list. The code below is what our prof. wrote on the board in pusdo code in class, yet it doesn't seem to be working correctly for me. I also need to preface that the list is not sorted. Prior to entering this part of the code we need to link two preexising lists together. The directions do not say to sort the two list once they are put together.

Any advice would be helpful.

Thanks

Expand|Select|Wrap|Line Numbers
  1. r = first;
  2.     while ( r != nil )
  3.     {
  4.         target = (*r).info;
  5.  
  6.         q = r;
  7.         p = (*q).next;
  8.  
  9.         while ( ((*p).next != nil) )
  10.         {
  11.  
  12.             if ( target == (*p).info )
  13.             {
  14.                 (*q).next = (*p).next;
  15.             }
  16.             else
  17.             {
  18.                 q = p;
  19.                 p = (*p).next;
  20.             }//end if
  21.         }//end while
  22.  
  23.         r = (*r).next;
  24.     }
  25.  
Aug 19 '07 #1
5 3441
ilikepython
844 Expert 512MB
Hello,

I have a linked list in C++ and I am trying to remove all duplicate nodes from the list. The code below is what our prof. wrote on the board in class, yet it doesn't seem to be working correctly for me. Any advice would be helpful.

Thanks

Expand|Select|Wrap|Line Numbers
  1. r = first;
  2.     while ( r != nil )
  3.     {
  4.         target = (*r).info;
  5.  
  6.         q = r;
  7.         p = (*q).next;
  8.  
  9.         while ( ((*p).next != nil) )
  10.         {
  11.  
  12.             if ( target == (*p).info )
  13.             {
  14.                 (*q).next = (*p).next;
  15.             }
  16.             else
  17.             {
  18.                 q = p;
  19.                 p = (*p).next;
  20.             }//end if
  21.         }//end while
  22.  
  23.         r = (*r).next;
  24.     }
  25.  
Have you declared nil? Usually you check like this:
Expand|Select|Wrap|Line Numbers
  1. while (r != NULL);
  2.  
Aug 19 '07 #2
JosAH
11,448 Expert 8TB
Hello,

I have a linked list in C++ and I am trying to remove all duplicate nodes from the list. The code below is what our prof. wrote on the board in pusdo code in class, yet it doesn't seem to be working correctly for me. I also need to preface that the list is not sorted. Prior to entering this part of the code we need to link two preexising lists together. The directions do not say to sort the two list once they are put together.

Any advice would be helpful.

Thanks

Expand|Select|Wrap|Line Numbers
  1. r = first;
  2.     while ( r != nil )
  3.     {
  4.         target = (*r).info;
  5.  
  6.         q = r;
  7.         p = (*q).next;
  8.  
  9.         while ( ((*p).next != nil) )
  10.         {
  11.  
  12.             if ( target == (*p).info )
  13.             {
  14.                 (*q).next = (*p).next;
  15.             }
  16.             else
  17.             {
  18.                 q = p;
  19.                 p = (*p).next;
  20.             }//end if
  21.         }//end while
  22.  
  23.         r = (*r).next;
  24.     }
  25.  
That while condition at line #9 is not correct, i.e. the last node of the list should
be considered to be a duplicate to be removed; make it:

Expand|Select|Wrap|Line Numbers
  1. while (p != nil)
  2.  
I realize that 'nil' is part of the pseudo code, make that 'NULL' in your actual code.
You can alse write '(*p).next' as 'p->next'; it's more concise and takes less of
those darn parentheses ;-)

kind regards,

Jos
Aug 19 '07 #3
That while condition at line #9 is not correct, i.e. the last node of the list should
be considered to be a duplicate to be removed; make it:

Expand|Select|Wrap|Line Numbers
  1. while (p != nil)
  2.  
I realize that 'nil' is part of the pseudo code, make that 'NULL' in your actual code.
You can alse write '(*p).next' as 'p->next'; it's more concise and takes less of
those darn parentheses ;-)

kind regards,

Jos
Thanks for the advice Jos. nil is actually declared in my program. I agree that it is a little funny, but that is the way the prof wants it. I am still having trouble though. Just prior to this section of code is where I merge the two lists together. If I skip this code above and print out my list I get one long list (both put together) the way I would expect. When I run this code inbetween I only get the values from the second list and then the code stops and wont allow me to 'press any key to continue.' I am really lost because I would expect the code above to work as expected. I think it might have something to do with my pointers, but I am not sure.

Thanks
Aug 19 '07 #4
JosAH
11,448 Expert 8TB
When I run this code inbetween I only get the values from the second list and then the code stops and wont allow me to 'press any key to continue.' I am really lost because I would expect the code above to work as expected. I think it might have something to do with my pointers, but I am not sure.

Thanks
Well, you have to show us a bit of relevant code then; we're not psychic ;-)

kind regards,

Jos
Aug 19 '07 #5
Well, you have to show us a bit of relevant code then; we're not psychic ;-)

kind regards,

Jos
I actually figured out what it was. When the two nodes match it makes the update to remove it but never moves the p and q pointers so it gets stuck in a loop. I needed to add the code in the false part of the if to the bottom of the code in the true part.

Thanks for the help!!
Aug 20 '07 #6

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

Similar topics

4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
5
by: b b | last post by:
I created the following code to delete all linked tables in my database (Access 200): -------------------------------------------------------- Dim tbl As TableDef Dim dbs As Database Set dbs...
5
by: CR | last post by:
I've been to figure out how to get AddSortLast function to add nodes in acsending order (a b c d) I figured it out how to get it to sort in decending order but I can't get it to sort in acsending...
0
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...
6
by: jw | last post by:
hi all,i have such 2 classes for a linked list, class Node{ private: int value; Node *next; public: Node() { next=NULL; }
3
by: dazzle | last post by:
I have an XML file and I would like to remove duplicate nodes within it but I can't get my head round on how to do this. Example XML file: <root> <plugin> <title>A9</title> <url>some...
11
by: JoeC | last post by:
I have been using C++ for several years now and I have been reading about nodes. I am a little confused. I understand many of the concepts behind this such as pointers and dynamic binding. My...
3
by: KL | last post by:
I am so confused. I am working on a project for my class, and I can't remember how to do Nodes and linked lists. I have the following bit of code but don't know how to proceed from here. If...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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
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...
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.