Guest User

Untitled

a guest
Aug 5th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.28 KB | None | 0 0
  1. unit Mylist;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. interface
  6.  
  7. uses  SysUtils;
  8.  
  9.   type
  10.   TInfo   = string;
  11.   PElement = ^TElement;
  12.   TElement=record
  13.     Info: TInfo;
  14.     Next: PElement;
  15.     index: integer;
  16.   end;
  17.     Tlist = record
  18.     head, cur, tail: PElement;
  19.     end;
  20.  
  21.   procedure list_create(var Plist: tlist);                // sozdanie spiska
  22.   procedure list_insert(var plist: tlist; s: string);     // vstavka v spisok
  23.   procedure list_index(var Plist: tlist; ind: integer);   // perenos ukazatelia na element s ukazannim indexom
  24.   procedure list_show(var Plist: tlist);                  // otobrajenie spiska
  25.   procedure list_add(var Plist: tlist);                   // dobavlenie elementa v spisok
  26.   procedure list_delete(var Plist: tlist);                // udalenie elementa iz spiska
  27.   procedure list_element(var Plist: tlist);               // otobrajenie indexa ukazannogo elementa
  28.   procedure list_save(var Plist: tlist);                  // sohranenie dannih iz spiska
  29.   procedure list_sort(var Plist: tlist);                  // sortirovka po dline stroki
  30.  
  31. implementation
  32.  
  33. procedure list_index(var plist: tlist; ind: integer);
  34. begin
  35.   plist.cur:=plist.head;
  36.   while plist.cur.index<ind do plist.cur:=(plist.cur)^.Next;
  37. end;
  38.  
  39. procedure list_create(var plist:tlist);
  40. begin
  41.   new(plist.cur);
  42.   plist.cur.index:=1;
  43.   plist.cur.next:=nil;
  44.   plist.head:=plist.cur;
  45.   plist.tail:=plist.cur;
  46. end;
  47.  
  48. procedure list_insert(var plist: tlist; s: string);
  49. begin
  50.   if s<>'' then
  51.   begin
  52.     if plist.head.info='' then
  53.     begin
  54.       (Plist.cur)^.Info:=s;
  55.       plist.head:=plist.cur;
  56.       (Plist.cur)^.Next:=nil;
  57.     end
  58.     else
  59.     begin
  60.       New(Plist.cur);
  61.       (Plist.cur)^.Info:=s;
  62.       plist.cur.index:=plist.tail.index+1;
  63.       (Plist.cur).Next:=nil;
  64.       (Plist.tail)^.Next:=(Plist.cur);
  65.       Plist.tail:=Plist.cur;
  66.     end;
  67.   end;
  68. end;
  69.  
  70. procedure list_show(var plist:tlist);
  71. begin
  72.   begin
  73.     if not(plist.head=nil) then
  74.     begin
  75.     Plist.cur:=Plist.head;
  76.     writeln;
  77.     begin
  78.     while Plist.cur<>nil do
  79.     begin
  80.       writeln ((Plist.cur)^.info, ' ');
  81.       Plist.cur:=(Plist.cur)^.next;
  82.     end;
  83.     end
  84.     end
  85.     else
  86.     begin
  87.       writeln;
  88.       writeln('Spisok pust!');
  89.     end;
  90.   end;
  91. end;
  92.  
  93. procedure list_add(var plist: tlist);
  94. var
  95. NewEl, el: string;
  96. poisk: boolean;
  97. q: Pelement;
  98. begin
  99.     writeln;
  100.     writeln('element?');
  101.     readln(NewEl);
  102.     if plist.head=nil then
  103.     begin
  104.       New(Plist.cur);
  105.       (Plist.cur)^.Info:=newel;
  106.       (Plist.cur).Next:=nil;
  107.       plist.cur.index:=1;
  108.       Plist.head:=(Plist.cur);
  109.       plist.tail:=plist.cur;
  110.       writeln;
  111.       writeln('Gotovo!');
  112.     end
  113.     else
  114.     begin
  115.     writeln('Pered kakim elementom?');
  116.     readln (EL);
  117.     Plist.cur:=Plist.head;
  118.     poisk:=false;
  119.     while ((Plist.cur)<>nil) and not(poisk) do
  120.     begin
  121.       if (Plist.cur)^.info=el then poisk:=true
  122.       else
  123.         Plist.cur:=(Plist.cur)^.next;
  124.     end;
  125.     if poisk then
  126.     begin
  127.       new(q);
  128.       q^.Info:=(Plist.cur)^.info;
  129.       (Plist.cur)^.info:=newel;
  130.       q^.Next:=(Plist.cur)^.Next;
  131.       (Plist.cur)^.next:=q;
  132.       q.index:=plist.cur.index;
  133.       if plist.cur=plist.tail then plist.tail:=q;
  134.       while plist.cur.next<>nil do
  135.       begin
  136.         plist.cur.next.index:=plist.cur.next.index+1;
  137.         plist.cur:=plist.cur.next;
  138.       end;
  139.       writeln;
  140.       writeln('Gotovo!');
  141.     end
  142.     else begin writeln; writeln ('Net takogo elementa'); end;
  143.     end;
  144. end;
  145.  
  146. procedure list_delete (var plist:tlist);
  147. var el: string;
  148. poisk, poisk1, poisk2: boolean;
  149. q: Pelement;
  150. tmpind: integer;
  151. begin
  152.     if not(plist.head=nil)  then
  153.     begin
  154.       writeln;
  155.       writeln ('Kakoi element?');
  156.       readln (el);
  157.       Plist.cur:=Plist.head;
  158.       poisk:=false;
  159.       poisk1:=false;
  160.       poisk2:=false;
  161.       if (Plist.head).info=el then poisk1:=true;
  162.       if (plist.tail).info=el then poisk2:=true;
  163.       while ((Plist.cur)^.Next<>nil) and not(poisk) and not(poisk1) and not(poisk2) do
  164.       begin
  165.         if ((Plist.cur)^.next)^.info=el then poisk:=true
  166.         else
  167.         Plist.cur:=(Plist.cur)^.next;
  168.       end;
  169.       if poisk then // udalenie ukazannogo elementa
  170.       begin
  171.         if plist.cur.Next.index<>(plist.tail.index-1) then
  172.         begin
  173.           q:=(Plist.cur)^.next;
  174.           tmpind:=1;
  175.           list_index(plist, (plist.tail.index-tmpind));
  176.           plist.cur.Next.index:=plist.cur.index;
  177.           repeat
  178.           begin
  179.             plist.cur:=plist.head;
  180.             list_index (plist, plist.tail.index-tmpind);
  181.             plist.cur.Next.index:=plist.cur.index;
  182.             tmpind:=tmpind+1;
  183.           end;
  184.           until (plist.tail.index-tmpind)=q.index-1;
  185.           list_index (plist, q.index-1);
  186.           (Plist.cur)^.Next:=q^.Next;
  187.           Dispose(q);
  188.           writeln;
  189.           writeln('Gotovo!');
  190.         end
  191.         else
  192.         begin
  193.           q:=plist.cur.next;
  194.           plist.cur.Next:=plist.tail;
  195.           plist.tail.index:=q.index;
  196.           dispose(q);
  197.           writeln;
  198.           writeln('Gotovo!');
  199.         end;
  200.       end
  201.       else
  202.       begin
  203.         if poisk1 then  // udalenie pervogo elementa v spiske
  204.         begin
  205.           q:=plist.head;
  206.           if plist.head.next<>nil then
  207.           begin
  208.             plist.cur:=plist.head;
  209.             repeat
  210.             begin
  211.               plist.cur:=plist.cur.Next;
  212.               if plist.cur<>nil then plist.cur.index:=plist.cur.index-1;
  213.             end;
  214.             until plist.cur=nil;
  215.             plist.head:=q.next;
  216.             dispose(q);
  217.           end
  218.           else
  219.           begin
  220.             q:=plist.head;
  221.             plist.head:=nil;
  222.             dispose(q);
  223.           end;
  224.           writeln;
  225.           writeln('Gotovo!');
  226.         end
  227.         else
  228.         if poisk2 then // óudalenie poslednego elementa v spiske
  229.         begin
  230.           list_index(plist, plist.tail.index-1);
  231.           q:=plist.cur.Next;
  232.           plist.tail:=plist.cur;
  233.           plist.tail.next:=nil;
  234.           dispose(q);
  235.           writeln;
  236.           writeln('Gotovo!');
  237.         end
  238.         else
  239.         if not(poisk1) and not(poisk2) then
  240.         begin
  241.           writeln;
  242.           writeln ('Net takogo elementa!');
  243.         end;
  244.       end;
  245.     end
  246.     else
  247.     begin
  248.       writeln;
  249.       writeln('Spisok pust');
  250.     end;
  251. end;
  252.  
  253. procedure list_element (var plist:tlist);
  254. var el: string;
  255. poisk: boolean;
  256. begin
  257. if not(plist.head=nil) then
  258.   begin
  259.     writeln;
  260.     writeln ('Kakoi element?');
  261.     readln (el);
  262.     Plist.cur:=Plist.head;
  263.     poisk:=false;
  264.     if (Plist.cur)^.info=el then poisk:=true;
  265.     while ((Plist.cur)<>nil) and not(poisk) do
  266.     if (Plist.cur)^.Info=el then poisk:=true
  267.     else
  268.     Plist.cur:=(Plist.cur)^.Next;
  269.     if poisk then
  270.     begin
  271.       writeln;
  272.       writeln ('Poriadkovii nomer elementa= ', plist.cur.index)
  273.     end
  274.     else
  275.     begin
  276.       writeln;
  277.       writeln ('Net takogo elementa');
  278.     end;
  279.   end
  280.   else
  281.   begin
  282.     writeln;
  283.     writeln('Spisok pust!');
  284.   end;
  285. end;
  286.  
  287. procedure list_save( var plist:tlist);
  288. var f1: textfile;
  289. s1: string;
  290. begin
  291.     AssignFile (f1, 'itog.txt');
  292.     erase(f1);
  293.     rewrite(f1);
  294.     Plist.cur:=Plist.head;
  295.     if not(plist.head=nil) then
  296.     while (Plist.cur)<>nil do
  297.     begin
  298.       s1:=((Plist.cur)^.Info);
  299.       writeln (f1, s1);
  300.       Plist.cur:=(Plist.cur)^.Next;
  301.     end;
  302.     closefile (f1);
  303.     writeln;
  304.     writeln('Gotovo!');
  305. end;
  306.  
  307. procedure list_sort (var plist:tlist);
  308. var i, j: integer;
  309. key: string;
  310. begin
  311.     plist.cur:=plist.head;
  312.     if (plist.cur<>nil) and (not(plist.head=nil))
  313.     then
  314.     begin
  315.       for i:= 2 to plist.tail.index do
  316.       begin
  317.         list_index(plist, i);
  318.         key:=(plist.cur)^.Info;
  319.         j:=i-1;
  320.         list_index(plist, j);
  321.         while (j>0) and (length(plist.cur^.info)>length(key)) do
  322.         begin
  323.           list_index(plist, j);
  324.           (plist.cur)^.Next^.info := (plist.cur)^.Info;
  325.           j:=j-1;
  326.           list_index(Plist, j);
  327.         end;
  328.         if j=0 then plist.cur.info:=key
  329.         else
  330.         (plist.cur)^.Next^.info := key;
  331.       end;
  332.       writeln;
  333.       writeln ('Gotovo!');
  334.     end
  335.     else
  336.     begin
  337.      writeln;
  338.      writeln('Spisok pust!');
  339.     end;
  340. end;
  341.  
  342. end.
Add Comment
Please, Sign In to add comment