fork download
  1. program ideone;
  2.  
  3. type
  4. pel = ^el;
  5. el = record
  6. s: integer;
  7. p: pel;
  8. end;
  9.  
  10. function sortList(head: pel): pel;
  11. var cur: pel;
  12. procedure swap;
  13. var t: integer;
  14. begin
  15. t := cur^.p^.s;
  16. cur^.p^.s := cur^.s;
  17. cur^.s := t;
  18. end;
  19. begin
  20. cur := head;
  21. if head <> nil then
  22. while cur^.p <> nil do
  23. if cur^.s <= cur^.p^.s
  24. then cur := cur^.p
  25. else swap;
  26. sortList := head;
  27. end;
  28.  
  29. function readList(): pel;
  30. var n, i: byte;
  31. var p, head: pel;
  32. begin
  33. readln(n);
  34. if n = 0
  35. then begin
  36. head := nil;
  37. end
  38. else begin
  39. new(head);
  40. read(head^.s);
  41. p := head;
  42. for i := 2 to n do
  43. begin
  44. new(p^.p);
  45. p := p^.p;
  46. read(p^.s);
  47. end;
  48. end;
  49. readList := head;
  50. end;
  51.  
  52. procedure printList(head: pel);
  53. begin
  54. while head <> nil
  55. do begin
  56. write(head^.s, ' ');
  57. head := head^.p;
  58. end;
  59. end;
  60.  
  61. begin
  62. printList(sortList(readList()));
  63. end.
Success #stdin #stdout 0.01s 4112KB
stdin
5
1 3 2 5 4
stdout
1 2 3 4 5