TIẾP THEO LOẠT BÀI TẬP VỀ DANH SÁCH LIÊN KẾT ĐÔI MỘT ƯU ĐIỂM CỦA DSLK ĐÔI SO VỚI DSLK…
- User
TIẾP THEO LOẠT BÀI TẬP VỀ DANH SÁCH LIÊN KẾT ĐÔI MỘT ƯU ĐIỂM CỦA DSLK ĐÔI SO VỚI DSLK ĐƠN:
- Cho phép truy xuất đến phần tử đứng trước một phần tử bất kỳ nào đó. Từ đó, thao tác thêm 1 phần tử vào vị trí bất kỳ trong danh sách trở nên dễ dàng hơn.
MÔ TẢ BÀI TOÁN
Viết 1 chương trình game đơn giản như sau:
-
Chương trình cho phép người dùng (user) chọn thực hiện 1 thao tác nào đó từ Menu, trả về kết quả thực hiện thao tác đó.
-
Viết hàm cho phép thêm các giá trị y vào sau (hoặc vào trước) các giá trị x có trong danh sách. Nếu có nhiều giá trị x được lưu trữ trong danh sách thì chèn thêm giá trị y vào sau tất cả các giá trị x đó. Nếu danh sách không chứa x thì không thêm gì cả và xuất thông báo theo câu lệnh: cout << "\nCan't find the value "<<x;
INPUT & OUTPUT
- Theo template /###Begin banned keyword - each of the following line if appear in code will raise error. regex supported define include using ###End banned keyword/
#include using namespace std;
struct DNode { int info; DNode *pNext, *pPrev; };
struct DList { DNode *pHead, *pTail; };
//###INSERT CODE HERE -
int main() { DList L; init(L); int x,y,choice;
cout<<"MENU:"; cout<<"\n1. Create a DList"; cout<<"\n2. Print the DList"; cout<<"\n3. Insert a value at the front"; cout<<"\n4. Insert a value at the end"; cout<<"\n5. Insert a value after a given value (only for the first value found)"; cout<<"\n6. Insert a value before a given value (only for the first value found)"; cout<<"\n7. Insert a value after a given value (for all the same values)"; cout<<"\n8. Insert a value before a given value (for all the same values)"; cout<<"\n20. Exit"<<endl; while(true) { cout<<"\n\t\tPLEASE SELECT YOUR CHOICE: "; cin>>choice; switch(choice) { case 1: cout<<"\nEnter your positive integers until you enter -1 to finish: "; createList(L); break; case 2: cout<<"\nYour current DList: "; printList(L); break; case 3: cout<<"\nEnter a number: "; cin>>x; addHead(L,x); break; case 4: cout<<"\nEnter a number: "; cin>>x; addTail(L,x); break; case 5: cout<<"\nEnter two numbers: "; cin>>x>>y; addAfter(L,x,y); break; case 6: cout<<"\nEnter two numbers: "; cin>>x>>y; addBefore(L,x,y); break; case 7: cout<<"\nEnter two numbers: "; cin>>x>>y; addAfterMulti(L,x,y); break; case 8: cout<<"\nEnter two numbers: "; cin>>x>>y; addBeforeMulti(L,x,y); break; case 20: cout<<"\nGOOD BYE"; return 0; } } return 0;}
- Assistant
- User
fix lỗi In function 'void createList(DList&)': 26:9: error: 'addTail' was not declared in this scope In function 'void addTail(DList&, int)': 50:21: error: conversion from 'DNode*' to non-scalar type 'DNode' requested 51:12: error: base operand of '->' has non-pointer type 'DNode' 52:12: error: base operand of '->' has non-pointer type 'DNode' 53:12: error: base operand of '->' has non-pointer type 'DNode' 56:19: error: cannot convert 'DNode' to 'DNode*' in assignment 58:26: error: cannot convert 'DNode' to 'DNode*' in assignment
- Assistant
Rất tiếc vì những lỗi xảy ra ở đây. Dưới đây là các hàm đã được sửa lỗi:
- Hàm
createList()để người dùng nhập danh sách gồm các số nguyên dương:
void createList(DList &L) { int x; cin >> x; while (x != -1) { addTail(L, x); cin >> x; } }- Hàm
addTail()để thêm một giá trị vào cuối danh sách:
void addTail(DList &L, int x) { DNode *newNode = new DNode; newNode->info = x; newNode->pNext = nullptr; newNode->pPrev = L.pTail; if (!L.pHead) L.pHead = newNode; else L.pTail->pNext = newNode; L.pTail = newNode; }Với các hàm trên, lỗi trong hàm
createList()đã được khắc phục. - Hàm
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.