Hi, can anyone tell me how by using structures and pointers define list where every element has a pointer to another list?. That's my unsuccessfull code:
type next_cityt = ^city_type;
city_type = record
nextCit: next_cityt;
end;
type vertex = ^vertex_type;
vertex_type = record
nextVer: vertex;
nextCity: next_cityt;
end;
Vertex is suposed to be pointer to array of lists
Defining 2d dynamic array
Started by thatsme, May 12 2010 12:25 PM
5 replies to this topic
#1
Posted 12 May 2010 - 12:25 PM
|
|
|
#2
Posted 12 May 2010 - 02:43 PM
It looks like a good start. Why don't you think this will work?
#3
Posted 12 May 2010 - 11:35 PM
if definition is goog then i might have done mistakes accesing pointer's pointer. The bolded line doesn't compile
procedure graph_print(node: vertex);
var no, verno: integer;
temp: next_cityt;
begin
verno := 0;
if node = nil then
writeln('No verteces')
else
begin
while node <> nil do
begin
inc(verno);
writeln(verno,'st vertex');
no := 0;
if node^.nextCity <> nil then
begin
temp := node^.nextCity;
while temp <> nil do
begin
inc(no);
writeln(no,' ok');
temp = temp^.nextCit;
end;
end;
node = node^.nextVer;
end;
end;
end;
sorry for no code indent, but when i try to do it and save the message, indentation always disappears
procedure graph_print(node: vertex);
var no, verno: integer;
temp: next_cityt;
begin
verno := 0;
if node = nil then
writeln('No verteces')
else
begin
while node <> nil do
begin
inc(verno);
writeln(verno,'st vertex');
no := 0;
if node^.nextCity <> nil then
begin
temp := node^.nextCity;
while temp <> nil do
begin
inc(no);
writeln(no,' ok');
temp = temp^.nextCit;
end;
end;
node = node^.nextVer;
end;
end;
end;
sorry for no code indent, but when i try to do it and save the message, indentation always disappears
Edited by thatsme, 12 May 2010 - 11:44 PM.
indentation
#4
Posted 13 May 2010 - 12:08 AM
don't need help any more, typed = instead of := :), C habits
#5
Posted 13 May 2010 - 12:26 AM
I checked it over (here's indented version, use CODE tags to keep indentation...) and I found your problem.
It's really very simple. But next time you ask a question, please report the actual error you're getting.
Ps: Coding conventions say you need to use different naming for your types:
It's much cleaner looking, and quicker to type, Plus, you now have the same "name" part for both the pointer and the plain type.
For pointers to pointers to things, use "PP" as the prefix, like:
PPCity=^PCity;
Anyway, your code seem to be fine otherwise.
Please note that in Delphi you can use dynamic arrays to store 2d (or any-d) data.
As you can see this could be simpler... But I'm not sure you're using Delphi, and older Pascals do not have support for dynamic arrays.
Good luck.
procedure graph_print(node: vertex);
var
no, verno: integer;
temp: next_cityt;
begin
verno := 0;
if node = nil then
writeln('No verteces')
else
begin
while node <> nil do
begin
inc(verno);
writeln(verno,'st vertex');
no := 0;
if node^.nextCity <> nil then
begin
temp := node^.nextCity;
while temp <> nil do
begin
inc(no);
writeln(no,' ok');
temp [B]:=[/B] temp^.nextCit; //you're going to need ":=", not "="
end;
end;
node = node^.nextVer;
end;
end;
end;
It's really very simple. But next time you ask a question, please report the actual error you're getting.
Ps: Coding conventions say you need to use different naming for your types:
type
PCity=^TCity;
TCity=record
Next:PCity;
end;
PVertex=^TVertex;
TVertex=record
city:PCity;
next:PVertex;
end;
It's much cleaner looking, and quicker to type, Plus, you now have the same "name" part for both the pointer and the plain type.
For pointers to pointers to things, use "PP" as the prefix, like:
PPCity=^PCity;
Anyway, your code seem to be fine otherwise.
Please note that in Delphi you can use dynamic arrays to store 2d (or any-d) data.
type TCityArray=array of array of TCity;
var
c:TCityArray;
n,i:Integer;
begin
SetLength(c,10,10); //make it a 10x10 array
c[5,3].Name:='Chicago'; //depends on how "TCity" is defined, of course...
//it could be a string or a record....
//or
SetLength(c,10); //sets the first dimension to 10 items, that is 10 arrays of whatever
SetLength(c[3],20);
SetLength(c[5],30);
c[3,19].Name:='City no 20 of the 4th list';
c[5,29].Name:='The last city of the 6th list';
//these last lines do exactly what your graph_print proc would do:
for n:=0 to Length(c)-1 do
begin
writeln(n+1,'st nd rd th vertex');
for i:=0 to Length(c[n])-1 do
WriteLn('City ',n,',',i,' is ',c[n,i].Name); //or writeln(i+1,' ok');
end;
end;
As you can see this could be simpler... But I'm not sure you're using Delphi, and older Pascals do not have support for dynamic arrays.
Good luck.
#6
Posted 13 May 2010 - 12:29 AM
thatsme said:
don't need help any more, typed = instead of := :), C habits
Well, that's funny, the answer came before mine... I took a while typing it, I guess :)


Sign In
Create Account


Back to top










