Tải bản đầy đủ (.doc) (8 trang)

BÀI TẬP CẤU TRÚC DỮ LIỆU

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (78.67 KB, 8 trang )

BÀI TẬP CẤU TRÚC DỮ LIỆU
BÀI 1_3.
{---------------TRON 2 DANH SACH ----------}
Procedure MergerList(L1,L2:List;Var L:List);
Var Q,P,T: List;
Begin
sapxep(L1);
sapxep(L2);
P:=first(L1);
{ CACH 1: while P<>end_L(L1) do
begin
insert_L(retrieve(P,L1),End_L(L),L);
P:=P^.next;
end;
Q:=L2;
while Q<>end_L(L2) do
begin
T:=first(L);
while (T<>nil) and (retrieve(T,L)<retrieve(Q,L2)) do
T:=T^.next;
insert_L(retrieve(Q,L2),T,L);
Q:=Q^.next;
end; }
P:=First(L1);q:=FIRST(L2);
while (P<>End_L(L1)) and (Q<>End_L(L2)) do
if retrieve(P,L1)<Retrieve(Q,L2) then
begin
insert_L(retrieve(P,L1),End_L(L),L);
P:=P^.next;
end
else


begin
insert_L(retrieve(Q,L2),End_L(L),L);
Q:=Q^.next;
end ;
while P<>end_L(L1) do
begin
insert_L(retrieve(P,L1),End_L(L),L);
P:=P^.next;
end;
while Q<>end_L(L2) do
begin
insert_L(retrieve(Q,L2),End_L(L),L);
Q:=Q^.next;
end;
End;
{---------------TRON 2 DANH SACH ----------}
Procedure MergerList(L2:List;Var L1:List);
Var Q,P,T: List;
Begin
Q:=first(L2);
while Q<>end_L(L2) do
begin
T:= L1^.next;
while (T<>nil) and (retrieve(T,L1)<retrieve(Q,L2)) do
T:=T^.next;
insert_L(retrieve(Q,L2),T,P);
Q:=Q^.next;
end;
End;
{--------TRON N DANH SACH ---------}

Procedure Merger_nl(N_List:mang; m: integer; Var L:List);
Var i:integer;
Begin
L:=N_List[1];
for i:=2 to m do
mergerlist(N_List[i],L);
End;
{-------- NHAP N DANH SACH -------}
Procedure READ_NL(Var N_List:mang);
Var j:integer;
Begin
write('Co bao nhieu danh sach can tron: ');readln(m);
for j:=1 to m do
begin
makenull(N_List[j]);
writeln('------- DANH SACH THU ',j,' ------');
readlist(N_List[j]);
sapxep(N_List[j]);
WRITELN;
end;
End;
BÀI 1_5:
uses crt;
Type Elementype=integer;
Position=^List;
List=record
heso,somu:elementype;
next:Position;
end;
Var A, B, C: position;

{--------THEM PHAN TU VAO DA THUC --------------}
Procedure Insert(c,e:elementype; Var A: position);
Var Q,P:position;
Begin
new(P);p^.heso:=c;P^.somu:=e;P^.next:=nil; {tao nut tam}
Q:=A;
while (Q^.next<>nil) and (Q^.next^.somu>e) do
Q:=Q^.next;
If Q^.next=nil then Q^.next:=P
else
if Q^.next^.somu=e then
Q^.next^.heso:=Q^.next^.heso+C
else
begin
P^.next:=Q^.next;
Q^.next:=P;
end;
End;
{---------- NHAN DA THUC -----------}
Procedure Add(D1,D2: position; Var P:position);
Var p1, p2: Position;x,y:elementype;
Begin
p1:=first(D1);p2:=first(D2);
makenull(P);
while (P1<>end_L(D1)) and (P2<>end_L(D2)) do
if p1^.next^.somu>P2^.next^.somu then
begin
insert(P1^.next^.heso,P1^.next^.somu,P);
P1:=P1^.next;
end

