Board index » delphi » What's wrong with my program?

What's wrong with my program?

Quote
Benjy wrote in message <01bde60d$fdcbd800$0a0ebcca@jerryngu>...
>Hi all,

>I'm just wondering if any fellow pascal can help look into the
problem in
>this piece of program. The problem starts when Insert_Node is allowed
to
>run.

Program Student_Result; -- Student? Hints only then.

Look at the declaration of the parameter New_Node:

Procedure Insert_Node(Var List: ListType;
                      New_Node: ListType;
                      Var Success: boolean);

Do you want to modify New_Node? How do you declare a parameter you
want to modify?

FP

 

Re:What's wrong with my program?


Quote
Benjy wrote:
> I'm just wondering if any fellow pascal can help look into the problem in
> this piece of program. The problem starts when Insert_Node is allowed to
> run. The Pascal compiler would terminate itself if run in Windows, the
> whole computer would hang if run in DOS mode.....I can't get to figure
> out......can someone please help...

I think you have got a little bit irritated in Insert_Node with the booleans, and there is
no need to do so. Let me offer you a better way:

{---------------------------------------------------------------------------}
 procedure Insert_Node(var List : ListType;
                        New_Node : ListType;
                        var Success : Boolean);
  var
    Curr, Last : ListType;
  begin
    {-Success by default}
    Success := True;

    {-Setup the local var's}
    Curr := List;
    Last := Curr;

    {-First check if the list is empty}
    if Curr = nil then begin
      List := New_Node;
      Exit;
    end;

    {-Try to find the place to insert the node _before_ an existing node}
    while Curr <> nil do begin
      if New_Node^.Name < Curr^.Name then begin
        {-First patch the node to be insert}
        New_Node^.Prev := Last;
        New_Node^.Next := Curr;
        {-Patch the list before the node..}
        Last^.Next := New_Node;
        {...and behind}
        Curr^.Prev := New_Node;
        {-We have all done, go out}
        Exit;
      end;
      {-Remember the last node}
      Last := Curr;
      {-Go on with the next}
      Curr := Curr^.Next;
    end;

    {-No lower name in list, 2 situation's can happen:}
    if Last^.Name = New_Node^.Name then
      {-Allready in list}
      Success := False
    else begin
      {-It's the greatest name, append it}
      Last^.Next := New_Node;
      New_Node^.Prev := Last;
    end;
  end;
{---------------------------------------------------------------------------}

But there are more Bug's in your program:

{---------------------------------------------------------------------------}
  function Full_List(List : ListType) : Boolean;
  var Temp : ListType;
  begin
    New(Temp);
    (***
      Temp will ONLY be nil if it can't be allocated AND you have
      installed a HeapFunc! Otherwise Temp is purly coincidental.
    ***)
    if Temp = nil then
      Full_List := True
    else
    begin
      Full_List := False;
      Dispose(Temp);
    end;
    (***
      Hmm...this function check's if a node can be allocated. Why don't
      say: Full_List:= MaxAvail > SizeOf(ListType); ??
    ***)
  end;
{---------------------------------------------------------------------------}

If have test your program with this changes and it works. Another problem
is that you don't deallocate the node if a name is allready in the list.

BTW, why do you don't allow two person's named "Mike Smith" in the
list? IMHO it's better to compare the StudId as the names of the person's.

By, Andreas.

Other Threads