Solved

perl and linked lists

Posted on 2004-10-25
816 Views
Last Modified: 2010-05-18
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
Question by:police45s
    6 Comments
     
    LVL 18

    Expert Comment

    by:kandura
    I'd suggest using a database for the backend, DBI with DBD::SQLite, and Class::DBI for the model.
    0
     
    LVL 8

    Accepted Solution

    by:
    You tend not to use linked lists in Perl as arrays, hashes and other more complex data structures are usually more efficient.

    Dave...
    0
     
    LVL 18

    Assisted Solution

    by:kandura
    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
     

    Author Comment

    by:police45s
    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
     
    LVL 20

    Assisted Solution

    by:jmcg
    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
     
    LVL 18

    Expert Comment

    by:kandura
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Suggested Solutions

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    884 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

    16 Experts available now in Live!

    Get 1:1 Help Now