Sorted double linked list question

General discussion about C/C++

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Sorted double linked list question

Postby nmnir » Tue May 18, 2010 2:56 am

Hi All,

I'm making a sorted double linked list in the following way:

class Item
{
int time_stamp;
Item * prev;
Item * next;
}
class SortedList ()
{
Item * tail;
Item * head;
}
head should be the smallest item and tail is the largest.
Sorted list constructor:
_tail = new jitter_item () ;
jitter_item * curr = _tail, *tmp ;
for ( int i = 0; i < BUFFER_SIZE -1; i++ )
{
tmp = curr->_next = new jitter_item () ;
tmp->_previous = curr ;
curr = tmp ;
}
curr->_next = _head = new jitter_item ();

Now, my problem is that after the head is used I want to re-use it by making it the new tail and making head->prev the new head. I tried:

tmp1 = _head;
_head = _head->_previous ;
_head->_next = NULL ;

tmp2 = _tail->_next ;
_tail = tmp1;
tmp2->_previous = _tail;
_tail->_next = tmp2;
_tail->_previous = NULL;

But it has a bug.
Can some please help me?

Thanks,
Nahum
SortedList contructor:
nmnir
 
Posts: 1
Joined: Tue May 18, 2010 2:45 am

Re: Sorted double linked list question

Postby ventsyv » Thu May 20, 2010 9:51 am

try this:

curr->next = head;
curr->previous = NULL;//curr will be the new head
head->previous = curr;//head will now be second in the list so it needs to point to the new head.
head = curr; // now curr is your new head
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest