Connecting Tech Pros Worldwide Forums | Help | Site Map

Ordering a singly linked list

Member
 
Join Date: Sep 2007
Posts: 55
#1: Oct 31 '07
Hi,

I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them?

If my structs look like the one below.

Expand|Select|Wrap|Line Numbers
  1. struct block
  2. {
  3.    struct block *next;
  4.    int number;
  5. };
  6. typedef struct block block;
  7.  

sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Oct 31 '07

re: Ordering a singly linked list


Just out of curiosity, why not use the linked list in the STL (The one with a built-in sort() function) ?

Ordering a singly linked list is possible. You have to have a pointer to both of the (using your example) blocks - prev_block and next_block respectively (though you have prev_block->next, so you can use that...), create your new_block and assign it a value, point new_block's next to next_block, and then prev_block->next to new_block. Then you need to put your pointer on pre_del_block and get the block_to_delete->next, and move block_to_delete->next to pre_del_block->next (assuming there was a block being moved from one position to another inside the list.

Not really an algorithm, but does that give you a general idea?
Member
 
Join Date: Sep 2007
Posts: 55
#3: Oct 31 '07

re: Ordering a singly linked list


Quote:

Originally Posted by sicarie

Just out of curiosity, why not use the linked list in the STL (The one with a built-in sort() function) ?

Ordering a singly linked list is possible. You have to have a pointer to both of the (using your example) blocks - prev_block and next_block respectively (though you have prev_block->next, so you can use that...), create your new_block and assign it a value, point new_block's next to next_block, and then prev_block->next to new_block. Then you need to put your pointer on pre_del_block and get the block_to_delete->next, and move block_to_delete->next to pre_del_block->next (assuming there was a block being moved from one position to another inside the list.

Not really an algorithm, but does that give you a general idea?

Yes it does, I was thinking along the same lines.

Never heard of the STL one coz I am rather new to C and link list altogether.

Cheers.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#4: Oct 31 '07

re: Ordering a singly linked list


Quote:

Originally Posted by Alien

Never heard of the STL

The STL is the C++ Standard Template Library. There is code in there for variable sized arrays, linked lists, trees, sorting, etc. All the necessary plumbing you use in software development. You can use it or, you an re-invent the wheel and rewrite in your own image making your code much harder to understand by the next person.
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#5: Oct 31 '07

re: Ordering a singly linked list


Quote:

Originally Posted by weaknessforcats

The STL is the C++ Standard Template Library. There is code in there for variable sized arrays, linked lists, trees, sorting, etc. All the necessary plumbing you use in software development. You can use it or, you an re-invent the wheel and rewrite in your own image making your code much harder to understand by the next person.

He did mention (and I ignored the first code tag thinking it might be a typo), that he was using C. Still, it would be a good reference for the OP on linked list setup and manipulation.
Member
 
Join Date: Sep 2007
Posts: 55
#6: Oct 31 '07

re: Ordering a singly linked list


Quote:

Originally Posted by sicarie

He did mention (and I ignored the first code tag thinking it might be a typo), that he was using C. Still, it would be a good reference for the OP on linked list setup and manipulation.


Yes I am using C not C++.

So if there is no built in function or libary i can use to order linked list? Just have to do it manually?
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#7: Oct 31 '07

re: Ordering a singly linked list


If you're in the US, remember those commercials from yesteryear - "Behold, the power of cheese."? Same concept, different product.

Behold the power of Google.

Also, as I mentioned, you can look at the implementation of the STL as a reference.
Reply


Similar C / C++ bytes