Greeting everyone. I am developing a library management application for my DSA class. We use the Borland graphics.h library to draw the user interface (as can be seen from this image here). As from the image, the left column consists a list of structs of book title, a.k.a DauSach
with this code:
struct DauSach {
char ISBN[MAX_ISBN + 1];
char tensach[MAX_TENSACH + 1];
int sotrang;
char tacgia[MAX_TACGIA + 1];
int nxb;
char theloai[MAX_THELOAI + 1];
PtrSach* First = nullptr;
int soluotmuon;
int soluong;
DauSach() {
}
DauSach(char* isbn, char* ten, int st, char* tg, int xb, char* tl) {
strcpy(ISBN, isbn);
strcpy(tensach, ten);
sotrang = st;
strcpy(tacgia, tg);
nxb = xb;
strcpy(theloai, tl);
First = nullptr;
soluong = 0;
soluotmuon = 0;
}
};
struct DS_DauSach {
int n;
DauSach* nodes[MAX_SIZE_DAUSACH];
DS_DauSach() {
n = 0;
}
~DS_DauSach() {
while (n) {
DeleteAllPtrSach(nodes[n - 1]->First);
delete nodes[--n];
}
}
};
Inside DauSach
is a single linked list of book, a.k.a Sach
with this struct below:
struct Sach {
char MASACH[MAX_MASACH + 1];
int trangthai;
char vitri[MAX_VITRI + 1];
Sach() {
}
Sach(char ms[MAX_MASACH + 1], int tt, char vt[MAX_VITRI + 1]) {
strcpy(MASACH, ms);
trangthai = tt;
strcpy(vitri, vt);
}
};
// NODE Sach - DSLK don
struct PtrSach {
Sach sach;
PtrSach* next;
};
From the image you can saw above, the right column is where the list of Sach
is shown. Each book has a delete function, so let say that you have a book title named “TEST”, the list of “TEST” book is TEST-0
TEST-1
TEST-2
. I want to delete the book TEST-1
, it should only be TEST-0
and TEST-1
. Step to delete the book is like this below.
First, I get the node of the current book with this code.
PtrSach* nodeSelect = GetPtrSach(DSDS.nodes[curDauSach]->First, 10 * (curPageSach - 1) + curSach);
PtrSach* GetPtrSach(PtrSach* First, int position) {
PtrSach* node = First;
for (int i = 0; node != nullptr; node = node->next) {
if (i == position) {
return node;
}
++i;
}
return nullptr; // Return nullptr if position is out of bounds
}
Then, after I have the node, I delete the book by the current name of the struct Sach
is selected, let say it is TEST-1
currently.
XoaPtrSachTheoMaSach(nodeSelect, sach.MASACH);
void XoaPtrSachTheoMaSach(PtrSach*& head, char* targetMASACH) {
if (head == nullptr) return; // Empty list
while (head != nullptr && strcmp(head->sach.MASACH, targetMASACH) == 0) {
PtrSach* tmp = head;
head = head->next;
delete tmp;
}
if (head == nullptr) return;
PtrSach* current = head;
while (current->next != nullptr) {
if (strcmp(current->next->sach.MASACH, targetMASACH) == 0) {
PtrSach* tmp = current->next;
current->next = current->next->next;
delete tmp;
}
else {
current = current->next;
}
}
}
After the deletion, I just print out the current list of book to the right column again with this code.
void DrawDanhMucSach() {
... // these previous part can be ignored
PtrSach* node = GetPtrSach(DSDS.nodes[curDauSach]->First, 10 * (curPageSach - 1));
for (int i = 0; node != nullptr && i < 10; node = node->next) {
DrawItemSach(node->sach, i++);
}
}
The problem that I am having is, while testing outside (and I can assure that) the function XoaPtrSachTheoMaSach
is working as expected, but after having the code inside the program, it doesn’t print out correctly. Instead the node of TEST-1
seems like it is not fully deleted, as there are still garbage value left in it (suspect that is nullptr or something? I have no idea). Here is the image of what I mean. The thing is, if the delete function works, the node should be deleted, and the next node should be printed out, or if the deleted node is the last node already, it should be assigned to nullptr and stop the print. Do anyone have any idea or fix on how can this happen in the first place?
Note: We are not allowed to use standard library, however, there is string
library inside, which is only allowed to be used with the strcpy
function or compare between the char[]
.