Solved

Linked List

Posted on 1998-11-05
4
177 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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