E vừa học danh sách liên kết đơn nhưng nó lại bị lỗi ở hàm Insert, e debug mà cũng k biết lỗi chỗ nào, mọi người giúp e với ạ
link code của e:
#include <iostream>
using namespace std;
struct Data{
int n;
};
struct Node{
Data data;
Node* pnext;
};
struct List{
Node* pHead;
Node* pTail;
};
Node* CreateNode(Data data);
Node* CreateNode(int n);
void InitList(List &lst);
bool IsEmpty(const List &lst);
void AddHead(List &lst, Node* pnode);
void AddHead(List &lst, int n);
void AddTail(List &lst,Node* pnode);
void AddTail(List &lst,int n);
int Size(const List &lst);
void PrintList(const List &lst,string);
void Insert(List &lst,Node* pnode,int index);
//void Insert(List &lst,int n,int index);
int main(){
List lst;
InitList(lst);
Node* node1=CreateNode(5);
AddHead(lst,5);
PrintList(lst,"Add head 1");
AddHead(lst,5);
PrintList(lst,"Add head 2");
AddHead(lst,5);
PrintList(lst,"Add head 3");
AddHead(lst,5);
PrintList(lst,"Add head 4");
AddHead(lst,5);
PrintList(lst,"Add head 5");
// Insert(lst,node1,0);
// PrintList(lst,"Add index 1");
// Insert(lst,node1,1);
// cout<<"Insert k loi\n";
//
// Insert(lst,node1,0);
// PrintList(lst,"Add index 1");
// Insert(lst,node1,0);
// PrintList(lst,"Add index 1");
}
Node* CreateNode(Data _data){
Node* ptemp;
ptemp=new Node;
ptemp->data=_data;
ptemp->pnext=nullptr;
return ptemp;
}
Node* CreateNode(int _n){
Data _data;
_data.n=_n;
return CreateNode(_data);
}
bool IsEmpty(const List &lst){
if(lst.pHead && lst.pTail){
return false;
}
else
return true;
}
void InitList(List &lst){
lst.pHead=nullptr;
lst.pTail=nullptr;
}
void AddHead(List &lst,Node* pnode){
if(IsEmpty(lst)){
lst.pHead=lst.pTail=pnode;
}
else{
pnode->pnext=lst.pHead;
lst.pHead=pnode;
}
}
void AddHead(List &lst,int n){
Node* pnode=CreateNode(n);
if(IsEmpty(lst)){
lst.pHead=lst.pTail=pnode;
}
else{
pnode->pnext=lst.pHead;
lst.pHead=pnode;
}
}
void AddTail(List &lst,Node* pnode){
if(IsEmpty(lst)){
lst.pHead=lst.pTail=pnode;
}
else{
lst.pTail->pnext=pnode;
lst.pTail=pnode;
}
}
void AddTail(List &lst, int n){
Node* pnode=CreateNode(n);
if(IsEmpty(lst)){
lst.pHead=lst.pTail=pnode;
}
else{
lst.pTail->pnext=pnode;
lst.pTail=pnode;
}
}
int Size(const List &lst){
int count=0;
Node* pnode=lst.pHead;
while(pnode){
count++;
pnode=pnode->pnext;
}
return count;
}
void PrintList(const List &lst,string mess){
cout<<mess<<endl;
if(IsEmpty(lst)){
cout<<"\nList is Empty!!";
}
else{
Node* pnode=lst.pHead;
while(pnode){
cout<<pnode->data.n<<"\t";
pnode=pnode->pnext;
}
}
cout<<endl;
}
void Insert(List &lst,Node* pnode,int index){
int size_list=Size(lst);
if(index>size_list || index<0){
cout<<"\nIndex is invalid(bigger than size list or be negative number)!!";
return;
}
if(index==size_list){
AddTail(lst,pnode);
}
else if(index==0){
AddHead(lst,pnode);
}
else{
Node* ptemp=lst.pHead;
for(int i=1;i<=index;i++){
if(i==index){
pnode->pnext=ptemp->pnext;
ptemp->pnext=pnode;
}
ptemp=ptemp->pnext;
}
}
}
//void Inset(List &lst,int n,int index){
// int size_list=Size(lst);
// if(index<0 || index>size_list){
// cout<<"\nInvalid index(is bigger than size list or negative number!!)";
// return;
// }
//
// if(index==0){
// AddHead(lst,n);
// }
// else if(index==size_list){
// AddTail(lst,n);
// }
// else{
// Node* pnode=CreateNode(n);
// Node* ptemp=lst.pHead;
// for(int i=1;i<=index;i++){
// if(i==index){
// pnode->pnext=ptemp->pnext;
// ptemp->pnext=pnode;
// }
// ptemp=ptemp->pnext;
// }
// }
//}
Em cảm ơn ạ!