Danh sách liên kết
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "iostream"
using namespace std;
//Khai bao kieu du lieu
typedef struct SINHVIEN
{
char MSSV[10];
char HoTen[80];
float DiemTin;
float DiemToan;
float DiemAV;
}SINHVIEN;
//Cau truc mot phan tu trong danh sach gom du lieu va con tro de lien ket
typedef struct NODE
{
SINHVIEN Data;
struct NODE *pNext;
}NODE;
//Cau truc danh sach gom 2 con tro tro den dau danh sach va cuoi danh sach
typedef struct LIST
{
NODE *pHead;
NODE *pTail;
}LIST;
//Prototype cua cac ham xu ly cua struct SINHVIEN
void NhapSV(SINHVIEN &); //Xong//
void XuatSV(SINHVIEN); //Xong//
void Copy(char* , char* ); //Xong//
void EpChuoiVaoSV(char* ,LIST &,NODE*); //Xong//
SINHVIEN Gan(SINHVIEN); //Xong//
bool Equal(SINHVIEN, SINHVIEN ); //Xong//
float TinhDTB(SINHVIEN ); //Xong//
int DocFile(char *TenFile, LIST &); //Xong//
int GhiFile(char *TenFile, LIST);
//Prototype cua cac ham xu ly cua struct SINHVIEN
//Prototype cua cac ham xu ly cua danh sach
void KhoiTaoDS(LIST &); //Xong//
void AddHead(LIST &,NODE *); //Xong//
int RemoveHead(LIST &); //Xong//
int RemoveLast(LIST &); //Xong//
int RemoveNode(LIST &,SINHVIEN ); //Xong//
void AddLast(LIST &,NODE *); //Xong//
void AddAfter(NODE *, NODE *, SINHVIEN); //Xong//
void NhapDS(LIST &); //Xong//
void XuatDS(LIST ); //Xong//
void ResList(LIST &); //Xong//
void PrintList(LIST ); //Xong//
void TimSinhVien(LIST ); //Xong//
NODE* TimNode(LIST ,SINHVIEN ); //Xong//
NODE* TaoNode(SINHVIEN ); //Xong//
//Prototype cua cac ham xu ly cua danh sach
//Ham chinh
void main(int argc, char *argv[])
{
LIST List;
KhoiTaoDS(List);
cout<<"\t\tBai tap danh sach lien ket";
x: cout<<"
1.Lay danh sach tu file."
<<"
2.Nhap danh sach tu ban phim."
<<"
3.Xuat danh sach."
<<"
4.Tim sinh vien."
<<"
5.In danh sach nhung sinh vien co DTB >=5."
<<"
6.Dao nguoc danh sach."
<<"
7.Xoa mot sinh vien."
<<"
8.Xoa sinh vien o dau danh sach."
<<"
9.Xoa sinh vien o cuoi danh sach."
<<"
10.Thoat.";
cout<<"
Tuy chon: ";
cin>>n;
switch(n)
{
case 1:
DocFile("input.txt",List);
cout<<"Da load xong!
";
XuatDS(List);
goto x;
case 2:
NhapDS(List);
goto x;
case 3:
XuatDS(List);
goto x;
case 4:
TimSinhVien(List);
goto x;
case 5:
PrintList(List);
goto x;
case 6:
ResList(List);
cout<<"Dao xong!
";
XuatDS(List);
goto x;
case 7:
SINHVIEN SV;
cout<<"Nhap ten va MSSV can xoa.
";
cout<<"MSSV:";
cin>>SV.MSSV;
cout<<"Ho Ten:";
cin>>SV.HoTen;
if(RemoveNode(List,SV) == 1)
{
cout<<"Da xoa!
";
XuatDS(List);
}
else
{
cout<<"Khong co sinh vien "
<<SV.HoTen<<" co MSSV "
<<SV.MSSV<<" trong danh sach.
";
}
goto x;
case 8:
RemoveHead(List);
cout<<"Da xoa!
";
XuatDS(List);
goto x;
case 9:
RemoveLast(List);
cout<<"Da xoa!
";
XuatDS(List);
goto x;
case 10:
cout<<"Co muon thoat ko?? y/n";
char c;
do
{
cin>>c;
if(c == 'y')
exit(1);
else if(c == 'n')
goto x;
}while((c != 'y') || (c != 'n'));
}
}
//Ham chinh
//Dinh nghia cac ham xu ly cua struct SINHVIEN
SINHVIEN Gan(SINHVIEN SV) //Ham gan cua struct SINHVIEN tuong duong SINHVIEN1 = SINHVIEN2
{
SINHVIEN Temp;
strcpy(Temp.MSSV, SV.MSSV);
strcpy(Temp.HoTen,SV.HoTen);
Temp.DiemTin = SV.DiemTin;
Temp.DiemToan = SV.DiemToan;
Temp.DiemAV = SV.DiemAV;
return Temp;
}
//So sanh xem MSSV va Ten cua SV1 va SV2 co bang nhau ko
bool Equal(SINHVIEN SV1,SINHVIEN SV2) //Ham tim MSSV va Ho Ten cua 1 sinh
{
if(strcmp(SV1.MSSV,SV2.MSSV)==0
&& strcmp(SV1.HoTen,SV2.HoTen)==0)
{
return true;
}
return false;
}
void NhapSV(SINHVIEN &SV) //Nhap mot SINH VIEN
{
fflush(stdin);
cout<<"
MSSV:";
gets(SV.MSSV);
cout<<"Ho Ten:";
gets(SV.HoTen);
cout<<"Diem Tin:";
cin>>SV.DiemTin;
cout<<"Diem Toan:";
cin>>SV.DiemToan;
cout<<"Diem Anh Van:";
cin>>SV.DiemAV;
}
void XuatSV(SINHVIEN SV) //Xuat mot SINH VIEN
{
cout<<"
MSSV:"<<SV.MSSV<<" Ho Ten:"<<SV.HoTen
<<" Diem Tin:"<<SV.DiemTin
<<" Diem Toan:"<<SV.DiemToan
<<" Diem Anh Van:"<<SV.DiemAV<<"
";
}
float TinhDTB(SINHVIEN SV)
{
float DTB = 0;
DTB = SV.DiemTin + SV.DiemToan + SV.DiemAV;
return DTB/3;
}
int DocFile(char *TenFile,LIST &List)
{
FILE *f = fopen(TenFile,"rt");
if(f == NULL)
{
return 0;
}
char* ThongTin;
ThongTin = new char[100];
rewind(f);
SINHVIEN *Data;
//KhoiTaoDS(List);
fscanf(f,"%d
",&n);
NODE* pNewNode = new NODE[n];
for( int i = 0; i < n; i++)
{
fgets(ThongTin,100,f);
EpChuoiVaoSV(ThongTin,List,pNewNode);
pNewNode = new NODE[n];
}
fclose(f);
return 1;
}
//Ham chia mot chuoi khi doc tu file thanh nhieu chuoi nho de dua vao struct SINH VIEN
void EpChuoiVaoSV(char* ThongTin,LIST &l,NODE* pNode)
{
if(pNode == NULL)
{
return;
}
int i = 0; //Bien giu vi tri cua ky tu trong chuoi Thong Tin
int j = 0; //Bien giu vi tri cua ky tu trong chuoi DanhSachThongTinh[index]
int k = 0; //Bien giu vi tri cua nhung chuoi chua thong tin trong mang DanhSachThongTin
char DanhSachThongTin[5][80];
while(ThongTin[i] != NULL)
{
if(ThongTin[i] == ' ' && ThongTin[i+1] != ' ')
{
j = 0;
k++;
}
else if(ThongTin[i] != ' ')
{
DanhSachThongTin[k][j] = ThongTin[i];
j++;
}
i++;
}
Copy(pNode->Data.MSSV,DanhSachThongTin[0]);
Copy(pNode->Data.HoTen,DanhSachThongTin[1]);
pNode->Data.DiemTin = atof(DanhSachThongTin[2]);
pNode->Data.DiemToan = atof(DanhSachThongTin[3]);
pNode->Data.DiemAV = atof(DanhSachThongTin[4]);
pNode->pNext = NULL;
AddLast(l,pNode);
}
//Ham copy chuoi va tao ky tu null cho chuoi duoc copy
void Copy(char* ChuoiDuocCop,char* ChuoiCop)
{
int i = 0;
while(ChuoiCop[i] != 'Ì')
{
ChuoiDuocCop[i] = ChuoiCop[i];
i++;
}
ChuoiDuocCop[i] = 0;
}
//Dinh nghia cac ham xu ly cua struct SINHVIEN
//Dinh nghia cac ham xu ly cua danh sach
void KhoiTaoDS(LIST &l) //Khoi tao cho mot danh sach
{
l.pHead = NULL;
l.pTail = NULL;
}
NODE* TaoNode(SINHVIEN Data) //Ham tao mot node tra ve dia chi node vua tao
{
NODE *pNode;
pNode= new NODE();
if(pNode == NULL)
{
return NULL;
}
pNode->Data = Gan(Data);
pNode->pNext = NULL;
return pNode;
}
//Ham tim mot node dua vao ho ten va mssv dua vao tra ve dia chi cua node can tim
NODE* TimNode(LIST l, SINHVIEN SV)
{
NODE* pNode = l.pHead;
if(pNode == NULL)
{
return NULL;
}
while(pNode != NULL)
{
if(Equal(pNode->Data,SV) == true)
{
return pNode;
}
pNode = pNode->pNext;
}
return NULL;
}
void TimSinhVien(LIST l) //Ham tim kiem mot sinh vien trong danh sach
{
SINHVIEN SV;
cout<<"
Nhap MSSV va Ho Ten sinh vien can tim:";
cout<<"
MSSV:";
cin>>SV.MSSV;
cout<<"
Ho Ten:";
cin>>SV.HoTen;
NODE *pNode = TimNode(l,SV);
if(pNode == NULL)
{
cout<<"Khong tim thay "
<<SV.HoTen<<" co MSSV "<<SV.MSSV<<" trong danh sach!
";
return;
}
cout<<"Da tim thay:";
XuatSV(pNode->Data);
}
void AddHead(LIST &l,NODE *pNode) //Ham them mot node vao dau danh sach
{
if(pNode == NULL)
{
return;
}
if(l.pHead == NULL)
{
l.pHead = l.pTail = pNode;
}
else
{
pNode->pNext = l.pHead;
l.pHead = pNode;
}
}
void AddLast(LIST &l,NODE *pNode) //Them mot node vao cuoi danh sach
{
if(pNode == NULL)
{
return;
}
if(l.pHead == NULL)
{
l.pHead = pNode;
l.pTail = l.pHead;
}
else
{
l.pTail->pNext = pNode;
l.pTail = pNode;
}
}
void AddAfter(LIST &l,NODE *pNode, SINHVIEN Data) //Them mot node vao sau mot node da co trong danh sach
{
if(pNode == NULL)
{
return;
}
NODE *pNewNode = TaoNode(Data);
if(pNewNode == NULL)
{
return;
}
if(l.pHead == NULL)
{
l.pHead = pNewNode;
}
else
{
pNewNode->pNext = pNode->pNext;
pNode->pNext = pNewNode;
}
}
int RemoveHead(LIST &l) //Xoa mot node o dau danh sach
{
NODE* pTemp = l.pHead;
if(pTemp == NULL)
{
return 0;
}
l.pHead = l.pHead->pNext;
delete pTemp;
return 1;
}
int RemoveLast(LIST &l) //Xoa mot node o cuoi danh sach
{
NODE *p = l.pHead;
NODE *pTemp = l.pTail;
if(pTemp == NULL)
{
return 0;
}
while(p->pNext != l.pTail)
{
p = p->pNext;
}
p->pNext = NULL;
l.pTail = p;
delete pTemp;
return 1;
}
int RemoveNode(LIST &l, SINHVIEN SV)
{
NODE *pNode = l.pHead;
if(pNode == NULL)
{
return 0;
}
if(Equal(pNode->Data,SV) == true)
{
RemoveHead(l);
return 1;
}
while(pNode->pNext != NULL)
{
if(Equal(pNode->pNext->Data,SV) == true)
{
break;
}
pNode = pNode->pNext;
}
NODE* pTemp = pNode->pNext;
if(pTemp == NULL)
{
return 0;
}
pNode->pNext = pTemp->pNext;
delete pTemp;
return 1;
}
void NhapDS(LIST &l) //Ham cho phep nhap du lieu vao danh sach su dung khi nhap du lieu tu man hinh
{
//KhoiTaoDS(l);
cout<<"
Nhap so node can nhap:";
cin>>n;
for(int i = 0; i < n; i++)
{
SINHVIEN Data;
NODE *pNode;
cout<<"
Nhap du lieu cho node:";
NhapSV(Data);
pNode = TaoNode(Data);
AddLast(l,pNode);
}
}
void XuatDS(LIST l) //Xuat ra mot danh sach
{
NODE *p = l.pHead;
if(l.pHead == NULL)
{
cout<<"
Danh sach rong !!
";
return;
}
else
{
cout<<"\tDanh sach sinh vien:";
while(p != NULL)
{
XuatSV(p->Data);
p = p->pNext;
}
}
}
void ResList(LIST &l)
{
NODE *pNode = l.pHead->pNext;
l.pHead->pNext = NULL;
if(pNode == NULL)
{
return;
}
while(pNode != NULL) //Vong lap dung de dao nguoc danh sach
{
NODE *pTemp = pNode;
pNode = pNode->pNext;
AddHead(l,pTemp);
}
pNode = l.pHead; //Sau khi dao nguoc danh sach gia tri pTail trung voi gia tri pHead
while(pNode->pNext != NULL) //Duyet lai danh sach de tim pTail
{
pNode = pNode->pNext;
}
l.pTail = pNode;
}
void PrintList(LIST l) //In ra danh sach cac sinh theo dieu kien
{
NODE* pNode = l.pHead;
if(pNode == NULL)
{
return;
}
while(pNode != NULL)
{
float DTB = TinhDTB(pNode->Data);
if(DTB >= 5)
{
XuatSV(pNode->Data);
cout<<"Diem trung binh la:"
<<DTB<<"
";
}
pNode = pNode->pNext;
}
}
//Dinh nghia cac ham xu ly cua danh sach
Bạn đang đọc truyện trên: Truyen247.Pro