简体   繁体   中英

Doubly Linked List to Sort Name & Age Separately C++

I am having some problems with a program I am trying to create. I need to read in a text file which has the name of a person, followed by their age on the next line, for however many people..example:

Tyler
24
Greg
16
Laura
25
etc... 

I then need to build a list for the data which is maintained in ascending order based on both the name and their age with a doubly linked list. The list needs to be sorted as it goes (not at the end). So far I have completed building a sorted doubly linked list, and I am reading in the file, however when I print out the file, it gives me all the ages in ascending order, followed by all the names in alphabetical order. I need the age to stick to who it belongs to. For example with the names/ages above it is CURRENTLY printing out:

16
24
25
Greg
Laura 
Tyler

When I need it to print out:

(by age)
Greg 16
Tyler 24
Laura 25

or

(by name)
Greg 16
Laura 25
Tyler 24

I can post code if needed, but I am not really looking for the coding help (as of yet at least), more just the technique in order to keep them together. Currently I am reading the file, and passing it as a string to the insert function (I am pretty sure this is where I am mistaken, but don't know how to go about it).

Thanks in advance, always appreciate the help here!

I see a couple different solutions that could work here. If you define a class like so:

class Person {
 private:
  int name;
  int age;

  ...
};

with some different comparison functions ( isGreaterAge() , isAlphabeticalName() ), that might work.
If this doesn't have to be a linked list (is this schoolwork?), you could use maps or (more suited for your purpose) multimaps, which are specifically designed to do this sort of thing:
http://www.cplusplus.com/reference/map/multimap/?kw=multimap

This is mainly dependent on the way you have designed your data structure and how the sorting algorithm executed on it.

Data Structure:

You need to keep both the information name/age in a given node's data pointer apart from the doubly linked lists pointers. So the insert will have two arguments one is name and another age apart from the list head pointer (if function is re-entrant)

Sorting:

There are different ways to sort, what i would prefer is? Initially while list creation nodes are inserted in sorted manner based on the name (considering there is priority associated for name sorting rather than age)

So whenever user asks for the named sorting we can directly list all the elements.

Now when the user is asking for age-wise sorting you need to perform actual sorting but don't change the links just print them and mark them as visited or processed whatever you call and continue.

Hope this helps you. czar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM