LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2004, 05:38 PM   #1
durden2.0
Member
 
Registered: May 2003
Distribution: redhat 9.0
Posts: 113

Rep: Reputation: 15
c++ doubly linked lists


Ok I have one little bug and it is driving me crazy. I think I might have been just working with doubly linked lists for too many hours straight. Here is my problem, I am wanting to add to lists together. For example, I want to take list2 and put it on the end of list1 so that all of list2 is moved and put into list1, on to the back of list1 that is. Both lists are doubly linked lists, here is a little bit of the code, thanks for the help.


if(list2->next != NULL) // check to make sure there is something
{ // worth adding to list1
list1->next = list2;
list2->prev = list1;
}

I am sure it is obvious so someone out there, thanks for the help guys.
 
Old 02-25-2004, 05:47 PM   #2
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Rep: Reputation: 30
are you using two instansiations of the same object, or did you hard code 2 seperate lists.

The implimentation will differ a little, but both will consist of connecting the pointers you use for head and tail.

Last edited by xviddivxoggmp3; 02-25-2004 at 05:48 PM.
 
Old 02-25-2004, 05:48 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
well that code doesnt tell me much because i dont know what list1 and list2 are. im gonna assume that list1 is the first element of the first list and list2 is the first element of the second list. in this case what we want to do is find the last element of the first list and put the second one after it so how about.

Code:
list_type *l=list1;
while(l->next != NULL) l=l->next;
l->next=list2;
list2->prev=l;
i cant be more specific unless you show your code and give me a better description of the problem than 'one little bug'

also c++ has a wide range of containers in the stl including std::list you ought to think about using one of these instead unless you are coding a linked list just for understanding.

Last edited by kev82; 02-25-2004 at 05:51 PM.
 
Old 02-25-2004, 05:50 PM   #4
durden2.0
Member
 
Registered: May 2003
Distribution: redhat 9.0
Posts: 113

Original Poster
Rep: Reputation: 15
sorry I should have made more sense. Ok here goes, list1 is a pointer to the end of the first list. List2 is a pointer to the first item in list2, and yes it is two seperate lists. The problem I have is a sorting algorithm and after I sort the two lists, I keep adding elements from list2 to list1 and then when it is done I want to add all the remaining elements of list2 to the end of list1. I hope that helps more, sorry about that.
 
Old 02-25-2004, 05:56 PM   #5
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Rep: Reputation: 30
to have a smaller and cleaner program i would use 2 instansiations of one list object.

but for 2 seperate lists.
I would use the prior suggestion
i do not mean to over simplify, but this should work.
this creates list 3 and kills list 2

Last edited by xviddivxoggmp3; 02-25-2004 at 07:21 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 01:59 AM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
queue and linked lists Palamides Programming 2 03-09-2005 08:08 PM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration