daily pastebin goal
84%
SHARE
TWEET

Untitled

a guest Jan 17th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type
  2.   Pnode = ^Node;
  3.   Node = record
  4.     count: integer;
  5.     next: Pnode;
  6.   end;
  7.  
  8. function CreateNode(NewCount: integer): Pnode;
  9. var
  10.   NewNode: Pnode;
  11. begin
  12.   new(NewNode);
  13.   NewNode^.count := NewCount;
  14.   NewNode^.next := nil;
  15.   result := NewNode;
  16. end;
  17. //пидоры
  18. procedure AddFirst(var head: Pnode; NewNode: Pnode);
  19. begin
  20.   NewNode^.next := head;
  21.   head := NewNode;
  22. end;
  23.  
  24. procedure AddAfter(p, NewNode: Pnode);
  25. begin
  26.   NewNode^.next := p^.next;
  27.   p^.next := NewNode;
  28. end;
  29.  
  30. procedure AddBefore(var head: Pnode; p, NewNode: Pnode);
  31. var
  32.   q: Pnode;
  33. begin
  34.   q := head;
  35.   if p = head then AddFirst(head, NewNode)
  36.   else
  37.   begin
  38.     while (q <> nil) and (q^.next <> p) do
  39.       q := q^.next;
  40.     if q <> nil then AddAfter(q, NewNode);
  41.   end;
  42. end;
  43.  
  44. function FindPlace(head: Pnode; Count: integer): Pnode;
  45. var
  46.   q: Pnode;
  47. begin
  48.   q := head;
  49.   while (q <> nil) and (q^.count < Count) do
  50.     q := q^.next;
  51.   result := q;
  52. end;
  53.  
  54. var
  55.   head, NewNode, q: Pnode;
  56.   w: string;
  57.   z: integer;
  58.   f: text;
  59.  
  60. begin
  61.   Head := nil;
  62.   Assign(f, 'input.txt');
  63.   reset(f);
  64.   readln(f, w);
  65.   z := StrToInt(w);
  66.   NewNode := CreateNode(z);
  67.   AddFirst(head, NewNode);
  68.  
  69.   While True do
  70.   begin
  71.     readln(f, w);
  72.     z := StrToInt(w);
  73.     if z = 0 then break
  74.     else
  75.     begin
  76.       NewNode := CreateNode(z);
  77.       q := FindPlace(head, z);
  78.       AddBefore(head, q, NewNode);
  79.     end;
  80.   end;
  81.   Close(f);
  82.  
  83.   q := head;
  84.   Assign(f, 'output.txt');
  85.   rewrite(f);
  86.   while q <> nil do
  87.   begin
  88.     write(f,q^.count,' ');
  89.     q := q^.next;
  90.   end;
  91.   close(f);
  92. end.
RAW Paste Data
Top