• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 893
  • Last Modified:

perl and linked lists

I need to write a program which similates a "Rolodex".  
Each page should contain the following information:

      First_Name      Last_Name                   Phone_Number

The program should be menu driven, with the following options:

      1) Add a page,
      2) Search for a page by last name,
      3) Search for a page by phone number,
      4) Remove a page (found by searching by last name).
      5) Print out the whole rolodex sorted by last name.

the program must use linked lists.  Each page should be represented by a
hash, and the reference to the hash should be stored as the value of a node
in the linked list.  When a new node is inserted, it should be inserted into
the list so as to preserve alphabetical ordering.  
0
police45s
Asked:
police45s
3 Solutions
 
kanduraCommented:
I'd suggest using a database for the backend, DBI with DBD::SQLite, and Class::DBI for the model.
0
 
davorgCommented:
You tend not to use linked lists in Perl as arrays, hashes and other more complex data structures are usually more efficient.

Dave...
0
 
kanduraCommented:
jmcg,
> If you're just learning Perl, and don't already have experience with SQL
> or databases, I don't know that I can second Kandura's suggestion.

Hence the recommendation to use Class::DBI. All you need to know is how to create the base table, and CDBI will wrap each row in an object. There is no SQL in view anywhere :-)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
police45sAuthor Commented:
I understand and appreciate your comments, but this is the way that the program must be constructed, with a linked list.  I have worked on a single occasion with linked lists but the data that was being passed was just a scalar variable(a number).  This linked list is supposed to be created using a hash (for the 3 data fields), and the reference to the hash should be stored as a value of a node in the linked list.  This is the problem I'm having and unfortunately I can't get very far into the code unless I can constuct this hash and the reference to the hash.  Any help would be appreciated.
0
 
jmcgOwnerCommented:
Kandura -

I'll have to try that once I get my system back to working (HD failed, dammit).

Dave -

A reminder on hashes and hash references to maybe get you started:

You can initialize a hash via an array assignment. Here's a sloppy linked list example:

    %LList = ( next=>undef, content=>undef);

New elements can be anonymous hashes

     $LList{next} = { next=>undef,
            content=>{FirstName=>'James', LastName=>'Mason', Phone=>'555-1212' }
          };

I've made the content into a sub-hash rather than have them mix in with the namespace with 'next'  but that's a matter of taste and probably inefficient.

When you go to add another element, you start with

      $cursor = \%LList;

and chase down the linked list to find where to insert the new element:

       while( $cursor->{next}  && $cursor->{next}{content}{LastName} < $NewLastName ) {
                $cursor = $cursor->{next};
                }
        $cursor->{next} = { next=>$cursor->{next},
                  content=>{FirstName=$NewFirstName, Lastname=>$NewLastName, Phone=>$NewPhone}
                };
       
0
 
kanduraCommented:
jmcg,
I don't believe I deserve points for this topic, since my recommendation didn't satisfy the OP's requirements.

Good to see you back in cleaning though!

Sometimes I wish we had something of a chat room within this topic area. It would be nice to have a talk with you and the other experts on occasion.
Something I'd like to mention for example (even though it's not relevant to this particular topic) is that I'm in the process of moving to another country (Norway, to be precise) with my wife, so my activities on EE will be rather sporadic over the coming month.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now