else
if p1^.next^.somu<P2^.next^.somu then
begin
insert(P2^.next^.heso,P2^.next^.somu,P);
P2:=P2^.next;
end
else
begin
x:=P1^.next^.heso+P2^.next^.heso;
y:=P1^.next^.somu;
insert(x,y,P);
P1:=P1^.next;P2:=P2^.next;
end;
while P1<>end_L(D1) do
begin
insert(P1^.next^.heso,P1^.next^.somu,P);
P1:=P1^.next;
end;
while P2<>end_L(D2) do
begin
insert(P2^.next^.heso,P2^.next^.somu,P);
P2:=P2^.next;
end;
End ;
{--------- TINH TONG HAI DA THUC ----------}
Procedure SUM_DT(A, B: Position; Var C: Position);
Var T, K : Position;
Begin
makenull(C);
T:=A;

while T^.next<>nil do
begin
insert(T^.next^.heso,T^.next^.somu,C);
T:=T^.next;
end;
K:=B;
while K^.next<>nil do
begin
insert(K^.next^.heso,K^.next^.somu,C);
K:=K^.next;
end;
End;
{---------- NHAN HAI DA THUC ------------}
Procedure Nhan_DT(A: Position; B: Position; Var C: Position);
Var T1,T2: Position; D:Position;mu,he:elementype;
Begin
T1:=first(A);makenull(C);
While T1<>end_l(T1) do
begin
makenull(D);
T2:=first(B);
while T2<>end_l(T2) do
begin
he:=T1^.next^.heso*T2^.next^.heso ;
mu:=T1^.next^.somu+T2^.next^.somu;
insert(he,mu,D);
T2:=T2^.next;
end;
add(D,C,C);
T1:=T1^.next;

end;
End;
{----------DAO HAM CUA DA THUC ----------}
Function DaoHam(A: Position):Position;
Var K, P, Q: Position;
Begin
makenull(K);
P:=K;
Q:=A;
while Q^.next<>nil do
begin
new(P^.next);
P^.next^.heso:=Q^.next^.heso*Q^.next^.heso;
P^.next^.somu:=Q^.next^.somu-1;
Q:=Q^.next;
P:=P^.next;
end;
daoham:=k;
End;
BÀI 1_6: THỰC HIỆN CỘNG 1 VÀO DÃY SỐ NHỊ PHÂN
Type
b=0..1;
Position=^Cell;
Cell=Record
bit:0..1;
next:Position;
end;
Procedure INCREMENT(Var Bnumber: Position; Q: Position);
Var K,P: position;
Begin

P:=Bnumber;
while P^.next<>Q do P:=P^.next;
if P=Bnumber then
begin
new(K); K^.bit:=1;
K^.next:=bnumber^.next;
Bnumber^.next:=k;
end
else
if P^.bit=0 then P^.bit:=1
else
begin
P^.bit:=0; increment(Bnumber,P);
end;
End;
BÀI 1_9
Const max_T=100;
Type
Elementype= char;
Node=integer;
Tree = Record
label_T: array[1..max_t] of elementype;
Parent: array[1..max_t] of Node;
max_node:integer;
end;
Var T: Tree;
{----- CHIEU CAO CUA CAY -------}
Function Max(a,b:node):node;
Begin
if a>b then max:=a else max:=b;

end;
Function Height(n:node;T: Tree): integer;
Var h:integer;m:node;
Begin
if empty(T) then writeln('Cay rong!')
else
begin
h:=0;
m:=LEFTMOST_CHILD(n,T);
while m<>0 do
begin
h:=max(h,height(m,T));
m:=RIGHT_SIBLING(m,T);
end;
height:=h+1;
end;
end;
BÀI 2_2
{----- DUYET DUONG DI CUA 1 NUT VE GOC ------}
Procedure DD(m:node;VAR q: integer; Var A: mang;T:Tree);
Var k: node;j:integer;
Begin
k:=T.parent[m];
j:=0;
while k<>parent_T(Root(T),T) do
begin
a[j]:=k;
k:=T.parent[k];
j:=j+1;

×