Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

need help in sorting some objects inside objects

sigersiger Member Posts: 2
i have objects of type Reservation inside objects of type Apartment.
What i ned is jsut to insert object Reservation in the order of their names.
i used this code but it is jsut working for inserting in the begining and the end. It is not working for inserting in the middle
*************************************************************
public boolean makeReservation( String appType, String name, int weekBeg, int weekEnd)
{
Reservation newRes=new Reservation (weekBeg, weekEnd, name);

Appartment cur=head;
Reservation previous=null;

while ((cur.code!=appType)&&(cur!=null))
{
cur=cur.next;
}
if(cur.Res==null)
{
cur.Res=newRes;
newRes.link=null;
}
else
{
while((cur.Res!=null) &&(cur.Res.nameRenter.compareTo(name)<0))
{

previous=cur.Res;
cur.Res=cur.Res.link;
}
if(previous==null)
{
newRes.link=cur.Res;
cur.Res=newRes;
}
else
{
previous.link=newRes;
newRes.link=cur.Res;

}
retrun true;
}

Comments

  • chamsterchamster Member Posts: 662
    I believe you're looking for something like this:
    [code]
    public void addElement (Apartment b)
    {
    Apartment a = head;
    if (a > b)
    {
    b.tail = head;
    head = b;
    }
    else
    {
    for (a = head; a.tail != null && a.tail < b; a = a.tail);
    b.tail = a.tail;
    a.tail = b;
    }
    }
    [/code]

    It's not the fastest code but i tried to form it so it can be easily implemented into your method.


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • chamsterchamster Member Posts: 662
    [b][red]This message was edited by chamster at 2002-10-20 0:34:49[/red][/b][hr]
    I forgot to mention that [red]a < b[/red] will not work. I used it to shorten my code. What i really mean is [blue]a.name.compareTo (b.name)[/blue].

    Also, i think i used Apartment as the little object inside the big one (Reservation). That shouldn't be any problem for you to correct, as the point remains the same. Should you get stuck, just let me know...


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com



  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _ // http://forcoder.org // free video tutorials and ebooks about | Python Ruby PL/SQL JavaScript Objective-C MATLAB Swift Go Assembly Scratch Visual Basic .NET C C# Delphi Perl R Visual Basic Java PHP C++ Scheme Lua Fortran Crystal Dart FoxPro Apex Clojure Logo Ada ML Rust Transact-SQL Erlang LabVIEW F# COBOL Julia Bash Lisp SAS ABAP Alice Prolog Scala Hack Awk VBScript Kotlin D | _____

Sign In or Register to comment.