English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do I balance a linked list in C++?

To balance, I mean sorting the nodes in the list so that the node with the greatest value is at the head of the list, the second greatest node is at the end, and so on... (the data type is double)

Thanks for you help!!!

2007-03-20 06:37:06 · 3 answers · asked by sheket 1 in Computers & Internet Programming & Design

No, it is balanced as such"

Lets say you have a linked list of integers:

1->5->7->3->88

The postcondition would be:

88->5->1->3->7

Thus effectively balancing the biggest elements on either side...

2007-03-20 08:46:56 · update #1

3 answers

No, he/she means this I believe:

100 98 96 95 97 99

As for how to do this...interesting question. One thought is to take the original list and sort it:

100 99 98 97 96 95

Then take a pass through, skipping every other element until the middle:

100 98 96

Then make a second pass in the opposite direction:

95 97 99

Glue these two lists together.

2007-03-20 09:06:36 · answer #1 · answered by Anonymous · 0 0

So you don't really mean "balance" but rather simply "sort" in descending order. Correct?

Once the list is created (or manipulated) you need a sort routine to quickly sort it. If the list will be large, like in the thousands of nodes level, then create a quicksort routine. (Search the web for libraries or code - many exist).

Otherwise, just code up a simple exchange sort for smaller lists.

2007-03-20 06:43:14 · answer #2 · answered by BigRez 6 · 0 0

ok i went on teh D.C trip too, in 8th grade it replace into chilly and moist and the aspects would be rather undesirable and you are able to not deliver a million shoes and jackets and garments that's what i recommend placed on a sweatshirt and a snug jacket, like a northface fleece jacket or besides the fact that your heat wintry climate jacket is placed on denims and comfortable jogging shoes which you do no longer strategies getting moist and ruined, like old footwear i be attentive to this doeesn't sound glamorous, yet you will be jogging around lots outdoors and interior, so layers and convenience is particularly key! you would be sitting around in a bus lots so which you would be wanting to be comfortable additionally, bringing a bag/tote is a sturdy theory, yet as quickly as lower back, do no longer deliver a million! while my grade went, no person even introduced a tote everybody had backpacks with snacks and layers in and stuff. in case you place on tank tops and t shirts, no person additionally will see it different than once you're in yuor room. this is going to be chilly outdoors, so which you would be donning probably a sweatshirt AND jacket over any shirt or tank suited. everybody would be dressing very rather. this is exciting. rather its a great bonding trip and everybody sounds like crap of their pajamas eating junk foodstuff on the bus

2016-10-19 04:23:08 · answer #3 · answered by lipton 4 · 0 0

fedest.com, questions and answers