473,320 Members | 1,853 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,320 software developers and data experts.

Ordering a singly linked list

61
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.  
Oct 31 '07 #1
6 2498
sicarie
4,677 Expert Mod 4TB
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?
Oct 31 '07 #2
Alien
61
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.
Oct 31 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 31 '07 #4
sicarie
4,677 Expert Mod 4TB
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.
Oct 31 '07 #5
Alien
61
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?
Oct 31 '07 #6
sicarie
4,677 Expert Mod 4TB
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.
Oct 31 '07 #7

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

Similar topics

4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
7
by: Shwetabh | last post by:
Hi, can some one tell me: -> how to remove a loop from a singly linked list with a loop. -> how to count the number of nodes in a looped singly link list. Thanks
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
42
by: Avon | last post by:
Hello there. I have a problem regarding node deletion in singly-linked lists in C. What happens is that I can't hold the previous node efficiently. Here is the code I use. struct lista *search...
3
by: jou00jou | last post by:
Hello, I am trying to sort a singly linked list for the following typedef typedef struct message { int messageId; char * messageText; struct message * next; } message; I have successfully...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.