Pointer help!?
Author |
Message |
StixandRox
|
Posted: Tue Feb 27, 2007 9:07 pm Post subject: Pointer help!? |
|
|
I was doing the pointer tut and I get it, and I'm trying to do previous. It works alll the way until you get to 2 and it won't goto one. I know i'm close, but if you can give me a suggestion or another way to look at it that would be great.
code: | var list : collection of
record
num : int
next : ^list
prev : ^list
end record
var root : pointer to list %Start of the list
new list, root
var temp, temp2 : pointer to list
var ref : pointer to list %reference
ref := root
new list, temp
ref -> next := temp
ref -> prev := temp
ref -> num := 1
ref := temp
for i : 2 .. 100
temp2 := temp
new list, temp
ref -> next := temp
ref -> num := i
ref := temp
ref -> prev := temp2
end for
ref := root
put ref -> num
ref := list (ref).next %2
put ref -> num
ref := ref -> next %3
put ref -> num
ref := ref -> next %4
put ref -> num
ref := ref -> prev %3
put ref -> num
ref := ref -> prev %2
put ref -> num
ref := ref -> prev %1
put ref -> num |
The comment tells it what that line will make it (should make it) go to.
EDIT
When I put the line;
ref -> prev := temp2
After the ref -> next := temp2 line it gives me all 4's...
EDIT2
code: | for i : 2 .. 100
ref -> prev := temp
new list, temp
ref -> next := temp
ref -> num := i
ref := temp
end for |
This should work in my mind. It stores the reference in the previous (so at the beggining [i=2] it would reference the first temp [1]. Then a new temp would be made, stored in next, the number is declared, and then the new root is given what next references. It startes again by saying 2 references...oh I see it. Tricky...Hmm I wonder how to fix this.
Interesting, probably simple. Any help would be appreciated.
EDIT EDIT EDIT EDIT EDIT
code: | var list : collection of
record
num : int
next : ^list
prev : ^list
end record
var root : pointer to list %Start of the list
new list, root
var temp, temp2 : pointer to list
var ref : pointer to list %reference
ref := root
new list, temp
ref -> next := temp
ref -> prev := temp
ref -> num := 1
ref := temp
for i : 2 .. 99
temp2 := temp
new list, temp
ref -> next := temp
ref -> num := i
ref -> prev := temp2
ref := temp
end for
ref -> num := 100
ref -> next := temp
ref -> prev := temp2
ref := root
for i : 1 .. 100
put ref -> num
ref := ref -> next
end for
loop
ref := ref -> prev %3
put ref -> num
end loop
ref := ref -> prev %2
put ref -> num
ref := ref -> prev %1
put ref -> num |
GAH I reall thought this would work but I gots nothin x_X The prev just makes it 99 everytime (it does go back from 100 though haha)
This is tricky tricky...still working on it ^^; |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|