I'm currently try to implement one way linked list using pascal but i'm kinda lost , below i tried to create a list before adding a node then assign nil to the last node but then when i'm trying adding a new node i can't access it (runtime error 216) or that i get the last pointer value for some reasons i don't understand.
Program test;
type
TNodePtr = ^TNode;
TNode = record
number:Integer;
next:TNodePtr;
end;
var
head, tail, temp : TNodePtr;
node : TNode;
i : INTEGER;
procedure InitLists;
begin
head := nil ;
tail := head ;
end;
begin
//Initializing the list
InitLists;
new(head);
new(tail);
//creating the list
//first node
node.number := 1 ;
new(node.next);
head^ := node ;
tail^ := node ;
//second node
node.number := 2 ;
new(node.next);
tail^.next^ := node;
tail^ := node ;
//third node
node.number := 3 ;
node.next := nil;
tail^.next^ := node;
tail^ := node ;
//adding a node (forth node)
node.number := 4 ;
new(node.next);
new(tail^.next);
tail^.next^ := node ;
tail^ := node ;
//printing the list
temp := head ;
for i:=1 to 4 do
begin
writeln(temp^.number);
if temp^.next <> nil then
begin
temp := temp^.next ;
//this is for testing
//The final node is not being printed
//but instead i'm getting 3 which is the previous node value
writeln('done')
end
end
end.
My problem was with the node : Tnode that i used , and as suggested by someone in the comments section (He deleted his comment) I tried not using that method and allocating space and assigning nodes directly into the list without an intermediate and here how it worked for me : (Ps : Thanks a lot unknown hero)
Program test;
type
TNodePtr = ^TNode;
TNode = record
number:Integer;
next:TNodePtr;
end;
var
head, tail, node : TNodePtr;
procedure InitLists;
begin
head := nil ;
tail := head ;
end;
begin
//Initializing the list
InitLists;
//creating the list
//first node
writeln('=======================');
new(node);
node^.number := 1 ;
node^.next := nil ;
head := node ;
tail := node ;
writeln(tail^.number);
writeln(head^.number);
writeln('=======================');
//second node
//this is where my issue is
writeln('=======================');
new(node);
node^.number := 2 ;
node^.next := nil ;
tail^.next := node ;
tail := node ;
writeln(tail^.number);
writeln(head^.number);
//exactly here
writeln(head^.next^.number);
writeln('=======================');
//third node
writeln('=======================');
new(node);
node^.number := 3 ;
node^.next := nil ;
tail^.next := node ;
tail := node ;
writeln(tail^.number);
writeln(head^.number);
writeln(head^.next^.number);
writeln(head^.next^.next^.number);
writeln('=======================');
//adding a node (forth node)
writeln('=======================');
new(node);
node^.number := 4 ;
node^.next := nil ;
tail^.next := node ;
tail := node ;
writeln(tail^.number);
writeln(head^.number);
writeln(head^.next^.number);
writeln(head^.next^.next^.number);
writeln(head^.next^.next^.next^.number);
writeln('=======================');
end.
Related
I dont know why the prove is error at the overflow in the variable contador. I need help.
contador: Integer;
J: Integer;
function noPrimos (lista : My_Array) return Boolean
with
Global => contador,
--Depends => ...,
Pre => True and contador < Integer'Last,
Post => (noPrimos'Result = True or noPrimos'Result = False);
FILE ADB
function noPrimos (lista : My_Array) return Boolean is
contador: Integer;
begin
for I in lista'Range loop
contador:= 0;
if lista(I) /= 1 then
for J in 1.. lista(I) loop
if lista(I) rem J = 0 then
contador := contador + 1;
end if;
end loop;
if contador <= 2 then
return false;
end if;
else
return true;
end if;
pragma Loop_Variant(Increases => I);
end loop;
return true;
end noPrimos;
The problem is the overflow the result:
Phase 1 of 2: generation of Global contracts ...
Phase 2 of 2: flow analysis and proof ...
15:40: medium: overflow check might fail (e.g. when contador = 2147483647)
47:40: medium: overflow check might fail (e.g. when contador = 0)
First of all, I assume that the function noPrimos will return True only if the list lista does not contain any primes. That being said, I'm a little bit puzzled by some aspects of the code snippet:
The type definition of My_Array is missing.
The role of the global instance of contador (english: counter) is not clear from the given code snippet. By writing Global=> contador, you state that the global variable contador will be read by the function noPrimos (see also here), but that doesn't happen because the local instance of contador shadows the global instance of contador.
The reason for the variable J being defined globally is not clear, you can omit it.
The precondition True (on the left side of the Boolean operator and) is trivial and can be omitted.
The postcondition states that the result of noPrimos can either be True or False. This is trivial as noPrimos returns a boolean and can therefore be omitted. The postcondition should state the function's expected result given its inputs.
The loop variant pragma Loop_Variant(Increases => I); is trivial as the variable I will increase by the definition of the for-loop. Hence the loop variant can be omitted.
Below is an example of a function No_Primes that searches the given list L for primes and returns True if none are found. It proves in GNAT CE 2019:
primes.ads (spec)
package Primes with SPARK_Mode is
type List is
array (Natural range <>) of Positive;
--
-- Returns True if N is a prime number (or False otherwise).
--
function Is_Prime (N : Positive) return Boolean
with
Global => null,
Post => Is_Prime'Result =
(if N = 1 then False
else (for all I in 2 .. N - 1 => N rem I /= 0));
--
-- Returns True if list L does not contain any prime numbers (or False otherwise).
--
function No_Primes (L : List) return Boolean
with
Global => null,
Post => No_Primes'Result =
(for all I in L'Range => Is_Prime (L (I)) = False);
end Primes;
primes.adb (body)
package body Primes with SPARK_Mode is
--------------
-- Is_Prime --
--------------
function Is_Prime (N : Positive) return Boolean is
begin
if N = 1 then
return False;
else
for I in 2 .. N - 1 loop
if N rem I = 0 then
return False;
end if;
pragma Loop_Invariant
(for all J in 2 .. I => N rem J /= 0);
end loop;
end if;
return True;
end Is_Prime;
---------------
-- No_Primes --
---------------
function No_Primes (L : List) return Boolean is
begin
for I in L'Range loop
if Is_Prime (L (I)) then
return False;
end if;
pragma Loop_Invariant
(for all J in L'First .. I => Is_Prime (L (J)) = False);
end loop;
return True;
end No_Primes;
end Primes;
A small test program (main.adb)
with Ada.Text_IO; use Ada.Text_IO;
with Primes; use Primes;
procedure Main is
-- Some test vectors.
L1 : List := (1 => 1); -- Expect TRUE : 1 is not a prime.
L2 : List := (1, 2, 3, 5, 7); -- Expect FALSE : All are prime except 1.
L3 : List := (2, 3, 5, 7); -- Expect FALSE : All are prime.
L4 : List := (1, 4, 6, 8, 9); -- Expect TRUE : None are prime.
L5 : List := (4, 6, 8, 9); -- Expect TRUE : None are prime.
L6 : List := (3, 4, 5); -- Expect FALSE : 3 and 5 are prime.
begin
Put_Line ("No_Primes (L1) = " & Boolean'Image (No_Primes (L1)));
Put_Line ("No_Primes (L2) = " & Boolean'Image (No_Primes (L2)));
Put_Line ("No_Primes (L3) = " & Boolean'Image (No_Primes (L3)));
Put_Line ("No_Primes (L4) = " & Boolean'Image (No_Primes (L4)));
Put_Line ("No_Primes (L5) = " & Boolean'Image (No_Primes (L5)));
Put_Line ("No_Primes (L6) = " & Boolean'Image (No_Primes (L6)));
end Main;
yields
No_Primes (L1) = TRUE
No_Primes (L2) = FALSE
No_Primes (L3) = FALSE
No_Primes (L4) = TRUE
No_Primes (L5) = TRUE
No_Primes (L6) = FALSE
I am trying to add derives clause to this procedure and this is my solution :
--# derives Index from Key, Data & Found from Data & I from Data ;
I am not sure about it and I need help
procedure Find
(Key: Integer ;
Data : in MyArray ;
Index : out Integer ;
Found : out Boolean )
--# post (Found -> Data(Index) = Key);
--# derives ???
is
I: Integer ;
begin
I := 0;
Found := False ;
loop
--# assert (I >= 0) and
--# (I <= Data 'Last + 1) and
--# (Found -> Data(I) = Key);
exit when (I > Data 'Last ) or Found ;
if(Data(I)) = Key
then
Found := True;
else
I:= I + 1;
end if;
end loop;
Index := I;
end Find;
This Pascal code inserts a new element to a linked list with the help of "while loop":
program linked_list;
type
pointer_typ = ^record_2;
record_2 = record
data: integer;
next: pointer_typ;
end;
procedure Push_int(var p: pointer_typ; x: integer);
var
temp: pointer_typ;
begin
new(temp);
temp^.data := x;
temp^.next := p;
p := temp
end;
var
first: pointer_typ;
x: integer;
begin
first := nil;
while not Seekeof do begin
read(x);
Push_int(first, x)
end;
end.
Can you help with making a procedure which inserts a new element to linked list but with the help of recursion (not with while loop)?
I tried and got wrong results (for example, the code below returns values in "queue order", but not in "stack order" as I'd like):
program linked_list;
type
pointer_typ = ^record_2;
record_2 = record
data: integer;
next: pointer_typ;
end;
procedure Push_int(var p: pointer_typ);
var
temp: pointer_typ;
x: integer;
begin
read(x);
new(temp);
temp^.data := x;
temp^.next := p;
p := temp;
if Seekeof then
exit
else
Push_int(p^.next)
end;
var
first: pointer_typ;
begin
first := nil;
Push_int(first);
end.
Thank you.
Seems, this one works correct:
program linked_list;
type
pointer_typ = ^record_2;
record_2 = record
data: integer;
next: pointer_typ;
end;
procedure Push_int(var p: pointer_typ; x: integer);
var
temp: pointer_typ;
begin
new(temp);
temp^.data := x;
temp^.next := p;
p := temp;
if Seekeof then
exit
else begin
read(x);
Push_int(p, x)
end;
end;
var
first: pointer_typ;
x: integer;
begin
first := nil;
read(x);
Push_int(first, x)
end.
I would like know if it is possible to allocate a struct from a type specified by a nil pointer by using reflect.New()
type SomeType struct{
A int
}
sometype := (*SomeType)(nil)
v := reflect.valueOf(sometype)
// I would like to allocate a new struct based on the type defined by the pointer
// newA := reflect.New(...)
//
newA.A = 3
How should I do this ?
Use reflect.Type.Elem():
s := (*SomeType)(nil)
t := reflect.TypeOf(s).Elem()
v := reflect.New(t)
sp := (*SomeType)(unsafe.Pointer(v.Pointer()))
sp.A = 3
Playground: http://play.golang.org/p/Qq8eo-W2yq
EDIT: Elwinar in comments below pointed out that you can get the struct without unsafe.Pointer by using reflect.Indirect():
s := (*SomeType)(nil)
t := reflect.TypeOf(s).Elem()
ss := reflect.Indirect(reflect.New(t)).Interface().(SomeType)
ss.A = 3
Playground: http://play.golang.org/p/z5xgEMR_Vx
So I'm trying to permute all possible n digit long numbers out of x long array/set of elements. I've come up with a code that does that, however the digits are the same, how do I prevent that from happening. Here's my come(Pascal):
program Noname10;
var stop : boolean;
A : array[1..100] of integer;
function check( n : integer ) : boolean;
begin
if n = 343 // sets the limit when to stop.
then check := true
else check := false;
end;
procedure permute(p,result : integer);
var i : integer;
begin
if not stop
then if p = 0 then
begin
WriteLn(result);
if check(result)
then stop := true
end
else for i := 1 to 9 do
begin
permute(p - 1, 10*result+i);
end;
end;
begin
stop := false;
permute(3,0);
readln;
end.
Here is the code in Prolog
permutate(As,[B|Cs]) :- select(B, As, Bs), permutate(Bs, Cs).
select(A, [A|As], As).
select(A, [B|Bs], [B|Cs]) :- select(A, Bs, Cs).
?- permutate([a,b,c], P).
Pascal is much harder.
Here is an usefull algorithm, you might want to use. But it is not tested, so you have to debug it yourself. So you have to know how the algorithm works.
The Bell Permutation algorithm: http://programminggeeks.com/bell-algorithm-for-permutation/
procedure permutate(var numbers: array [1..100] of integer; size: integer;
var pos, dir: integer)
begin
if pos >= size then
begin
dir = -1 * dir;
swap(numbers, 1, 2);
end
else if pos < 1 then
begin
dir = -1 * dir;
swap(numbers, size-1, size);
end
else
begin
swap(numbers, pos, pos+1);
end;
pos = pos + dir;
end;
begin
var a, b: integer;
a = 1; b = 1;
while true do
begin
permutate(A, 5, a, b);
printArray(A, 5);
end;
end.