Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linked List

Posted on 1998-11-05
4
Medium Priority
?
181 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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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.
Suggested Courses

597 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