Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 132 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
132
Dung lượng
8,14 MB
Nội dung
llllllllllllllllilll GT.0000026859 \j DAI G IA O HOC CONG NGHlfiP HA NOl T R I N nh A x u At b A n kh o a h o c v A H KY THUAT TRUdNG £>AI HOC CONG NGHIEP HA NQI AN VAN MINH - TRAN HUNG CUCJNG G IA O T R I M ! A I NHA XUAT BAN KHOA HOC VA KY THUAT MUC LUC Trang HNOI DAU Chirong TONG QUAN VE CAU TRUC DU LIEU VA GIAI THUAT VAI TRO CUA VIEC XAY Dl/NG CAU TRUC DU' LIEU CAC TIEU CHUAN DANH G1A CAU TRUC DU' LIEU 12 CAC CAU TRUC DU' LIEU CO SCJ TRONG C/C++ 13 1.3.1 Djnh nghTa kieu dir lie u 14 1.3.2 Cac thuoc tinh cua mqt kieu dir lieu 14 1.3.3 Cac kieu du lieu cor b a n 14 1.3.4 Cac kieu dir lieu co cau true 15 1.3.5 Cac phep toan h? kieu C/C++ 19 g i Ai t h u At - p h An t ic h vA d An h g i A g i A i t h u At 19 1.4.1 Giai th u at 19 1.4.2 Bieu dien giai thuat 21 1.4.3 Phan tich giai th u at 21 1.4.4 Phan tich mpt so giai th u at 28 TLUAN CHUNG 32 ITAPCHU'ONG 32 Chining DE QUY VA GIAI THUAT DE QUY K.HA1 N1EM VEDEQUY 34 g i A i t h u At d e q u y v A h A m d e q u y 34 2.2.1 Giai thuat d? quy 34 2.2.2 Ham de quy 35 THIET K.E GlAl THUAT DE QUY 36 2.3.1 Ham n ! 36 2.3.2 Bai toan day s6 FIBONACCI 37 2.3.3 Bai toan “Thap HaNoi” 38 HIEU LQ'C CUA DE Q U Y 40 ITAPCHU'ONG 42 Chiro'ng DANH SACH TUYEN TINH 3.1 KHAlNIEM DANH SACH TUYEN TINH 44 3.1.1 Khai niem 44 3.1.2 Cac phep toan tren danh sach 44 3.2 LUlI TRU' KE TIEP CUA DANH SACH TUYEN TINH 46 3.2.1 Thiet ke cau true dir lieu 46 3.2.2 Cai dat cac phep toan tren danh sach 48 3.2.3 Bai tap ap dpng 54 3.3 DANH SACH MOC NOI 61 3.3.1 KiSu tro va cac khai niem lien quan 61 3.3.2 Danh sach moc noi don 66 3.4 DANH SACH NOI VONG 89 3.5 DANH SACH MOC NOI HAI CHIEU 90 3.5.1 Phep bo sung mQt nut m o i 92 3.5.2 Loai bo mpt nut tren danh sach 93 3.6 UNO DUNG DANH SACH MOC NOI 94 3.6.1 Giai thieu umg dung 94 3.6.2 Giai thuat .95 3.7 STACK VA QUEUE 97 3.7.1 Stack (Ngan x ep ) 97 3.7.2 Queue (Hang d p i) 114 BAI TAP CHUONG 125 Chirffng CAY 4.1 CAY VACAC KHAI NIEM CO BAN 131 4.1.1 Djnh nghTa 131 4.1.2 MQt so khai nipm ca ban 132 4.2 CAYNH1 PHAN 133 4.2.1 Djnh nghTa 133 4.2.2 Tinh chat 133 4.2.3 Bieu dien cay nhi phan 134 4.2.4 Phep duyet cay nhi phan 138 4.2.5 Cay nhj phan bi6u diin bieu thurc 140 CAY NHI PHAN l i\1 KIEM 142 4.3.1 Djnh nghTa 142 4.3.2 Cai dat cay nhj phan tim kiem 143 4.3.3 Cac thao tac ca ban tren cay nhj phan tim kiem 144 4.3.4 Thai gian thirc hien cac phep toan tren cay nhi phan tim kiem 157 CAY CAN BANG (AVL T R EE) 158 4.4.1 Cay can bang hoan toan (CCBHT) 159 4.4.2 Cay can b a n g - 159 Al TAP CHU'ONG 166 Chirffng SAP XEP VA TiM KIEM I c A c PHU’ONG PHAP SAP X E P 169 5.1.1 Khai niem sap x e p 169 5.1.2 Ba plurang phap sap xep ca ban 170 5.1.3 Phircmg phap phan doan 184 5.1.4 Phutmg phap vun dong 191 5.1.5 Phirong phap trpn 201 5.1.6 Bai tap ap dung 211 ET LUAN CHUNG 216 TiM KIEM 217 5.2.1 Bai toan tim kiem 217 5.2.2 Tim kiem tuan t p 217 5.2.3 Tim kiem nhj phan 221 5.2.4 Bai tap ap dpng 225 Al TAP CHU'ONG 229 Al I.If.UTHAM KHAO .231 LO I NOI DAU A lgdy cong nghe thong tin duqc ung dung rong rai moi ITnh V vice cua ddi song xa hoi Viec xdy dung cac he thong phan mem ung dung de gidi quyet yeu cdu thay the cho nguoi trd nen bien hon bao gia het Tuy nhien, day luon la mot viec het sue kho khan moi giai doqn phat trien, co mot giai doan het sue quan la thiet ke cdu true dir lieu he thong va cac gidi thuat gidi quyet cac yeu cdu Cuon gido trinh ‘Cau true dir lieu va giai tliuat " dai phdn nao giup sinh vien, nhieng nha phat trien phdn mem tuomg lai co duqc nhung kien thicc co ban ban ddu cho van de lira chon, xay dung cdu true dir lieu cung nhu cac gidi thuat Gido trinh la tai lieu hoc tap cua mot mon hoc co so cung ten Chuong trinh ddo tao ky su cong nghe thong tin Noi dung gido trinh trinh bay nhieng kien thirc co ban ve cdu true dir lieu va ede gidi thuat xir ly lien quan, giup sinh vien nhqn thirc duqc van de thiet ke vd lua chon cdu true dir lieu vd cac gidi thuat, mot giai doan quan trong quy trinh phdt trien phdn mem De hoc tot mon hoc nay, doi hoi sinh vien phai thao It nhat mot ngon ngir lap trinh co ban nhu Pascal, C/C++ thao cac ky thuat lap trinh nhu: cdu true re nhanh, cdu true lap, ky thuat lap trinh don the (sic dung ham) NOt dung gido trinh duqc chla lam chuong: • Chuong Tong quan ve cdu true die lieu vd gidi thuat, bao gom cac khdi niem ve cdu true dir lieu vd gidi thuat, moi quan he giua chung, van de thiet ke cdu true dir lieu, thiet ke vd phdn tich gidi thuat, danh gia phirc tap cua gidi thuat • Chuong De quy vd gidi thuat de quy, mot phuong phap thiet ke gidi thuat khd quan trong, nhat la vdi cac gidi thuat bieu dien cac thao tac xtir iy cdu true die lieu dang cay • Chuffng Danh sach tuyen tinh, mot lo'ai cdu true dir lieu rat bien cac bai loan tin hoc Trong chuong chung toi trinh bay cac phuong phap luu trie danh sdeh vd cac thao tac xic ly tuemg ung vdi moi loai danh sdeh • Chuffng Cay, mot dang cdu true die lieu phi tuyen tinh, chuong chu yeu noi ve cay nhi phdn vd cac ung dung cua chung • Chuffng Sap xep vd tim kiem, tap trung vao van de mo ta, thiet ke vd danh gia cac gidi thudt sap xep vd tim kiem thong dung, cung nhu van de cai dat cac gidi thudt bai toan ung dung Cac chuong trinh ung dung vd bai tap moi chuong da duoc chon loc d mice phu hop ddi vdi sinh vien, qua sinh vien hieu sau sac them ve bai giang, cimg cd them ve ky thudt chi dat chuong trinh vd nam bat duoc mot so kien thirc khdng duoc true tiep gioi thieu gido trinh Trong qua trinh bien soan gido trinh nay, chung toi da nhan duoc rat nhieu y kien dong gop ve noi dung tic phia ede dong nghiep Chung toi xin chan cam on Mac dii da cd gang rat nhieu bien soan, nhung cung khdng the tranh khoi nhung thieu sot Chung toi mong muon nhan duoc nhung y kien dong gop, chinh sica de noi dung cua gido trinh duoc hoan thien hon nhung Ian tai ban sau Moi y kien dong gop xin giri ve Khoa Cong nghe thong tin - Truong Dai hoc Cong nghiep Ha Noi, hoac giri vao hop thu dien tic: anvanminh 78@yahoo com NHOM TAC GIA A n Van M inli - Tran Ilu n g Cirung K iem tra h a n g d g i d a y int Full(Queue Q) return (Q.rear == Max-1); } Ma x B 10 E fro n t “ rear* H in h King dg'i d^y B o s u n g m ol p h d n lit m a i vdo cu o i h a n g d g i Khi bo sung mpt phan tir mai (vai thong tin lim bien X) vao hang dgri cin kiem tra xem hang c6 day khong, neu h ^ g chua day thi bo sung phan tir mai vao h^g, ngupc lai vipc bo sung khong duac thuc hien Hinh 3.44 mo ta viec bo sung mpt phan tCr vao cuoi h ^ g dpi Ma x > 10 4 ao a i Thao teic bo sung Ma x = 10 fro n t ” »2 a j 34 as as rear«= HSng dpi mPi H in h 4 116 Bo sung mpt phan tu vho cuoi hhng dpi int AddQ(Queue &Q, Item X) { if (Full(Q)) return 0; else { Q.rear = Q.rear + 1; Q.E[Q.rear] = X; return 1; } } Ham AddQ thirc hien bo sung phan lir mai vao cuoi hang dgi, ham tra ve neu bo sung cong, va tra ve neu ngugc lai L o g i ho m o t p h d n lu k h o i h a n g d g i Khi loai bo mot phan tir can phai kiem tra xem hang dgi c6 rong khong, neu hang dgi rong thi khong the thuc hien viec loai bo, ngugc lai thi loai bo phan tu a dau hang, ngi dung cua phan tu dugc luu bien X Them nua, loai bo, neu hang chi c6 mot phan tu (nghia la hang se rong sau loai bo) thi can khoi tao lai hang dgi (hinh 3.45 va 3.46) Ma x = 10 E ao 3l 32 33 34 3s X ! rear = fro nt = J Thao tec loai bo Max = 10 front = rear = Danh sSch mbi H h ih Lay mot phan tti' khoi hang dpi c6 nhieu hnn I phan t>r 117 front = Thao tac loai bo 10= Max front = rear = -1 Danh sach r5ng sau lo^i bo H in h Lay nigt phan tii' khoi hing dpi cd I phan tii' Dudi day la ham thirc hien vi^c loai bo int DeleteQ{Queue &Q, Item &X) { if (Empty(Q)) return 0; else X = Q.E[Q.front]; if (Q.front == Q.rear) { Q.front = 0; Q.rear = -l;//khdi tao lai hang dgi } else Q.front = Q.front + 1; return 1; } } 118 Ham DeleteQ thirc hien lay mot phan tir a dau hang doi, ham tra ve neu phep lay cong, ngugc lai tra ve Nhdn xel: Phucmg phap cai dat hang dgi bai mang voi hai chi so nhu tren c6 nhugc diem Ian Neu phep loai bo khong thudmg xuyen lam cho hang rong, thi cac chi so front va rear se tang lien tuc va se vugt qua c5 ciia mang Hang se trd day, mac du cac vi tri trong mang c6 the van nhieu (do viec loai bo cac phan tir d dau hang) De tranh tinh trang nay, moi hang dgi ddy ta lai kiSm tra khong gian nhd phia trudc hang dgi, neu thi day hang dgi ve phia trudc de tao khdng gian nhd trdng d phia sau Tuy nhien viec tieu ton rat nhieu thdi gian .7.2.J Cai dat hang dpi boi mang vong iron De khac phiic nhugc diem neu tren ngudi ta dua phuomg phap cai dat hang dgi bdi mang vdng trdn Dd la mot mang vdi chi so chay mien l max, vdi mgi i = 1,2, , max - 1, phan tir thu i ciia mang di trudc phan tir thu i + 1, phan tir thu max di trudc phan tu dau tien, tuc la cac phan tu ciia mang dugc xep vdng trdn (hinh 3.47) H'lnlt 3.47 Hang dpi v6ng Iron Khi bieu dien hang dgi bdi mang vdng trdn, de biet nao hang dgi day, nao hang dgi rdng ta can dua them vao bien c o u n t de dem so phan tir hang Chung ta cd khai bao cau true du lieu sau: 119 tdefine Max N //Khai bao kieu du li?u Item (neu can) structQueue { int count; int front, rear; Item E[Max]; }; Queue Q; Vai each cai dat nay, hang rong Q.count = 0, hang day □.count = Max Khi lam viec voi mang vong tron, can liru y rang, phan tir dau tien ciia mang di sau phan tir thur max Sau day chiing ta se cai dat thao tac bo sung mot phan tir vao hang dai va loai bo mot phan tir khoi h ^ g dgi, cac thao tac khac danh cho b ^ doc Ham thuc hien thao tac bo sung mot phan tir vao hang dgi int AddQ(Queue &Q, Item X) { if (Q.count == Max) return 0; else { if (Q.rear == Max-1) Q.rear = 0; else { Q.rear = Q.rear + 1; Q.ElQ.rear] = X; Q.count = Q.count + 1; } return 1; } } Ham thuc hien thao tac loai bo mot phan tir khoi hang dgi 120 int DeleteQ(Queue &Q, Item &X) { if (Q.count == 0) return 0; else ( X = Q.E[Q.front]; if (Q.front == Q.rear) ( Q f r o n t = 0; Q.rear = -1; } else if(Q.front == Max-1) Q.front = 0; else { Q.front = Q.front + 1; Q.count = Q.count - 1; } return 1; } } Hang dgi thuong dung de thirc hien cac “tuyen cha” (waiting lines) xu ly dong, dac biet cac he mo phong (simulation), la cac he mo hinh hoa cac qua trinh dong va nguai ta dung mo hinh de nghien cuu boat dong ciia cac qua trinh ay 7.2.4 Cai dat hang dgi boi tro Nhu ta da biet, doi vai ngan xep viec truy nhap chi dugc thirc hien mot dau (dinh) Vi vay, viec cai dat ngan xep bang tro kha thuan Igi ChSng han, vdfi ngan xep dugc cai dat bai tro thi S la tro tro tai dinh cua ngan xep Bo sung mot nut vao ngan xep chinh la viec bo sung mot niit vao niit dau tien ciia danh sach, loai bo mot nut khoi ngan xep chinh la loai bo nut dau tien cua danh sach dang tro bai S Viec bo sung vdi ngan xep dang khong can kiem tra hien tugng tran nhu v6i ngan xep luu tru ke tiep 121 Doi v6i hang doi thao tac loai bo dugc thuc hien d mot dau, thao tac bo sung dugc thuc hien a dau Neu coi danh sach moc noi dom nhu mot hang dgi thi viec loai bo mot nut cung giong nhu vdi ngan xep, nhung bo sung mot nut thi phai gan nut mdi vao cuoi hang dgi, nghTa la phai tim den nut cuoi cung Trong trudng hgp nay, de luu tru hang dgi nguoi ta diing hai tro, mot tro tro vao nut dau hang dgi va mot tro tro vao niit cuoi hang dgi Cau true du li?u bieu dien hang dgi nhu sau: HKhai bdo kieu du li^u Item (neu can) struct Node Item Infer; Node *Next; }; struct Queue { Node *front; Node *rear; }; Queue Q; Trong each bieu dien tren, front la tro tro den nut dau hang dgi, rear la tro tro den nut cuoi hang dgi Hlnh 3.48 Danh sich m6c noi bieu dien hhng dgi Voi each cai dat nay, hang dgi dugc xem la khong nao day Hang dgi rong Q.front = NULL Sau day la cac ham thuc hien cac phep toan tren hang dgi: 122 a Khai Igo hang dgi rang: void Create(Queue &Q) { Q.front = NULL; Q.rear = NULL; h Kiem Ira hang dgi rang: int Empty(Queue Q) { return (Q.rear == NULL); } c Bo sung mot phdn tic veto cuoi hang dgi: void ADD(Queue &Q, Item X) ( Node *P; P = new Node ; P->Infor = X; P->Next = NULL; if (Empty(Q)) { Q.front = P; Q.rear = P; } else { Q.rear->Next = P; Q.rear = P; } Hiiili 3.49 Minh hoa thao tac bo sung 123 d L a y m ol p h d n lie a d d u h a n g d a i int Del(Queue &Q, Item &X) { Node *M; if (Empty(Q)) return 0; else { M = Q.front; X = Q.front->Infor; // truong hpp hang dgi c6 mpt nut if (Q.front == Q.rear) Q.front = Q.rear = NULL; //Hang dpi nhieu hon mpt nut else Q.front = Q.front->Next; delete M; return 1; } } HinhJ.SO Minh hoa thao t ic liy 11191 phan tir Tren day chiing ta da xem xet mot loai cau true du lieu, dugc sir dung rat bien cac img dung, la danh sach tuyen tinh vai cac dang khac dugc cai ddt theo hai each: bang mang (luru tru ke tiep) va bang tro (luu tru moc noi), ciing voi cac phep toan xir ly tuemg img tren moi loai Ban dgc cung dugc tim hieu hai each thuc luu tru dac bigt la danh sach kieu ngdn xep va kieu h ^ g dgi Moi loai danh sach va each cai dat chiing CO nhung uu va nhugc diem khac Tuy nhien, de hieu ro hom ve no, ta can cai dat mot so ling dung nho tren may doi voi moi loai Khi cai dat hay lua chon cau true luu tru phii hgp vai du lieu ciia bai toan 124 BAI TAP CHl/aNG B a i I : Cho danh sach noi don voi nut dau tien dugc tro bai tro L nhu hinh ve: Yeu cdu: a Ve hinh mo ta tr^g thai ciia danh sach qua trinh tao mai danh sach gom phan tut nhu tren, xuat phat tu mot danh sach rong (yeu cau mo ta tirng buac qua trinh) b Ve hinh mo thao tac loai bo phan tit dau tien (aO) va phan tu thu (a2) danh sach (can chu thich ro rang) c Ve hinh mo ta thao tac bo sung phan tu ‘a4 ’ vao dau danh sach, va vao sau phan tu thii (a2 ) danh sach d Gia sir 32 la nho nhat, ve hinh mo ta tinh trang cua danh sach thao tac sap xep tdng dan bang phuong phap lira chpn a Ian duyet dau tien (chi ve trang thai luc bat dau duyet, luc chuan bj doi cho va sau doi cho) e Gia sir danh sach luu tru thong tin ve cac sinh vien, moi sinh vien gom: Ma sinh vien, ho va ten, nam sinh, diem tong ket Cai dat chuomg trinh thuc hien cac yeu cau sau: • Khai bao cau true du lieu cua danh sach • Tao mai danh sach gom 10 phan tir (cac thong tin dugc nhap tir ban phim) Hoac tao mai danh sach vai cac thong tin phan tu nhap tir ban phim, viec nhap ket thuc ten cua sinh vien nhap vao la xau rong 125 • Hien thi danh sach len man hinh • Xoa phan tu dau tien danh sach, hien thi lai danli sach • Xac dinh ty le sinh vien gioi (dtk>=8), kha (dtk>=6.5), trungbinh (dtk>=5), yeu (con lai) • Xoa phan tit thu danh sach, hien thj lai danh sach • Them mot phan tir vao dau danh sach, hien thi lai danh sach • Them mot phan tCr vao sau phan tit thu k danh sach (knhap tit ban phim), hien thi lai danh sach • Nhap vao ten cua mpt sinh vien, hien thi thong tin day du cua cac sinh vien COten vita nhap (thong bao truong hgrp khong c6 sinh vien nao) • Sap xep danh sach theo chieu tang dan cua ten sinh vien, hien thj lai danh sach • Them thong tin mot hoc sinh mai vao danh sach cho trat tu vira s3p khong bj thay doi B a i 2: Thuc hien lai cac yeu cau ciia bai v6i danh sach noi doi B a i 3: Cai dat chuong trinh thuc hien cac yeu cau: 126- • Tao mot danh sach noi dom/noi doi luu tru cac so nguyen, du lieu duoc nhap tit ban phim, viec nhap ket thiic so nguyen nhap vao la -1 (luu y -1 khong phai la mot phan tir cua danh sach) hoac cho phep nhap n so voi n nhap tir ban phim • Hien thj danh sach len man hinh • Bo sung so nguyen X vao vj tri K danh sach (X, K nhap tir ban phim), hien thj lai danh sach • Xoa so thu k danh sach (k nhap tu ban phim), hien thj lai danh sach • Xoa cac so am danh sach, hien thj lai danh sach • Sap xep danh sach theo chieu tSng d^giam dan, hien thi lai danh sach • Nhap mot so nguyen, bo sung no vao danh sach cho danh sach van COthu tu, hien thj lai danh sach • Cho biet chieu dai cua danh sach, so luong so am, so luong so duong Bai 4: Cai dat chuang Irinh thirc hien cac yeu cau sau: • Tao mot danh sach noi dcm/noi doi luu tru cac phan so c6 tii so va mau so la cac so nguyen khac khong, dO: lieu dupe nhap tir ban phi'm, viec nhap ket thiic neu gap phan so ma tii so hoSc mau so ciia no dupe nhap la so khong • Hien thi danh sach len man hinh (vi du: 3/4, -2/5, 1/2, ) • Cho biet danh sach tren c6 bao nhieu phan so chua dupe toi gian, hay toi gian cac phan so do, hien thi lai danh sach • Bo sung mot phan so moi vao vj tri thu k danh sach (phan so mai va k dupe nhap tir ban phim.), hien thi lai danh sach • Tinh tong cac phan so c6 ciing mau so la danh sach, hien thj ket qua sau da toi gian • X6a cac phan so c6 tu so la so am, hien thi lai danh saeh, B a i 5: Danh sach L dupe luu tru ke tiep bieu dien bai hinh ve (E: mang luu cac phan tir ciia danh sach, count bien nguyen luu dp dai thuc cua danh sach count = E ao 3| ^2 23 34 Yen cciu: a Ve hinh mo ta trang thai cua danh sach qua cac bude qua trinh tao mai danh sach tU mot danh shch rong b Ve hinh mo ta trang thai ciia danh sach thao tac loai bo phan tir dau tien (ao) va phan tir thii (a2 ) danh sach (can chit thich ro rang) c Ve hinh mo ta qua trinh bo sung phan tir as vao dau danh sach, vao sau phan tu thu (a2 ) danh sach d Gia sir danh sach luu tru thong tin ve cac sinh vien, moi sinh vien gom: Ma sinh vien, hp va ten, nam sinh, hp khau (tinh), diem tong ket Hay cai dat chuang trinh thuc hien cac yeu cau sau: 127 • Khai bao cau true du lieu cua danh sach • Nhap mod 10 phan tur cho danh sach, hien thi danh sach len man hinh • Xoa phan tu dau tien danh sach, hien thj lai danh sach • Xoa phan tir thur danh sach, hien thj lai danh sach • Them mot phan tir vao dau danh sach, hien thi lai danh sach • Them mot phan tir vao sau phan tir thir k danh sach (k nhap tii phim), hien thj danh sach • Nhap hp dem cua mot sinh vien, hien thi thong tin day dii cua nhung sinh vien c6 hp dem vira nhap (thong bao truong hop khong c6 sinh vien nao) • sap xep danh sach theo chieu tang dan cua hp khau, hien thj lai danh sach B a i 6: Cai dat chuong trinh thurc hien cac yeu cau; • Tao mot danh sach lixu tru ke tiep cac so nguyen, du lieu dupe nhap tir ban phim, vipc nhap ket thuc so nguyen nhap vao la -1 • Hien thj danh sach len man hinh • Bo sung so nguyen X vao vj tri K danh sach (X, K nhap tir ban phim), hien thj lai danh sach • Xoa so thu I danh sach (I nhap tir ban phim), hien thj lai danh sach • Xoa cac so am danh sach, hien thi lai danh sach • sap xep danh sach theo chieu tang dan/giam dan, hien thi lai danh sach • Nhap mot so nguyen, bo sung no vao danh sach cho danh sach van COthu tu, hien thi lai danh sach B a i 7: Cai dat chuomg trinh thuc hien cac yeu cau sau: • 128 Tao mot danh sach luu tru ke tiep cac phan so c6 tu so va mau so la cac so nguyen khac khong, du lipu dupe nhap tii ban phim, vipc nhap ket thuc neu gap phan so ma tir so hoac mau so cua no dupe nhap la so khong • Flien thi danh sach len man hinh (vi du: 3/4, -2/5, 1/2, ) • Cho biet danh sach tren c6 bao nhieu phan so chua duoc toi gian, hay toi gian cac phan so do, hien thj lai danh sach • Bo sung mot phan so mcri vao vj tri thii k danh sach (phan so moi va k duac nhap tu ban phim), hien thj lai danh sach • Tinh tong cac phan so c6 ciing mau so la danh sach, hien thj ket qua sau da toi gian • Xoa cac phan so c6 tir so la so am, hien thj lai danh sach B u i 8: Cho ngan xep S dugc bieu dien boi hinh ve top=4 aj ^3 32 3| ao NgSn xep S • Ve hinh mo ta trang thai cua ngSn xep qua cac buoc, thirc hi$n thao tac loai bo phan tir ngSn xep • Ve hinh mo ta trang thai ciia ngan xep qua cac buac thirc hien thao tac bo sung phan tir a; vao day ngan xep • Giu sir ngan x6p duoc luu irtl bOi mang/con irO vdi m6i ph4n lir la m^l so nguyen Yeu can: - Cai dat cau true du lieu cua ngan xep ciing voi cac phep loan ca so cua no - Cai dat phep loan loai bo phan tCrthii ngan xep ti'nh tir day - Cai dat phep loan bo sung phantir vao day ngan xep - Cai dat phep loan loai bo phan tit thit k ngan xep -G JA ITH U A T 129 Bai 9: Cho hang doi Q dugc bieu dien bai hinh ve sau: • Ve hinh mo ta tr^g thai cua h ^ g dgi thuc hien thao tac bo sung phdn tu as vao dau hkig dgi va bo sung phan tii a6 vao sau ph^ tu a2 • Ve hinh mo ta trang thai ciia h ^ g dgi thuc hien thao tac loai bo phan tu a2 va phan tu a4 hmg dgi • Gia su h ^ g dgi dugc luu tru bdi mang/con tro, moi phan tu la mot so nguyen Yeu cdu: - Cai dat cau true du lieu cua hang dgi cung voi cac phep toan ca sa cua no - Cai dat phep toan bo sung mgt phan tir vao dau hang dgi - Cai dat phep toan bo sung mgt phan tu vao sau phan tir thu tinh tir dau hang dgi - Cai dat phep toan loai bo phan tir thu k hang dgi 130