Solved

Linked List

Posted on 1998-11-05
4
175 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
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

808 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