0% found this document useful (0 votes)
19 views3 pages

Danhsachke

Uploaded by

tien nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Danhsachke

Uploaded by

tien nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;
const int MAXV = 20;
struct Node {
int dinhKe;
int trongSo;
Node* link;
};
struct DanhSachKe {
Node* ds[MAXV];
int n;
};

bool docFile(const char* filename, DanhSachKe& dske);


void xuat(DanhSachKe dske);
int tinhBacVao(DanhSachKe dske, int dinh);
int tinhBacRa(DanhSachKe dske, int dinh);

struct Canh {
int dinhdau;
int dinhcuoi;
int trongso;
};
struct Danhsachcanh {
int n;
Canh ds[MAXV];
};
Danhsachcanh timtapcanh(DanhSachKe dske);
void intapcanh(Danhsachcanh c);
void in1Canh(Canh c);

#include"Header.h"
bool docFile(const char* filename, DanhSachKe& dske) {
FILE* f = fopen(filename, "rt");
if (f == NULL)
return 0;
fscanf(f, "%d", &dske.n);
for (int i = 1; i <= dske.n; i++)
dske.ds[i] = NULL;
for (int i = 1; i <= dske.n; i++)
{
int m;
fscanf(f, "%d", &m);
for (int j = 1; j <= m; j++)
{
Node* p = new Node;
fscanf(f, "%d%d", &p->dinhKe, &p->trongSo);
p->link = dske.ds[i];
dske.ds[i] = p;
}
}
fclose(f);
return 1;
}

void xuat(DanhSachKe dske)


{
cout << endl;
for (int i = 1; i <= dske.n; i++) {
cout << endl;
Node* p = dske.ds[i];
cout << i << ": ";
while (p != NULL) {
cout << " (" << p->dinhKe << "," << p->trongSo << ")";
p = p->link;
}
}
}
int tinhBacVao(DanhSachKe dske, int dinh)
{
int dem = 0;
for (int i = 1; i <= dske.n; i++) {
if (i == dinh) continue;
Node* p = dske.ds[i];
while (p != NULL) {
if (p->dinhKe == dinh)
dem++;
p = p->link;
}
}
return dem;
}
int tinhBacRa(DanhSachKe dske, int dinh) {
int kq = 0;
Node* p = dske.ds[dinh];
while (p != NULL) {
kq++;
p = p->link;
}
return kq;
}

Danhsachcanh timtapcanh(DanhSachKe dske)


{
Danhsachcanh dsc;
dsc.n = 0;
for (int i = 1; i <= dske.n; i++) {
Node* p = dske.ds[i];
while (p != NULL) {
Canh c = { i, p->dinhKe, p->trongSo };
dsc.ds[dsc.n] = c;
dsc.n++;
p = p->link;
}
}
return dsc;
}
void intapcanh(Danhsachcanh c) {
for (int i = 0; i < c.n; i++) {
cout << " (" <<c.ds[i].dinhdau<< ", " << c.ds[i].dinhcuoi << ", " << c.ds[i].trongso << ")";
}
}
void in1Canh(Canh c) {
cout << "(" << char(c.dinhdau-1 + 65) << ", " << char(c.dinhcuoi + 65) << ", " << c.trongso << ")";
}
#include"Header.h"
void main() {
DanhSachKe a;
docFile("dothi.txt", a);
xuat(a);

int dinh;
cout << "\nNhap so dinh de tinh bac vao: ";
cin >> dinh;
cout << "So bac vao cua dinh " << char(dinh + 65) << " la: " << tinhBacVao(a, dinh);

cout << "\nNhap so dinh de tinh bac ra: ";


cin >> dinh;
cout << "Bac ra cua dinh " << char(dinh + 65) << " la: " << tinhBacRa(a, dinh);

Danhsachcanh dsc;
dsc = timtapcanh(a);
cout << "\nTap canh cua do thi la:";
intapcanh(dsc);
int stt;
cout << "\nNhap thu tu tap canh muon xuat: ";
cin >> stt;
cout << "Tap canh thu " << stt << " la: ";
in1Canh(dsc.ds[stt]);
}

You might also like