Advertisement
huyhung94

Danh sách móc nối kép ( không đồ họa)

Sep 21st, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.92 KB | None | 0 0
  1. Program Baitap5;
  2. Uses crt;
  3. Type
  4.      contro=^truong;
  5.      truong=record
  6.            data:string[20];
  7.            Next,Prev :contro;
  8.      end;
  9. Var
  10.   Pdau,Pcuoi:contro;
  11.   chose : char;
  12. Procedure tao_ds;
  13. Var
  14.   dkien:STRING;
  15.   P,R:contro;
  16. Begin
  17. clrscr;
  18. Pdau :=nil;
  19. Repeat
  20. writeln('Bo trong du lieu de thoat.');
  21. write('nhap vao du lieu ');
  22. readln(dkien);
  23. if (dkien <> '') then
  24.    begin
  25.      new(p);
  26.      p^.data:=dkien;
  27.      if Pdau=nil then
  28.         begin
  29.            P^.Next :=nil;
  30.            P^.Prev :=nil;
  31.            Pdau:=P;
  32.            Pcuoi:=P;
  33.         end
  34.       else
  35.         begin
  36.            R:=Pcuoi;
  37.            R^.Next:=P;
  38.            P^.Next :=nil;
  39.            P^.prev :=R;
  40.            Pcuoi:=P;
  41.         end;
  42.       END;
  43. Until dkien='';
  44. end;
  45. Procedure inds;
  46. Var P: contro;
  47. Begin
  48.      clrscr;
  49.      if Pdau =nil then
  50.         write ('khong co du lieu de in ra')
  51.      else
  52.          begin
  53.               writeln('Danh sach du lieu duoc in ra theo chieu thuan');
  54.               P:=Pdau;
  55.               writeln;
  56.               write('Pdau--',#16);
  57.               Repeat
  58.                 write(P^.data:4);
  59.                 P:=P^.Next;
  60.                 write(' --',#16);
  61.               Until P=nil;
  62.               writeln('Nil');
  63.               writeln;
  64.               P:=Pdau;
  65.               write('Nil',#17,'--');
  66.  
  67.               Repeat
  68.                 write(p^.data:4,'  ');
  69.                 P:=P^.Next;
  70.                 write(#17,'--');
  71.               until P=nil;
  72.               write('Pcuoi');
  73.          End;
  74.          readln;
  75. End;
  76.  
  77. Procedure bo_sungds;
  78. Var
  79.    data1 :string;
  80.    P,Q,R: contro;
  81.    kt:boolean ;
  82. Begin
  83. clrscr;
  84.    write ('nhap du lieu cua con tro Q ');
  85.    readln(data1); P:=Pdau;
  86.    kt:=false;
  87.    repeat
  88.        if data1 =P^.data then
  89.           begin
  90.              kt:=true;
  91.              new(Q);
  92.              write('nhap vao du lieu bo sung');
  93.              readln(Q^.data);
  94.              if P=Pdau then
  95.                  begin
  96.                      Pdau:=Q;
  97.                      Q^.Next:=P;
  98.                      P^.Prev:=Q;
  99.                  end
  100.              else
  101.                  begin
  102.                      R:=P^.Prev;
  103.                      Q^.Next:=R^.next;
  104.                      R^.next:=Q;
  105.                      Q^.Prev:=P^.Prev;
  106.                      P^.prev:=Q;
  107.                  end;
  108.           end;
  109.        P:=P^.Next;
  110.    until P=nil;
  111.    writeln('**************ket thuc bo sung ************');
  112.    if kt=false then write('khong tim thay du lieu vua nhap');
  113.    readln;
  114. end;
  115. Begin
  116.    Repeat
  117.    CLRSCR;
  118.    writeln;
  119.    writeln('***TUY CHON CHUC NANG ***');
  120.    writeln('1 nhap vao danh sach');
  121.    writeln('2 in ra danh sach theu chieu thuan ');
  122.    writeln('3 bo sung danh sach ');
  123.    writeln('4 save and exit ');
  124.    writeln('Chuc nang ban chon: ');
  125.    readln(chose);
  126.    case chose of '1':tao_ds;
  127.                  '2':inds;
  128.                  '3':bo_sungds;
  129.    end;
  130.    until chose ='4';
  131. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement