Solved

Linked List

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now