em có làm một bài quản lý sinh viên sử dụng Linked List thế này
#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
using namespace std;
typedef struct sinhvien
{
char name[30];
char account[10];
char subject[10];
int score;
} STUDENT;
typedef struct node
{
STUDENT sSV;
struct node *pNext;
} NODE;
typedef struct list
{
NODE *pHead;
NODE *pTail;
} LIST;
void KhoiTaoDS(LIST *l)
{
l->pHead = NULL;
l->pTail = NULL;
}
NODE *KhoiTaoNODE(STUDENT sv)
{
NODE *p;
p = new NODE;
if(p == NULL)
{
cout << "Cap phat bo nho khong thanh cong";
}
strcpy(p->sSV.name, sv.name);
strcpy(p->sSV.account, sv.account);
strcpy(p->sSV.subject, sv.subject);
p->sSV.score = sv.score;
p->pNext = NULL;
return p;
}
void ThemDau(LIST *l, NODE *p)
{
if(l->pHead == NULL)
{
l->pHead = l->pTail = p;
}
else
{
p->pNext = l->pHead;
l->pHead = p;
}
}
void ThemCuoi(LIST *l, NODE *p)
{
if(l->pHead == NULL)
{
l->pHead = l->pTail = p;
}
else
{
l->pTail->pNext = p;
l->pTail = p;
}
}
void InDS(LIST l)
{
cout << setw(30) << left << "Name" << setw(10) << left << "Account" << setw(10) << left << "Subject" << setw(5) << left << "Score" << endl;
for(NODE *i = l.pHead; i != NULL; i = i->pNext)
{
cout << setw(30) << left << i->sSV.name;
cout << setw(10) << left << i->sSV.account;
cout << setw(10) << left << i->sSV.subject;
cout << setw(5) << left << i->sSV.score;
cout << "\n";
}
}
void TimScore(LIST l, int score)
{
for(NODE *i = l.pHead; i != NULL; i = i->pNext)
{
if(i->sSV.score == score)
{
cout << "Name: " << i->sSV.name << "\n";
cout << "Account: " << i->sSV.account << "\n";
cout << "Subject: " << i->sSV.subject << "\n";
cout << "Score: " << i->sSV.score << "\n";
cout << "\n\n";
}
}
}
void XoaSinhVien(LIST *l, char acc[10])
{
if(l->pHead == NULL)
{
cout << "Khong ton tai danh sach\n";
}
else
{
for(NODE *i = l->pHead; i != NULL; i = i->pNext)
{
if(strcmp(i->pNext->sSV.account,acc) == 0)
{
NODE *d;
d = i->pNext;
i->pNext = d->pNext;
delete d;
}
else
{
cout << "Khong ton tai Account nay";
}
}
}
}
void NhapDanhSachSinhVien(LIST *l)
{
int n;
cout << "Nhap so luong sinh vien: ";
cin >> n;
for(int i = 1; i <= n; i ++)
{
STUDENT a;
cout << "\nNhap thong tin cho sinh vien thu " << i << "\n";
cout << "Name: ";
fflush(stdin);
gets(a.name);
cout << "Account: ";
fflush(stdin);
gets(a.account);
cout << "Subject: ";
fflush(stdin);
gets(a.subject);
do
{
cout << "Score: ";
cin>>a.score;
}
while(a.score<0||a.score>10);
NODE *p = KhoiTaoNODE(a);
ThemCuoi(l, p);
}
cout << "\n\n";
}
void LuuFile(LIST l)
{
ofstream outfile;
outfile.open("STUDENT.LOG");
outfile << setw(30) << left << "Name" << setw(10) << left << "Account" << setw(10) << left << "Subject" << setw(5) << left << "Score" << endl;
for(NODE *i = l.pHead; i != NULL; i = i->pNext)
{
outfile << setw(30) << left << i->sSV.name;
outfile << setw(10) << left << i->sSV.account;
outfile << setw(10) << left << i->sSV.subject;
outfile << setw(5) << left << i->sSV.score;
outfile << "\n";
}
}
int main()
{
LIST l;
KhoiTaoDS(&l);
int a;
do
{
cout << "*********************************************\n";
cout << "* STUDENT MANAGEMENT *\n";
cout << "*********************************************\n\n";
cout << "\n1. INSERT STUDENT";
cout << "\n2. DELETE STUDENT";
cout << "\n3. SEARCH STUDENT BASE ON SCORE";
cout << "\n4. SHOW STUDENT INFORMATION";
cout << "\n5. SAVE STUDENT INFORMATION";
cout << "\n\n6. EXIT\n\n";
cin >> a;
switch(a)
{
case 1:
{
NhapDanhSachSinhVien(&l);
cout << "\n\n";
break;
}
case 2:
{
char acc[10];
cout << "Ban muon xoa Account nao: ";
fflush(stdin);
gets(acc);
XoaSinhVien(&l,acc);
cout << "\n";
break;
}
case 3:
{
int score;
cout << "Nhap diem ban muon tim: ";
cin >> score;
TimScore(l, score);
break;
}
case 4:
{
InDS(l);
cout << "\n\n";
break;
}
case 5:
{
LuuFile(l);
cout << "Luu thanh cong vao file STUDENT.LOG\n";
break;
}
case 6:
{
break;
}
default:
{
cout << "\nMoi ban nhap lai\n";
}
}
}
while(a != 6);
return 0;
}
thế nhưng phần xóa một node bị lỗi và em không biết sửa thế nào.
mọi người giúp em với
em cảm ơn
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?