24
Prog_2 course- 2014 2 bytes team Kinan keshkeh IT Engineering-Damascus University 3 rd year

2Bytesprog2 course_2014_c5_pointers

Embed Size (px)

Citation preview

Page 1: 2Bytesprog2 course_2014_c5_pointers

Prog_2 course- 2014

2 bytes team

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Page 2: 2Bytesprog2 course_2014_c5_pointers

POINTERS

Page 3: 2Bytesprog2 course_2014_c5_pointers

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

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

Why Dynamic??

1) better usage to memory

2) to create advance data structure

like trees , lists….atc

Page 6: 2Bytesprog2 course_2014_c5_pointers

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

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

xx

P1^

P2

Dispose(p2);

Value..

xx

P1^

xx

P2:=nil;

Value..

+1

Page 9: 2Bytesprog2 course_2014_c5_pointers

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

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

STACK {FILO}

First In Last Out

pringles

Page 12: 2Bytesprog2 course_2014_c5_pointers

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

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

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

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

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

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

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

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

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

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

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

Homework: :لديك تعبير حسابي مثال

(( (1 * (3+2)) * 1) +3 )

:المطلىب التعبير الحسابي وطباعتها على قيمةحساب

pop/pushالشاشة باستخدام المكدس

!مكدس واحد فقط :مالحظة

+20 points

Page 24: 2Bytesprog2 course_2014_c5_pointers

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team