Użytkownik: reason
Tytuł: Ile identycznych elementow w lancuchu
Język: Pascal
Data: 8 lut 2010, 15:14
Link: http://3paste.com/s/170r/pascal/ile_identycznych_elementow_w_lancuchu
Ocena:
  1. program ilepowt;
  2. uses crt;
  3. type pnode = ^node;
  4. node = record
  5. val : integer;
  6. next : pnode;
  7. end;
  8.  
  9. procedure dodaj(var head : pnode; a : integer); // procedura dodajaca element do lancucha;
  10. var w, tmp : pnode;
  11. begin
  12. if (head <> nil) then begin
  13. w := head;
  14. while (w^.next <> nil) do w := w^.next;
  15. end;
  16. new(tmp);
  17. tmp^.val := a;
  18. tmp^.next := nil;
  19. if head = nil then
  20. head := tmp
  21. else
  22. w^.next := tmp;
  23. end;
  24.  
  25. function ile(a,b : pnode) : integer;
  26. begin
  27. if (a = nil) or (b = nil) then ile := 0 // tak sie zastanawiam czemu to dziala; wiecej info w mailu;
  28. else
  29. if a^.val > b^.val then ile := ile(a, b^.next)
  30. else
  31. if a^.val < b^.val then ile := ile(a^.next, b)
  32. else
  33. ile := ile(a^.next, b^.next) + 1;
  34. end;
  35.  
  36. var
  37. head, head2 : pnode;
  38. begin
  39. head := nil; // tworze jeden lancuch
  40. dodaj(head, 2);
  41. dodaj(head, 3);
  42. dodaj(head, 11);
  43. dodaj(head, 13);
  44. head2 := nil; // tworze drugi lancuch
  45. dodaj(head2, 3);
  46. dodaj(head2, 11);
  47. dodaj(head2, 13);
  48. dodaj(head2, 17);
  49. writeln(ile(head,head2)); // wypisuje ile maja identycznych elementow
  50. end.
© 2008 3paste.com Pytania i odpowiedzi | Kontakt | Zmiany 3paste_Poland 3paste_England