01 линейные структуры данных

  • View
    5.920

  • Download
    1

Embed Size (px)

DESCRIPTION

" "

Transcript

  • 1. 1 01.12.2008, 08.12.2008 -, 261

2.

  • Pascal (Delphi)

3.

  • + = ( )

4.

  • , , , ,

5.

  • ,
  • type student = record
  • name : string;
  • class : integer;
  • marks : array[1..10] of integer;
  • end;

6.

  • var
  • s : student;
  • s.name := Vasya;
  • writeln(s.name);

7. Pascal

  • =
  • ( new ) ( delete )
  • pstudent = ^student

8.

  • ()
  • type
    • pnode = ^node;
    • node = record
      • data : integer;
      • next : pnode;
    • end;
  • first

9.

  • ( add )
  • ( find )
  • ( remove )

10. 11.

  • procedure add(x : longint);
  • var
  • p : pnode;
  • begin
  • new(p);
  • p^.data := x;
  • p^.next := first;
  • first := p;
  • end;

12.

  • function find(x : integer) : boolean;
  • var
  • wp : pnode;
  • begin
  • wp := first;
  • while (wp nil) do begin
  • if (wp^.data = x) then begin
  • result := true;
  • exit;
  • end;
  • wp := wp^.next;
  • end;
  • result := false;
  • end;

13.

14.

  • ,

15. ()

  • procedure remove(x : integer);
  • var
  • cur, prev, tmp : pnode;
  • begin
  • if (first = nil) then begin
  • exit;
  • end;
  • if (first^.data = x) then begin
  • tmp := first;
  • first := first^.next;
  • delete(tmp);
  • exit;
  • end;

16. ()

  • prev := first;
  • cur := first^.next;
  • while (cur nil) do begin
  • if (cur^.data = x) then begin
  • prev^.next := cur^.next;
  • delete(cur);
  • exit;
  • end;
  • cur := cur^.next;
  • prev := prev^.next;
  • end;
  • end;

17.

  • add O(1)
  • find O(n)
  • remove O(n)

18.

  • ,
  • type
    • pnode = ^node;
    • node = record
      • data : integer;
      • next,prev: pnode;
    • end;

19.

  • ( add )
  • ( find )
  • ( remove )

20.

  • procedure add(x : longint);
  • var
  • p : pnode;
  • begin
  • new(p);
  • p^.data := x;
  • p^.next := first;
  • if (first nil) then begin
  • first^.prev := p;
  • end;
  • first := p;
  • end;

21.

  • ,
  • function find(x : integer) : boolean;
  • var
  • wp : pnode;
  • begin
  • wp := first;
  • while (wp nil) do begin
  • if (wp^.data = x) then begin
  • result := true;
  • exit;
  • end;
  • wp := wp^.next;
  • end;
  • end;

22. 23.

  • ,
  • ,

24. ()

  • procedure remove(x : integer);
  • var
  • cur, prev, next : pnode;
  • begin
  • if (first = nil) then begin
  • exit;
  • end;
  • end;

25. ()

  • cur := first;
  • while (cur nil) do begin
  • if (cur^.data = x) then begin
  • prev := cur^.prev;
  • next := cur^.next;
  • // 4 :
  • //
  • exit;
  • end;
  • cur := cur^.next;
  • end;

26. (1, 2)

  • if (prev=nil) and (next=nil) then begin
  • //
  • first := nil;
  • delete(cur);
  • end;
  • if (prev nil) and (next = nil) then
  • begin
  • //
  • prev^.next := nil;
  • delete(cur);
  • end;

27. (3, 4)

  • if (prev = nil) and (nextnil) then
  • begin
  • //
  • first := next;
  • delete(cur);
  • end;
  • if (prevnil) and (nextnil) then begin
  • //
  • prev^.next := next;
  • next^.prev := prev;
  • delete(cur);
  • end;

28. ?

  • ( sentinel elements ) , ,

29.

  • type
    • pnode = ^node;
    • node = record
      • data : integer;
      • next, prev : pnode;
      • isSentinel : boolean;
    • end;

30. 31.

  • procedure add(x : longint);
  • var
  • p : pnode;
  • begin
  • new(p);
  • p^.data := x;
  • p^.next := first;
  • first^.prev := p;
  • first := p;
  • end;

32.

  • function find(x : integer) : boolean;
  • var
  • wp : pnode;
  • begin
  • wp := first;
  • while (wp nil) do begin
  • if ( ( not( wp^.isSentinel ) ) and
  • (wp^.data = x) ) then begin
  • result := true;
  • exit;
  • end;
  • wp := wp^.next;
  • end;
  • result := false;
  • end;

33.

  • procedure remove(x : integer);
  • var
  • cur, prev, next : pnode;
  • begin
  • cur := first;
  • while (cur nil) do begin
  • if ( ( not( cur^.isSentinel ) ) and
  • (cur^.data = x) ) then begin
  • cur^.prev^.next := cur^.next;
  • cur^.next^.prev := cur^.prev;
  • delete(cur);
  • exit;
  • end;
  • cur := cur^.next;
  • end;
  • end;

! 34.

  • add O(1)
  • find O(n)
  • remove O(n)

35.

  • Stack
  • LIFO (Last In First Out)

36.

  • push(x)
  • pop()
  • isEmpty()

37.

  • stack,
  • top,

38.

  • procedure push(x : integer);
  • begin
  • inc(top);
  • stack[top] := x;
  • end;
  • function isEmpty() : boolean;
  • begin
  • result :=( top = 0 ) ;
  • end;

39.

  • function pop() : integer;
  • begin
  • if (isEmpty()) then begin
  • //
  • //
  • exit;
  • end;
  • result := stack[top];
  • dec(top);
  • end;

40.

  • Queue
  • FIFO (First In First Out)

41.

  • add
  • remove
  • isEmpty

42.

  • queue,
  • head ,
  • tail ,
  • ,

43.

  • procedure add(x : integer);
  • begin
  • queue[tail] := x;
  • inc(tail);
  • end;
  • function isEmpty() : boolean;
  • begin
  • result :=( head = tail ) ;
  • end;

44.

  • function remove() : boolean;
  • begin