inserting confusion...

Programming, for all ages and all languages.
Post Reply
User avatar
eboyd
Member
Member
Posts: 97
Joined: Thu Jul 26, 2007 9:18 am
Location: United States

inserting confusion...

Post by eboyd »

I just need some ideas, brain-strorming if you will:

I have a doubly-linked-list template class, OK, great.





My class has member functions (among others):

1) int sorted() -checks to see if the list is in fact sorted. Returns "1" if sorted in ascending order, "-1" if sorted in descending order, or "0" if not sorted at all.

2) void sort() -sorts the list in ascending order.

3) void revsort() -sorts the list descending order.

4) void insert(const T& X) -inserts (X) into a list. Only inserts if list is sorted in ascending order. If the list is not sorted in ascending order, sort() is called, the list is sorted in ascending order, and the insertion is performed.

5) void revins(const T& X) -same as insert(). Its the same as insert, but traverses the list from the tail. It's important to note that this function, like insert(), takes respect to a list sorted in "ascending" order as well. I'll explain the purpose of this in the next function...

6) void ins(const T& X) -this function "chooses" insert() or revins() depending on whether (X) is closer to the head or tail of the list. I included this for cases where the list might be thousands or hundreds of thousands entries long.





Whoopty friggin' doo! (I'm sure you're thinking it!)





I don't really have a question, rather, I just want to get your ideas on how to handle some of these ambiguities:

-what if my list is in descending oreder?

-do i sort ascending, insert, then sort descending?

-how do i know if the user of the class wants one kind of functionality or another?

-do write into my template 3 more insertion functions to handle "descending lists"? If so, how is the end-user effected by this?

-what if i don't want the list sorted at all? is it possible to keep insert from being called unintentionally?





I know these are pretty open-ended questions, with a lot of different answers/solutions. Just like to hear others' thoughts and maybe what the "standards" are (if any) on writing lists.
    frank
    Member
    Member
    Posts: 729
    Joined: Sat Dec 30, 2006 2:31 pm
    Location: East Coast, USA

    Post by frank »

    I really don't like the requirement that the list must remain sorted to use insert. What if the order is important? What if i want to have 5 2 4 6 7 in my list in that order?

    I don't have the full code for your ll so I don't know exactly what you have but I'll recommend a few things. Unless you are designing this list for your specific needs (which in case you are you should probably call it a sort linked list instead of a generic linked list,) you might want to look at the C++ standard library class dequee, which is basically a doubly linked list. This is only if you are not relying on the sorting ability of the list and want to make a generic linked list.
    User avatar
    eboyd
    Member
    Member
    Posts: 97
    Joined: Thu Jul 26, 2007 9:18 am
    Location: United States

    Post by eboyd »

    I'm making my list template class for a couple of different reasons:

    1) to keep my mind fresh on the topic of linked-lists

    2) to make a list that's useful for most of the things I do.

    PostPosted: Thu Sep 20, 2007 2:33 pm Post subject:
    I really don't like the requirement that the list must remain sorted to use insert.
    Well, I suppose it would be easy enough to write an insert function that takes three arguments specifying exactly where the insertion should take place.
    What if the order is important? What if i want to have 5 2 4 6 7 in my list in that order?
    Well... my list has push and pop functionality. That combined with the three argument insert function should take care of that situation.

    Really the objective I was trying to reach in my post was to take the burden of deciding which "insert" method away from the user. If your list is sorted (whether it's ascending or descending), you expect "insert" to behave a certain way without destroying the integrity of the list's order.

    I've been making linked-lists for a while, but it wasn't until I tried lending my hand to a "full-featured" template list that I ran into this confusion.

    Before, most of my lists were very problem specific (obviously NOT using templates), and they were custom-tailored to my needs.
    you might want to look at the C++ standard library class dequee, which is basically a doubly linked list.
    I've seen & studied the STL a bit, and the functionality of its lists is really simple (but standardized so everyone understands how to use it).

    I'm not really concerned with the practicality of my template list, it's more for learning than anything else (I'm a 3rd year CS student).
    User avatar
    Candy
    Member
    Member
    Posts: 3882
    Joined: Tue Oct 17, 2006 11:33 pm
    Location: Eindhoven

    Post by Candy »

    http://en.wikipedia.org/wiki/Policy-based_design

    Try inserting with a policy saying how, and your code will be optimal for all cases (by virtue of templates only instantiating the requested code).
    DeletedAccount
    Member
    Member
    Posts: 566
    Joined: Tue Jun 20, 2006 9:17 am

    For making it more spicy............

    Post by DeletedAccount »

    For making it more spicy ... also use operator overloading instead of
    functions .. eg + for inserting etc .. 8)
    User avatar
    Candy
    Member
    Member
    Posts: 3882
    Joined: Tue Oct 17, 2006 11:33 pm
    Location: Eindhoven

    Re: For making it more spicy............

    Post by Candy »

    SandeepMathew wrote:For making it more spicy ... also use operator overloading instead of
    functions .. eg + for inserting etc .. 8)
    If you mean +=, why not?
    Post Reply