هياكل البيانات

Upload: mohammed-mohyeldin-siddig

Post on 10-Jul-2015

3.156 views

Category:

Documents


3 download

TRANSCRIPT

: 1- :- / . / . :- / oh notation . (O(N / 2 . log (O(N . : :- 2((O(N1) + O(N2) = O(MAX(N1,N 2. N1>>N : oh notation : For i :=1 to N do ; (Read (x . (O(N : : For i:= 1 to n do For j :=1 to n do ; (Read (x 2. (O(N : For i:= 1 to n do ; (Read(x ; (Read(y 1(. (O(N)+O 1 N2>>N . (O(N : oh notation : ; 0 = sum 1/ for I := 1 to n do 1 + ;sum := sum1

k := n * n ; /2 for I := 1 to n do for j :=1 to k do sum := sum + 1 ; for i:= 1 to n do /3 for j := 1 to I do ;sum := sum + 1 N N/2 N/2 X =X .X N N N-1/2 N-1/2 X =X .X .X N N X : oh notation ; Function power(x,n:integer ):real Var ; Sum:real ; I : integer Begin ; Sum:= 0 For I := 1 to n do ; sum := sum x ; power := sum ; end : Function power(x,n:integer):real; Begin If n = 0 then power :=1 Else If odd (n) then ((Power := x * sqr(power (x,n div 2 Else ; ((Power :=sqr (power (x,n div 2 ; End

2

26 X 26( (O :- 13X62 = X31 . X X31 = X15 . X15 . X X15 = X7 . X7 . X X7 = X3 . X3 . X X3 = X2 . X 26X 5(. (O

) . ( . .

:- ). (TOP LIFO LAST IN FIRST OUT . . :- . 1- . PUSH FULL . STACK OVER FLOW3

STACKS

2- . . POP EMPTY . STACK UNDER . FLOW :- 1- . 2- . MAXSTACK BOTTOMMOST . TOP :- CONST 001 = ; MAXSTACK TYPE STACK = RECORD ; ITEM : ARRAY [1..MAXSTACK] OF INTEGER ; TOP : 0..MAXSTACK ; END VAR ;S : STACK -: EMPTY . . EMPTY TRUE FALSE . . VALUE PARAMETER . :- ; Function empty (s:stack):boolean Begin If s.top = 0 then Empty := true Else ; Empty := false4

;End

:- ; )'If empty (s) then write('stack is empty -: Full . . MAXSTACK FULL TRUE FALSE . PARAMETER . VALUE PARAMETER . ; Function full (s:stack):boolean Begin If s.top = maxstack then Full := true Else ; Full := false ;End :- ; )'If full (s) then write ('stack is full stacktop . ; Function stacktop (s:stack ) : integer Begin )'If empty (s) then write ('stack is empty Else ; ]Stacktop := s.item [s.top ; End :- stack . ; Uses stack Begin5

Writeln(' input your decimal number ') ; Read(n) ; Repeat X := N mod 16 ; Push (s,x) ; N := n div 16 ; Until n = 0 ; Writeln(' your number in hex-decimal is ') ; While not empty (s) do Begin Case pop(s) of 10 : write ('A') ; 11 : write ('B') ; 12 : write ('C') ; 13 : write ('D') ; 14 : write ('E') ; 15 : write ('F') ; else write (pop(s)) ; end ; end ; readln ; end . addtop . -: Op2 = pop(s) If empty(s) then Element = true Push( s,op2) Else Op1 = pop (s) Element = false Push ( s,op1+op2 ) : S : addtop

6

addstack . :- Repeat ; Addtop ; Until element :- S :- addstack :- addstack , addtop . ) ( 1- . postfix notation 2- infix notation . posfix notation -: Postfix notation . .+AB A+B :- + A B Postfix notation a+b + A B . Postfix Notation -: Postfix Notation 1- ) . p 2- p 3 4 . 3- . S 4- ) + ( :- / S 1 OP 2. OP / 2. OP1 ( + ) OP / ) ( .7

/ ) 4 ( . 5- ) 2 ( . 6- . S 7- . : + A B = A 53=. B 3 ) (Push ( S , A 2 ) Push ( S , B 1 OP2 = POP(S 2OP 4)( OP1 = POP(S

52RESULT = OP1 + OP

6PUSH( S, RESULT

:- ABC+D 2=.A=5 B=3 C=1 D 1 2 )(PUSH (S,A 3 )PUSH(S,B 4 PUSH(S,C

5

6

7 ( PUSH ( S , D

Infix notation -: Postfix notation :- Q . Infix " ( " " ) " . Q 1-8

Q 3 6 2- . Q ) P 3- .( Postfix Q . 4- Q ) ( )( :- 5- ) ( ) + ( P . ) ( ) ( S ) 5 ( . Q :- 6- ) ( S P . ) ( " ( " ) ( S P ) 6 ( . 7- 2 . 8- . :- INFIX . POSTFIX A-(B+C)*D 4 P=A 3 P=A 2 P=A ) - , ( PUSH ( S 1 P = AB PUSH ( S , C

8 P = AB

7 P = ABC ) + , ( PUSH ( S

6 P = ABC

5 +P = ABC POP ( S

21 +P = ABC

11 +P = ABC9

01 +P = ABC

9 P = ABC+D

) ( + , POP ( S

PUSH ( S

61P = ABC+D

51 41 *P=ABC+D* P = ABC+D ) ( POP ( S

31 * P = ABC+DPOP ( S

:- 1- *(AB C (POSTFIX NOTATION 2=C 3=, B 71 = , A 2- + * - ABCD 31 = D , 81 = , C 52 = B = 2 , A . 3 POSTFIX NOTATION :- A+B*C / / ) A + B) * C / )(A * B / C ) / (A - D

:- postfix . 0'(;('Value :=Ord (ch)-ord ; Program evalute Const 05 = ; Maxarray Type ; Arr = array [ 1..maxarray ]of char Var01

; X : arr ; Position : 1..maxarray ; Function eval ( x : arr ) :real Const ; Maxstack = maxarray Type Stack = record ; Item : array [ 1..maxstack ] of real ; Top : 0..maxstack ; End Var ; St : stack ; Opnd1 , Opnd2 , Value : real ; Ch : char ; Position : 1..maxarray ; Function pop (var s:stack):real { body of pop } ;(Procedure push (var s:stack;x:real {body of push} ; Function op(ch:char):boolean {body of op} ; function oper (ch:char ;op1,op2 : real) : real {body of oper} {begin {function eval ;st.top:= 0 ;position := 1 ;[ch:=arr[position while ch < > ' ' do { begin{ is found if op(ch)then begin ;('value :=ord(ch)-ord('0 ;(push(st,value end else {begin{operator is found ;(opnd2:=pop(st11

;(opnd1:=pop(st ;(value:=oper (ch,opnd1,opnd2 ;(push(st,value end if position < maxarray then begin ;position:= position + 1 ;[ ch:=arr [ position end else ; ' ' =:ch ;end ;(eval:= pop(st ;end begin ;('write('enter your postfix expression for i:= 1 to maxarray do ; ( [ read ( arr [ position ; readln ;(' writeln(' original postfix expression is for position:=1 to maxarray do ; ( [ write ( arr [ position ;((writeln('value of postfix is ',eval(arr ;writeln .end

-: Op -1 False true . : ; Function op (ch:char):boolean Begin (('If (ord(ch)>=ord ('0')) and (ord(ch)