Advertisement
niepok

lista dwukierunkowa z sortowaniami

Jan 25th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 9.97 KB | None | 0 0
  1. program ldwukierunkowa;
  2. uses crt;
  3. const N=100;
  4. type
  5.   ListaPtr=^Lista;
  6.   Lista = record
  7.     value : integer;
  8.     next : ListaPtr;
  9.     prev : ListaPtr;
  10.   end;
  11. procedure Init(var head : ListaPtr; var tail : ListaPtr; var current : ListaPtr);
  12.           begin
  13.                head:=NIL;
  14.                tail:=NIL;
  15.                current:=NIL;
  16.           end;
  17. procedure DodajNaPoczatek(var head : ListaPtr; var tail : ListaPtr; var current : ListaPtr);
  18.           var
  19.              nowy : ListaPtr;
  20.           begin
  21.                New(nowy);
  22.                nowy^.value:=random(N);
  23.                if head = NIL then
  24.                begin
  25.                     nowy^.next:=NIL;
  26.                     nowy^.prev:=NIL;
  27.                     head:=nowy;
  28.                     tail:=nowy;
  29.                     current:=nowy;
  30.                end else
  31.                begin
  32.                     nowy^.next:=head;
  33.                     head^.prev:=nowy;
  34.                     nowy^.prev:=NIL;
  35.                     head:=nowy;
  36.                     current:=nowy;
  37.                end;
  38.           end;
  39. procedure DodajNaKoniec(var head : ListaPtr; var tail : ListaPtr; var current : ListaPtr);
  40.           var
  41.              nowy : ListaPtr;
  42.              i,j : integer;
  43.           begin
  44.                writeln('Ile liczb chcesz dodac : ');
  45.                readln(i);
  46.                for j:=1 to i do
  47.                begin
  48.                New(nowy);
  49.                nowy^.value:=random(N);
  50.                if tail=NIL then
  51.                begin
  52.                     nowy^.next:=NIL;
  53.                     nowy^.prev:=NIL;
  54.                     head:=nowy;
  55.                     tail:=nowy;
  56.                     current:=nowy;
  57.                end else
  58.                begin
  59.                     nowy^.next:=NIL;
  60.                     nowy^.prev:=tail;
  61.                     tail^.next:=nowy;
  62.                     tail:=nowy;
  63.                     current:=nowy;
  64.                end;
  65.                end;
  66.           end;
  67. procedure UsunKoniec(var head : ListaPtr; var tail : ListaPtr; var current : ListaPtr);
  68.           var
  69.              tmp : ListaPtr;
  70.           begin
  71.                if head=NIL then
  72.                   writeln('Lista jest pusta!')
  73.                else if head^.next=NIL then
  74.                begin
  75.                     head:=NIL;
  76.                     tail:=NIL;
  77.                end else
  78.                begin
  79.                     tmp:=tail;
  80.                     tail:=tmp^.prev;
  81.                     tail^.next:=NIL;
  82.                     dispose(tmp);
  83.                end;
  84.           end;
  85. procedure Erase(var head : ListaPtr; var tail : ListaPtr; var current : ListaPtr);
  86.           var
  87.              tmp : ListaPtr;
  88.           begin
  89.                while head<>NIL do
  90.                begin
  91.                     tmp:=head;
  92.                     head:=tmp^.next;
  93.                     dispose(tmp);
  94.                end;
  95.                head:=NIL;
  96.                tail:=NIL;
  97.           end;
  98. procedure Print(var head : ListaPtr);
  99.           var
  100.              tmp : ListaPtr;
  101.           begin
  102.                if head=NIL then writeln('Lista jest pusta')
  103.                else
  104.                begin
  105.                     tmp:=head;
  106.                     while tmp<>NIL do
  107.                     begin
  108.                          writeln(tmp^.value);
  109.                          tmp:=tmp^.next;
  110.                     end;
  111.                end;
  112.           end;
  113. function amount(var head : ListaPtr):integer;
  114.          var
  115.             licznik : integer =0;
  116.             tmp : ListaPtr;
  117.          begin
  118.               tmp:=head;
  119.               while tmp<>NIL do
  120.               begin
  121.                    licznik+=1;
  122.                    tmp:=tmp^.next;
  123.               end;
  124.               amount:=licznik;
  125.          end;
  126. procedure BubbleSort(var head : ListaPtr);
  127.           var
  128.              i,j,m: integer;
  129.              tmp, it2, it3 : ListaPtr;
  130.              zmiana : boolean;
  131.           begin
  132.                m:=amount(head);
  133.                j:=m-1;
  134.                repeat
  135.                         zmiana:=false;
  136.                         it2:=head;
  137.                         if head<>NIL then it3:=head^.next;
  138.                         for i:=1 to j do
  139.                         begin
  140.                              if it2^.value>it3^.value then
  141.                              begin
  142.                                   zmiana:=true;
  143.                                   if it2^.prev <> NIL then
  144.                                   begin
  145.                                        it2^.prev^.next:=it3;
  146.                                        it3^.prev:=it2^.prev;
  147.                                   end else
  148.                                   begin
  149.                                        head:=it3;
  150.                                        head^.prev:=NIL;
  151.                                   end;
  152.                                   tmp:=it3^.next;
  153.                                   it3^.next:=it2;
  154.                                   it2^.next:=tmp;
  155.                                   if tmp <> NIL then tmp^.prev:=it2;
  156.                                   it2^.prev:=it3;
  157.                                   it3:=it2;
  158.                              end;
  159.                              it2:=it3;
  160.                              it3:=it3^.next;
  161.                         end;
  162.                         Dec(j);
  163.                until (j<1) or not(zmiana);
  164.                Print(head);
  165.           end;
  166. procedure SelectionSort(var head: ListaPtr);
  167.           var
  168.              it1, it2 ,min, tmp : ListaPtr;
  169.           begin
  170.                it1:=head;
  171.                while it1^.next <> NIL do
  172.                begin
  173.                     it2:=it1;
  174.                     min:=it1;
  175.                     while it2^.next<> NIL do
  176.                     begin
  177.                          if it2^.next^.value < min^.value then
  178.                          begin
  179.                               min:=it2^.next;
  180.                          end;
  181.                          it2:=it2^.next;
  182.                     end;
  183.                     if min<>it1 then
  184.                     begin
  185.                          if it1^.prev=NIL then head:=min;
  186.                          if min^.prev=it1 then
  187.                          begin
  188.                               min^.prev:=it1^.prev;
  189.                               it1^.next:=min^.next;
  190.                               min^.next:=it1;
  191.                               it1^.prev:=min;
  192.                               if min^.prev <> NIL then min^.prev^.next := min;
  193.                               if it1^.next <> NIL then it1^.next^.prev := it1;
  194.                          end else
  195.                          begin
  196.                               tmp:=min^.prev;
  197.                               min^.prev:=it1^.prev;
  198.                               if min^.prev <> NIL then min^.prev^.next := min;
  199.                               it1^.prev:=tmp;
  200.                               tmp^.next:=it1;
  201.                               tmp:=min^.next;
  202.                               min^.next:=it1^.next;
  203.                               min^.next^.prev:=min;
  204.                               it1^.next:=tmp;
  205.                               if tmp<>NIL then tmp^.prev:=it1;
  206.                          end;
  207.                          it1:=min;
  208.                     end;
  209.                     it1:=it1^.next;
  210.                end;
  211.                Print(head);
  212.           end;
  213. procedure InsertionSort(var head : ListaPtr);
  214.           var
  215.              nowy, it, it2, newhead : ListaPtr;
  216.           begin
  217.                it := head;
  218.                if it <> NIL then
  219.                begin
  220.                     newhead := it;
  221.                     it := it^.next;
  222.                     newhead^.next := NIL;
  223.                     newhead^.prev := NIL;
  224.                end;
  225.                while it <> NIL do
  226.                begin
  227.                     nowy := it;
  228.                     it := it^.next;
  229.                     it2 := newhead;
  230.                     if(it2^.value > nowy^.value) then
  231.                     begin
  232.                          nowy^.next := it2;
  233.                          it2^.prev := nowy;
  234.                          newhead := nowy;
  235.                          newhead^.prev := NIL;
  236.                     end else
  237.                     begin
  238.                          while (it2^.next <> NIL) and (it2^.next^.value < nowy^.value) do it2 := it2^.next;
  239.                          nowy^.next := it2^.next;
  240.                          if it2^.next <> NIL then it2^.next^.prev := nowy;
  241.                          nowy^.prev := it2;
  242.                          it2^.next := nowy;
  243.                     end;
  244.                end;
  245.                head := newhead;
  246.                Print(head);
  247.           end;
  248. function menu:char;
  249.          begin
  250.               clrscr;
  251.               writeln('[1] - Dodaj zadanie na poczatek listy');
  252.               writeln('[2] - Dodaj zadanie na koniec listy');
  253.               writeln('[3] - Wyswietl liste zadan');
  254.               writeln('[4] - Usun zadanie z konca listy');
  255.               writeln('[5] - Posortuj liste zadan babelkowo');
  256.               writeln('[6] - Posortuj liste zadan przez wybor');
  257.               writeln('[7] - Posortuj liste zadan przez wstawienie');
  258.               writeln('[8] - Wyczysc liste');
  259.               writeln('[Esc] - Wyjscie z programu');
  260.               menu := readkey;
  261.          end;
  262. var
  263.    head, tail, current: ListaPtr;
  264. begin
  265.   randomize;
  266.   Init(head, tail, current);
  267.   repeat
  268.            begin
  269.                 clrscr;
  270.                 case menu of
  271.                      '1':DodajNaPoczatek(head, tail, current);
  272.                      '2':DodajNaKoniec(head, tail, current);
  273.                      '3':Print(head);
  274.                      '4':UsunKoniec(head, tail, current);
  275.                      '5':BubbleSort(head);
  276.                      '6':SelectionSort(head);
  277.                      '7':InsertionSort(head);
  278.                      '8':Erase(head, tail, current);
  279.                      #27:halt;
  280.                 end;
  281.                 writeln;
  282.                 writeln('Aby wrocic do menu nacisnij [ENTER]');
  283.                 readln;
  284.            end;
  285.      until FALSE;
  286.      end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement