?
Solved

Linked List

Posted on 1998-11-05
4
Medium Priority
?
180 Views
Last Modified: 2010-04-02
I am writing a program that will allow a user to enter a series of information for a mailing list. I want to pass this information through a function to a linked list that will have a sort both on the person's last name and another sort on their zip code.

I want the user to enter his/her data and then have a function that will pass the data to the linked list. A loop will allow the user to enter multiple listings, and after each full entry the function to pass the data to a linked list will occur.


The two areas that I am having trouble with are:
1) Having the two separate sorts for the list (name and zip)
2) In passing to the function how do I create a head pointer for the first call of the function yet bypass creating a head pointer in subsequent calls.
0
Comment
Question by:opus9966
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 300 total points
ID: 1177082
Use a separate linked list to hold the order. ie: You have a list of entries and another separate list whose nodes are in the order you require and which point to the items in the first list. In this way you can have another list with a different order without chaning the original list of items.

Create a head pointer as the first thing you do (ie: during program initialisation). Always pass this pointer to your function along with the item to be added to the list that the head pointer is the head of! Initially this pointer will be nil.

Cheers,

Raymond.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177083
>> You have a list of entries and another separate list whose nodes
>> are in the order you require and which point to the items in the
>> first list. In this way you can have another list with a different
>> order without chaning the original list of items.

That is a good technique to use if your sort needs are very dynamic.  Like if the user selectes an option to sort of this or that and you need t evaluate their options and resort on what they choose.  But if you will always be sorting on two fixed sequences, it is a little easier if you create nodes that have two sets of link pointers, like

class Address
{
   Address *NextName;
   Address *NextZip;
   char[30] Name;
   char[30] Line1;
   char[30] Line2;
   char[18] City;
   char[2] State;
   char[10] Zip;
};

The list head would also need two pointers, like

class AddressList
{
   Address* FirstName;
   Address* FirstZip;
};
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1177084
I would do it nietod's way, but you have to be VERY careful, :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177085
>> I would do it nietod's way, but you have to be VERY careful, :)

There is less room for error with the way I suggested--that is why I suggested it.  There are less dynamic memory allocations so less to track and less to screw up.  The 2nd list doesn't have to maintain pointers back to nodes in the first, so there are less pointers to track.  It is clearly a less powerful solution, so it has to have some advantage over the other, and that advantage is that it is easier to write and less likely to have bugs.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question