[Pascal]Some operations in the queue – code Pascal
By the way, have you by doing all of the queue, its always post up here.
In all there 8 operations:
1. Check Empty: If the number of elements (size) the queue = 0 it returns true, opposite flase
2. Check full: If the number of elements (size) của queue = Max (The maximum number of elements of the queue) it returns true, opposite flase
3. Enter: As usual array entry: enter the number of elements, then enter the elements in loop
4. Export: browse by loop and output.
5. Insert: inserted into the line by increasing the number of elements (size) to 1, then assign the last element of x to insert
6. Erase: Delete where every element: Using the variable x to save the element, then move the elements from the first 2 onwards ago 1 unit, reduce the number of elements xuogns 1
7. Fix in position k. Assign element at position k the new value.
8. Fixing element x of y.
+/ Building search function in every element x: use a for loop to find the location of x, found, it returns the position.
+/ Use the edit function in position to fix the element k y.
program hangdoi; uses crt; const Max = 100; {hang doi co toi da 100 phan tu} type queue = record A:array[1..Max] of integer; size : integer; End; var Q:queue; x,y,k,chon:integer; function KTRong(Q:queue):boolean; {kiem tra rong} begin if Q.size = 0 then KTRong := true else KTRong := false; end; function KTDay(Q:queue):boolean; {kiem tra day} begin if Q.size = Max then KTDay := true else KTDay := false; end; procedure nhap(var Q:queue); {nhap hang doi} var i:integer; begin write('Nhap so phan tu cua hang doi : '); readln(Q.size); for i:=1 to Q.size do begin write('Nhap phan tu thu ',i,' : '); readln(Q.A[i]); end; end; procedure xuat(Q:queue); {xuat hang doi} var i:integer; begin for i:=1 to Q.size do write(Q.A[i],' '); end; procedure chen(var Q:Queue; x:integer); {chen phan tu vao cuoi hang} var i:integer; begin if KTday(Q) then writeln('hang day, khong the chen them !') else begin Q.size :=Q.size + 1; Q.A[Q.size] := x; writeln('Chen thanh cong !'); end; end; procedure xoa(var Q:queue; var x:integer); {xoa phan tu} var i:integer; begin if KTRong(Q) then writeln('Hang rong, khong the xoa !') else begin x := Q.A[1]; for i:=1 to Q.size-1 do Q.A[i] := Q.A[i+1]; Q.size := Q.size - 1; writeln('Xoa thanh cong !'); end; end; function tim_x(Q:queue; x:integer):integer; var i:integer; begin if KTRong(Q) then tim_x := 0 else begin for i:=1 to Q.size do if Q.A[i] = x then begin tim_x := i; break; end; end; end; procedure sua_k(var Q:queue; var x:integer; k:integer); {sua phan tu o vi tri k thanh x} var i:integer; begin if (k>Q.size+1) or (k<1) then writeln('Vi tri khong hop le !') else begin Q.A[k] := x; writeln('Sua thanh cong o vi tri ',k,' !'); end; end; procedure sua_x(var Q:queue; var x:integer; y:integer); {sua phan tu x thanh y} var i,j:integer; begin j := tim_x(Q,x); if j = 0 then writeln('Khong tim thay x !') else begin while (j<>0) do begin sua_k(Q,y,j); j := tim_x(Q,x); end; end; end; BEGIN writeln('1: Kiem tra hang rong khong'); writeln('2: Kiem tra hang day khong'); writeln('3: Nhap hang doi'); writeln('4: Xuat hang doi'); writeln('5: Chen phan tu trong hang'); writeln('6: Xoa phan tu trong hang'); writeln('7: Sua phan tu o vi tri k trong hang'); writeln('8: Sua phan tu x trong hang thanh y'); writeln('9: Thoat'); while chon <> 9 do begin writeln; writeln('Chon mot trong cac so de lam :'); readln(chon); case chon of 1: begin if KTRong(Q) then writeln('Hang doi rong !') else writeln('Hang khong rong !'); end; 2: begin if KTDay(Q) then writeln('Hang doi day !') else writeln('Hang khong day !'); end; 3: begin nhap(Q); writeln('Nhap thanh cong !'); end; 4: begin writeln('Cac phan tu trong hang doi :'); xuat(Q); end; 5: begin write('Nhap phan tu can chen :'); readln(x); chen(Q,x); end; 6: xoa(Q,x); 7: begin write('Nhap vi tri can sua :'); readln(k); write('Sua thanh phan tu nao ? '); readln(x); sua_k(Q,x,k); end; 8: begin write('Nhap phan tu can sua :'); readln(x); writeln('Sua ',x,' thanh phan tu nao ? '); readln(y); sua_x(Q,x,y); end; end; end; readln; END.
Recent Comments