1. Trang chủ
  2. » Luận Văn - Báo Cáo

CÁC KIỂU DỮ DIỆU TRỪU TƯỢNG

23 438 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 23
Dung lượng 132,5 KB

Nội dung

CÁC KIỂU DỮ DIỆU TRỪU TƯỢNG

1 CÁC KIỂU DỮ DIỆU TRỪU TƯỢNG I. DANH SÁCH {------------ THUC THI DANH SACH BANG MANG (DS DAC) ------------} Uses CRT; Const max=100; Type Datatype=integer; List=record data:array[1 max] of DataType; Last:integer; End; Var L : List;i,n,x:integer; {--------- TAO DANH SACH RONG --------} Procedure Makenull(Var L: List); Begin L.Last:=0; End; {---------- KIEM TRA DANH SACH RONG --------} Function Empty(L:List):boolean; Begin empty:=L.Last=0; End; {---------- KIEM TRA DANH SACH DAY ----------} Function Full(L:List):boolean; Begin Full:=L.Last>max; End; {----------- TRA VE VI TRI PHAN TU SAU PT CUOI CUNG ------} Function End_List(L:List): integer; Begin End_List:=L.Last+1; End; {---------- TRA VE VI TRI PHAN TU DAU TIEN -----------} Function First(L:List):integer; Begin First:=1; End; {----------- TRA VE VI TRI PHAN TU SAU PHAN TU P ---------} Function Next(P:integer;L:List):integer; Begin Next:=P+1; End; {----------- TRA VE VI TRI PHAN TU TRUOC PHAN TU P ---------} Function Previous(P:integer;L:List):integer; Begin Previous:=P-1; End; {----------- TRA VE VI TRI PHAN TU CUOI CUNG ---------} 2 Function Last(L:List):integer; Begin Last:=L.Last; End; {---------- TIM PHAN TU CO GIA TRI LA X -----------} Function Local(x:datatype;L:List):integer; Var i,tim:integer; Begin tim:=0; for i:=1 to L.Last do if L.data[i]=x then tim:=i; Local:=tim; End; {---------- THEM PHAN TU VAO DANH SACH TAI VI TRI P ----------} Procedure Insert(x:integer;P:integer; Var L:List); Var q:integer; Begin if Full(L) then writeln('Danh sach day!') else if (P<1) and (p>L.Last) then writeln('P is out position!') else begin For q:=L.Last+1 downto p+1 do L.data[q]:=L.data[q-1]; L.Last:=L.Last+1; L.data[p]:=x; end; End; {------------ XOA PHAN TU TAI VI TRI P ----------} Procedure Delete(p:integer;Var L:List); Var i: integer; Begin if Full(L) then writeln('Danh sach day!') else if (P<1) and (p>L.Last) then writeln('P is out position!') else begin For i:=L.Last-1 downto p do L.data[i]:=L.data[i+1]; L.Last:=L.Last-1; end; End; {------------ LAY GIA TRI PHAN TU TAI VI TRI P -----------} Function Retrieve(P: integer;L:List):integer; Var i:integer; Begin if empty(L) then writeln('Danh sach rong!') else if (P<1) and (p>L.Last) then writeln('P is out position!') else 3 retrieve:=L.data[p]; End; {----------------- THUC THI DANH SACH LIEN KET DON ----------- } Uses CRT; Type datatype=integer; List=^Cell; Cell= Record data:datatype; next:List; end; Var L: List; x,n:integer; {---------- TAO DANH SACH RONG ---------} Procedure Makenull(Var L: List); Begin L^.next:=nil; End; {----------- KIEM TRA DANH SACH RONG --------} Function Empty(L:List):Boolean; Begin empty:=L^.next=nil; End; {----------- TRA VE PHAN TU SAU PHAN TU P -----------} Function Next(P:List;L:List):List; Begin Next:=P^.next; End; {------------ TRA VE PHAN TU DAU DANH SACH --------------} Function First(L:List):List; Var P: List; Begin if not empty(L) then P:=L; End; {----------- TRA VE PHAN TU SAU PHAN TU CUOI DANH SACH ----------} Function End_List(L:List):List; Var P: List; Begin P:=L; While P^.next<>nil do P:=P^.next; End_List:=P; End; {------------ TRA VE VI TRI PHAN TU TIM THAY --------} Function Local(x:datatype;L:List):List; Var P: List; Begin if empty(L) then Local:=nil else begin 4 P:=L; while (P^.next<>nil) do begin if P^.next^.data=x then Local:=p; P:=P^.next; end; end; End; {------------ LAY GIA TRI TAI PHAN TU P -----------} Function Retrieve(P:List;L:list):datatype; Begin if P^.next<>nil then Retrieve:=P^.next^.data; End; {------ THEM PHAN TU VAO VI TRI BAT KI TRONG DANH SACH ---------} Procedure Insert_L(x:datatype;p:integer;Var L:List); Var tam,Q:List;i:integer; Begin new(tam);tam^.data:=x; tam^.next:=nil;Q:=L; if P=1 then begin tam^.next:= Q^.next; Q^.next:=tam end else begin For i:=2 to p do Q:=Q^.next; tam^.next:=Q^.next; Q^.next:=tam; end; End; {------------ THEM 2 PHAN TU VAO DANH SACH DA DUOC SAP XEP --------} Procedure Insert_L1(x:integer;Var L: List); Var tam:integer;P,Q:List; Begin new(Q); Q^.data:=x; Q^.next:=nil;P:=L; While (P^.next<>nil) and (x>P^.next^.data) do P:=P^.next; Q^.next:=P^.next; P^.next:=Q; End; {-------------- XOA PHAN TU TAI VI TRI n TRONG DANH SACH -----------} Procedure Delete(n:integer;Var L:List); Var i: integer; P,P1:List; Begin if n=1 then begin P:=L; 5 P^.next:=P^.next; end else begin P:=L; For i:=2 to n do P:=P^.next; P^.next:=P^.next; end; P^.next:=P^.next^.next; End; {------------ SAP XEP DANH SACH --------} Procedure SapXep(Var L: List); Var P,Q: List; tam:integer; Begin P:=L; while P^.next<>nil do begin Q:=P^.next; While Q<>nil do begin if P^.next^.data>Q^.next^.data then begin tam:=P^.next^.data; P^.next^.data:=Q^.next^.data; Q^.next^.data:=tam; end; Q:=Q^.next; end; P:=P^.next; end; End; II- HÀNG ĐỢI {---------------THUC THI HANG DOI BANG CON TRO ---------} Uses CRT; Type elementype=integer; Node=^Cell; Cell= record element:elementype; next:Node; end; Queue=record front,rear: Node; end; Var Q: Queue;x,m,n:integer; {--------- TAO HANG RONG -----------} Procedure Makenull(Var Q: Queue); Begin 6 new(Q.front); Q.front^.next:=nil;Q.front:=Q.rear; End; {-------- KIEM TRA HANG RONG--------} Function Empty(Q:Queue):boolean; Begin empty:=Q.front=Q.rear; End; {---------- LAY NOI DUNG TAI VI TRI DAU HANG --------} Function Front(Q:Queue):elementype; Begin if not empty(Q) then front:=Q.front^.next^.element; End; {--------- XOA PHAN TU DAU HANG ------------} Procedure DelQueue(Var Q: Queue); Var T: node; Begin if not empty(Q) then Q.front:=Q.front^.next; End; {---------- THEM PHAN TU VAO CUOI HANG -------- } Procedure EndQueue(x:elementype;Var Q: Queue); Begin new(Q.rear^.next); Q.rear:=Q.rear^.next; Q.rear^.element:=x; Q.rear^.next:=nil; End; {------------ THUC THI HANG DOI BANG MANG VONG ---------------} Uses CRT; Const max=100; Type elementype=integer; Queue = Record element:array[1 max-1] of elementype; front,rear:integer; end; Var Q: Queue;x,m,n:integer; {---------- TAO HANG RONG ----------} Procedure Makenull(Var Q: Queue); Begin Q.front:=0;Q.rear:=0; End; {--------- KIEM TRA HANG RONG -------} Function Empty(Q:Queue):Boolean; Begin empty:=Q.Front=Q.rear+1; End; {--------- KIEM TRA HANG DAY ---------} 7 Function Full_Q(Q:Queue):Boolean; Begin Full_Q:=Q.rear=max; End; {--------- THEM PHAN TU VAO CUOI HANG ----------} Procedure EndQueue(x:elementype;Var Q: Queue); Begin if Full_Q(Q) then writeln('Hang day!') else begin if Q.front=0 then Q.front:=Q.front+1; Q.rear:=Q.rear+1; Q.element[Q.rear]:=x; end; End; {--------- XOA PHAN TU RA KHOI HANG ---------} Procedure DelQueue(Var Q: Queue); Begin if empty(Q) then writeln('Hang doi rong!') else Q.front:= Q.front+1; End; III- NGĂN XẾP: {------------- THUC THI NGAN XEP BANG MANG -------------} Uses CRT; Const max = 100; Type Elementype=integer; STACK=Record element:array[1 max] of elementype; Top:integer; end; Var S:Stack;x,n,m:integer; {----------- TAO NGAN XEP RONG ----------} Procedure Makenull(Var S: Stack); Begin S.Top:=max; End; {--------- KIEM TRA NGAN XEP RONG --------} Function Empty(S:Stack):boolean; Begin empty:=S.Top=max; End; {---------- KIEM TRA NGAN XEP DAY ----------} Function Full_Stack(S:Stack):boolean; Begin Full_Stack:=S.Top=0; End; 8 {---------- THEM PHAN TU X LEN DINH NGAN XEP ----------} Procedure Push(x:elementype;Var S:Stack); Begin if full_Stack(S) then writeln('Ngan xep day!') else begin S.Top:=S.Top-1; S.element[S.top]:=x; end; End; {-------- TRA VE PHAN TU TREN DINH NGAN XEP -------} Function TOP(S:Stack):elementype; Begin if empty(S) then writeln('Ngan xep rong!') else Top:=S.element[S.top]; End; {---------- XOA PHAN TU O DINH NGAN XEP ---------} Procedure POP(Var S: stack); Begin if empty(S) then writeln('Ngan xep rong!') else S.Top:=S.Top+1; End; {------------- THUC THI NGAN XEP BANG CON TRO -------------} Uses CRT; Type elementype=integer; Stack=^Node; Node=Record element:elementype; Link:Stack; end; Var S: Stack; x,m,n:integer; {----------- TAO STACK RONG ----------} Procedure Makenull(Var S: Stack); Begin S:=nil; End; {---------- KIEM TRA RONG ---------} Function Empty(S:Stack):boolean; Begin empty:=S=nil; End; {--------- THEM PHAN TU ---------} Procedure Push(x:elementype;Var S:Stack); Var P: Stack; Begin 9 new(P);P^.element:=x; P^.link:=S;S:=P; End; {----------- XOA PHAN TU TAI DINH NGAN XEP --------} Procedure POP(Var S: Stack); Var P: Stack; Begin if empty(S) then writeln('ngan xep rong!') else begin P:=S^.link; dispose(S); S:=P; end; End; IV- CÂY TỔNG QUÁT: {------------- THUC THI CAY TQ BANG MANG -------------} Uses CRT; Const max=100; Type elementype= char; Node=integer; Tree= record parent:array[1 max] of node; labell:array[1 max] of elementype; max_node:node; end; Var T: Tree;i:node; {--------- TAO CAY RONG --------} Procedure Makenull(Var T:Tree); Begin T.max_node:=0; End; {-------- KIEM TRA CAY RONG -------} Function Empty(T:Tree):boolean; Begin empty:=T.max_node=0; End; {--------- TIM CHA CUA NUT N --------} Function Parent(n: node; T: Tree):node; Begin if empty(T) then writeln('Cay rong!') else parent:=T.parent[n]; End; {-------- XAC DINH NUT GOC CUA CAY ---------} Function ROOT(T:Tree):node; Begin 10 if empty(T) then writeln('Cay rong!') else ROOT:=1; End; {-------- NHAN CUA NUT N ---------} Function Label_T(n:node; T: Tree):elementype; Begin if empty(T) then writeln('Cay rong!') else Label_T:=T.labell[n]; End; {------- TIM CON TRAI CUA NUT N --------} Function Leftmost_child(n:node;T:Tree):node; Var i:node; Begin if empty(T) then Leftmost_child:=0 else begin i:=n+1; while (T.parent[i]<>n) and (i<=T.max_node) do i:=i+1; if T.parent[i]=n then Leftmost_child:=i else if i>T.max_node then Leftmost_child:=0; end; End; {---------- ANH EM BEN PHAI CUA NUT N ----------} Function Right_Sibling(n:node;T:Tree):node; Var i: node; Begin if empty(T) then Right_Sibling:=0 else begin i:=n+1; while (T.parent[i]<>T.parent[n]) and (i<=T.max_node) do i:=i+1; if T.parent[i]=T.parent[n] then Right_Sibling:=i else if i>T.max_node then Right_Sibling:=0; end; End; {----------- DUYET CAY THEO PA TIEN TU --------} Procedure PreOrder(n:node;T:Tree); Var i:node; Begin write(label_T(n,T)); i:=Leftmost_child(n,T); while i<>0 do begin n:=i; Preorder(n,T);

Ngày đăng: 25/04/2013, 20:35

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w