Board index » delphi » array of pointers

array of pointers

Hi all,

I am stuck in my program, which is supposed to simulate a basic flight
booking system, at the point where I have to use an array of pointers. I
have never used the pointers as arrays and I am really confused.
I am pasting some of the code, can please anybody give me a clue on what to
change to get the procedure to work.
I am still at the beginning of the program, I will have to add, delete,
modify, etc, manipulate this array of linked list in all possible ways, but
if somebody could help me on this first procedure, adding of a passenger in
a flight, I will know to do the rest, because I know to work with simple
linked lists. All I need is to get the idea of an array of linked
lists...Thanks in advance for any help.

Program Flight;

Type
        flights = (CY357, BA234, MA246);
        listpointer = ^ listnode;
        listnode =    record
                  name : packed array[1..10] of char;
                  seat : integer;
                  link: listpointer;
                end;
        pointerarray = array[flights] of listpointer;

Var
   choice : char;
   arraypointers : pointerarray;

Procedure Selectflight(var flightno: flights);
Var
   fl : integer;
   valid : boolean;

Begin
   writeln('Select one of the available flights with the options: ');
   writeln('1 for CY357');
   writeln('2 for BA234');
   writeLn('3 for MA246');
   writeln('0 for exit');
   valid := false;
   repeat
         readln(fl);
         if fl = 1 then flightno := CY357 else
         if fl = 2 then flightno := BA234 else
         if fl = 3 then flightno := MA246 else
         if fl = 0 then valid := true else
         if fl > 3 then
         begin
              writeln('Please try again');
              valid := true;
         end;
   until valid;
End;
----------- here is the code that I am confused about --------------
Procedure Addpassenger(var oneflight: flights; var l: arraypointers);
var p: arraypointers;
begin
     Selectflight(oneflight);
     new(oneflight[l]);    <-------

end;

 

Re:array of pointers


hi

"Kislany" <mar...@spidernet.net> schrieb im Newsbeitrag
news:9a2hap$37tq2$1@ID-76148.news.dfncis.de...

Quote
> Hi all,

> I am stuck in my program, which is supposed to simulate a basic flight
> booking system, at the point where I have to use an array of pointers.
I
> have never used the pointers as arrays and I am really confused.
> I am pasting some of the code, can please anybody give me a clue on
what to
> change to get the procedure to work.
> I am still at the beginning of the program, I will have to add,
delete,
> modify, etc, manipulate this array of linked list in all possible
ways, but
> if somebody could help me on this first procedure, adding of a
passenger in
> a flight, I will know to do the rest, because I know to work with
simple
> linked lists. All I need is to get the idea of an array of linked
> lists...Thanks in advance for any help.

> Program Flight;

> Type
>         flights = (CY357, BA234, MA246);
>         listpointer = ^ listnode;
>         listnode =    record
>                   name : packed array[1..10] of char;
>                   seat : integer;
>                   link: listpointer;
>                 end;
>         pointerarray = array[flights] of listpointer;

> Var
>    choice : char;
>    arraypointers : pointerarray;

> Procedure Selectflight(var flightno: flights);
> Var
>    fl : integer;
>    valid : boolean;

> Begin
>    writeln('Select one of the available flights with the options: ');
>    writeln('1 for CY357');
>    writeln('2 for BA234');
>    writeLn('3 for MA246');
>    writeln('0 for exit');
>    valid := false;
>    repeat
>          readln(fl);
>          if fl = 1 then flightno := CY357 else
>          if fl = 2 then flightno := BA234 else
>          if fl = 3 then flightno := MA246 else
>          if fl = 0 then valid := true else
>          if fl > 3 then
>          begin
>               writeln('Please try again');
>               valid := true;
>          end;
>    until valid;
> End;
> ----------- here is the code that I am confused about --------------
> Procedure Addpassenger(var oneflight: flights; var l: arraypointers);
> var p: arraypointers;
> begin
>      Selectflight(oneflight);
>      new(oneflight[l]);    <-------

try it this way :

Procedure Addpassenger(var oneflight: flights; var l: arraypointers);
var p: listpointer;

begin
 Selectflight(oneflight);
 { you will get errors, when typing 0! - think of your procedure
select... }
 new(p)
 { fill p^with data }
 p^.link := l[oneflight];
 l[oneflight] := p;
end;

hth
jochen

and i hope, this wasn't all of your homework. ;-)

Re:array of pointers


Hi,

Thanks for your help on this.
I have finalised the add procedure and added a traverse procedure, both
which are working fine. I have also created a procedure to delete a
passenger from one flight, depending on the name of the passenger.
It does work fine as long as the passenger is not the first in the list. As
soon as it is, instead of deleting the passenger, it cuts the name of that
first passenger and adds to it a weird character. I have tried anything I
could think of to fix it, but I cannot. Any ideas of what am I doing wrong?

