2bytesprog2 course_2014_c5_pointers
TRANSCRIPT
![Page 1: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/1.jpg)
Prog_2 course- 2014
2 bytes team
Kinan keshkeh
IT Engineering-Damascus University
3rd year
![Page 2: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/2.jpg)
POINTERS
![Page 3: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/3.jpg)
Static Variables && Dynamic Variables
Static Variables :
var x:integer;
The memory for this
variables x still until
program End , and
doesn’t free even we
don’t use it
Ex: X
Memory
We determine their places
at memory first of all
value
![Page 4: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/4.jpg)
Dynamic Variables :
Memory
The Heap
X var x: ^integer; Ex:
the address
the address value
The memory for this
variables x in the Heap,
we can take it or
dispose it, so it can be
free!!
We determine their places at
memory during the program
![Page 5: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/5.jpg)
Why Dynamic??
1) better usage to memory
2) to create advance data structure
like trees , lists….atc
![Page 6: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/6.jpg)
What is the pointer?
The Definition
The pointer is an address to a memory place
Var ptr: ^Type;
Lets Have an explication!!
but…..
![Page 7: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/7.jpg)
P1
X Value..
P1^
P1
Var p1,p2: ^Type;
x :Type;
P1^
P1
P1^
X:=value
P2
xx
P1^
P2
New(p1);
P1^:=x
Value..
P2:=P1
Value..
P1:=nil;
Value..
+1
![Page 8: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/8.jpg)
xx
P1^
P2
Dispose(p2);
Value..
xx
P1^
xx
P2:=nil;
Value..
+1
![Page 9: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/9.jpg)
Program pointer on Soso; There is wrong !
Type
pEmp=^Emp;
Emp=Record
eno:integer; ename:string[20];
esal:real;
end;
Var
E:Emp; E1:pEmp;
n,m,y:^integer;
EXAMPLE:
![Page 10: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/10.jpg)
Begin i:=5;
new(y); y^:=50; write(y); write(^y); write(y^);
y:=i y^:=i ; y^:=20; i:=y^;
y:=0;
dispose(y); y:=nil;
new(m); new(y); m^:=30; y^:=80;
n:=y;
write(y^);
write(n^);
E1:=nil; new(E1);
E1^:=E ; Read(E1^.Eno);
Dispose(E1);
End.
false false TRUE
NOTE: Without “ New(n)” !! 80 80
false TRUE
![Page 11: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/11.jpg)
STACK {FILO}
First In Last Out
pringles
![Page 12: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/12.jpg)
The Data
next
Nil
TOP
Stack=Record
the Data: Type;
next: Pstack;
end;
The Definition
Pstack=^stack;
Var
Top: Pstack;
D:Type;
……….
![Page 13: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/13.jpg)
Stack procedures :
Push(s,ch)
Pop(s,ch)
S_Top(s,ch)
Is_Empty(s) 3)
2)
1)
4)
![Page 14: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/14.jpg)
EX: You have a string
“KiKi”
How to put/take it in
/from the STACK ?!
i next
TOP
K
i
K
Nil
![Page 15: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/15.jpg)
Stack=Record
ch: char;
next: Pstack;
end;
Type
Pstack=^stack;
Var Top: Pstack; st: string[4]; i:integer; x:char;
BEGIN
Readln(st); top:=nil;
For i:=1 to (length(st) ) do
push( top, st[i] ) ; While(top<>Nil) do
begin
Pop( top, x) ; write(x,’ ’);
end;
END.
Program fifi;
![Page 16: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/16.jpg)
PUSH
Procedure push (var Top:Pstack; c:char);
Var
temp:Pstack;
Begin
end;
new(temp);
temp^.ch:=c;
temp^.next:=top;
top:=temp;
i next
TOP
K
i
K
Nil
temp K
TOP
i
TOP
K
TOP
i
![Page 17: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/17.jpg)
Stack=Record
ch: char;
next: Pstack;
end;
Type
Pstack=^stack;
Var Top: Pstack; st: string[4]; i:integer; x:char;
BEGIN
Readln(st); top:=nil;
For i:=1 to (length(st) )
push( top, st[i] ) ; While(top<>Nil) do
begin
Pop( top, x) ; write(x,’ ’);
end;
END.
Program fifi;
![Page 18: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/18.jpg)
POP
Procedure pop (var Top:Pstack; var c:char);
Var temp:Pstack;
Begin
end;
Dispose(temp);
Temp:=Top;
Top:=Top^.next ;
C:=top^.ch;
i next
TOP
K
i
K
Nil The output:
C
i
temp
K
TOP
temp
TOP i temp
TOP
K
temp
i K i K
![Page 19: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/19.jpg)
Stack=Record
ch: char;
next: Pstack;
end;
Type
Pstack=^stack;
Var Top: Pstack; st: string[4]; i:integer; x:char;
BEGIN
Readln(st); top:=nil;
For i:=1 to (length(st)-1)
push( top, st[i] ) ; begin
Pop( top, x) ; write(x,’ ’);
end;
END.
Program fifi;
While(top<>Nil) do
![Page 20: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/20.jpg)
Is_Empty
Function Is_Empty (top:Pstack):boolean
begin
if top =Nil then
Is_Empty:=True;
else
Is_Empty:=false;
end;
![Page 21: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/21.jpg)
S_Top
Function s_Top (top:Pstack): integer/…Type
begin
if not is_Empty(top) then
s_Top :=top^.key;
end;
NOTE: Don’t write then : top:=top^.next Cause that function just return the element without
change in stack!!
![Page 22: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/22.jpg)
NOTE: You can reach to the data , at a
determinate place by pointers without do
pop() cause with pop() you cant restoring
your ‘poped’ data ..
You do:
S:=top; {bring the third element 64}
i:=s^.next^.next^.key;
OR:
S:=top; {bring the 11 element}
For i:=1 to 10 do
s:=s^.next;
i:=s^.key
52 next
TOP
35
64
76
Nil
S +1
![Page 23: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/23.jpg)
Homework: :لديك تعبير حسابي مثال
(( (1 * (3+2)) * 1) +3 )
:المطلىب التعبير الحسابي وطباعتها على قيمةحساب
pop/pushالشاشة باستخدام المكدس
!مكدس واحد فقط :مالحظة
+20 points
![Page 24: 2Bytesprog2 course_2014_c5_pointers](https://reader030.vdocument.in/reader030/viewer/2022032504/55c29582bb61eb48128b45b7/html5/thumbnails/24.jpg)
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team