Procedure Deletepassenger(var oneflight: flights; l : pointerarray; var
target : string);

var found : boolean;
      p, q, result : listpointer;

Begin
  found := false;
  p := l[oneflight];
  Selectflight(oneflight);
  writeln('Enter name to delete');
  readln(target);
  if p = nil then
   writeln('List is empty') else
    if p^.name = target then
    begin
     q := p^.link;
     dispose(p);
     l[oneflight] := q;
   end
   else
   begin
     while (p <> nil) and (p^.name <> target) do
       begin
         q := p;
         p := p^.link;
       end;
       if p^.name = target then
       begin
         q^.link := p^.link;
         dispose(p);
         end
         else
          writeln('Passenger not in list');
         end;
     end;

Thanks in advance.

Quote
"Jochen" <jote...@hotmail.com> wrote in message

news:9a2km5$3bth7$1@ID-55934.news.dfncis.de...
Quote
> hi

> try it this way :

> Procedure Addpassenger(var oneflight: flights; var l: arraypointers);
> var p: listpointer;

> begin
>  Selectflight(oneflight);
>  { you will get errors, when typing 0! - think of your procedure
> select... }
>  new(p)
>  { fill p^with data }
>  p^.link := l[oneflight];
>  l[oneflight] := p;
> end;

> hth
> jochen

> and i hope, this wasn't all of your homework. ;-)

Re:array of pointers


hi kislany,

"Kislany" <mar...@spidernet.net> schrieb im Newsbeitrag
news:9a7dse$3tqrm$1@ID-76148.news.dfncis.de...

Quote
> Hi,

> Thanks for your help on this.
> I have finalised the add procedure and added a traverse procedure,
both
> which are working fine. I have also created a procedure to delete a
> passenger from one flight, depending on the name of the passenger.
> It does work fine as long as the passenger is not the first in the
list. As
> soon as it is, instead of deleting the passenger, it cuts the name of
that
> first passenger and adds to it a weird character. I have tried
anything I
> could think of to fix it, but I cannot. Any ideas of what am I doing
wrong?

> Procedure Deletepassenger(var oneflight: flights; l : pointerarray;
var
> target : string);

> var found : boolean;
>       p, q, result : listpointer;

> Begin
>   found := false;
>   p := l[oneflight];
>   Selectflight(oneflight);

p might (will) point ot the wrong list - or even none if oneflight was
not set correctly before.

rest of code looks ok to me.

error probably not in this procedure ?

btw. do you think, it is a good idea, to put the user-interaction and
datamanipulation in the same procedure?

Quote
>   writeln('Enter name to delete');
>   readln(target);
>   if p = nil then
>    writeln('List is empty') else
>     if p^.name = target then
>     begin
>      q := p^.link;
>      dispose(p);
>      l[oneflight] := q;
>    end
>    else
>    begin
>      while (p <> nil) and (p^.name <> target) do

this might cause trouble , if 'complete boolean eval' is on!

- Show quoted text -

Quote
>        begin
>          q := p;
>          p := p^.link;
>        end;
>        if p^.name = target then
>        begin
>          q^.link := p^.link;
>          dispose(p);
>          end
>          else
>           writeln('Passenger not in list');
>          end;
>      end;

> Thanks in advance.

your source-formatting is not easy to read. so i might have missed
something.

hth
jochen

Re:array of pointers


Quote
"Kislany" <mar...@spidernet.net> wrote in message

news:9a7dse$3tqrm$1@ID-76148.news.dfncis.de...

Quote
> It does work fine as long as the passenger is not the first in the list.
As
> soon as it is, instead of deleting the passenger, it cuts the name of
that
> first passenger and adds to it a weird character. I have tried anything
I
> could think of to fix it, but I cannot. Any ideas of what am I doing
wrong?

> Procedure Deletepassenger(var oneflight: flights; l : pointerarray; var
> target : string);

Hi Kislany,

You need to make l a var parameter, else it won't let you change the
elements of the array.

--
The Scarlet Manuka

Re:array of pointers


"The Scarlet Manuka" <sa...@maths.uwa.edu.au> schrieb im Newsbeitrag
news:9a8vhj$9rs$1@fang.dsto.defence.gov.au...

Quote
> "Kislany" <mar...@spidernet.net> wrote in message
> news:9a7dse$3tqrm$1@ID-76148.news.dfncis.de...

[...]
> Hi Kislany,

> You need to make l a var parameter, else it won't let you change the
> elements of the array.

> --
> The Scarlet Manuka

that looks much better and should solve the problem ;-)

greetings
jochen

Other Threads