Code insert vào danh sách liên kết đơn bị lỗi

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 ạ!

Trên IDEOne cũng báo cho bạn lỗi gì rồi.
Định nghĩa 2 hàm cùng tên và cùng tham số.

4 Likes

e vừa gửi lại link mới á a, cái nãy là do e comment hàm lỗi xong copy 1 phần của hàm lỗi để chạy xem nó lỗi phần nào, mà e quên edit lại ạ.

Thế bây giờ lỗi bạn nhận được là gì? Tụi mình cần lỗi thì mới biết cách sửa.

4 Likes

khi e gọi hàm Insert, rồi gọi hàm PrintList thì nó lại lặp in ra vô hạn p.Head->data.n rồi p.Tail->data.n ạ, các hàm khác vẫn hoạt động bình thường ạ, hàm Insert chèn vào 1 phần tử mà sau khi chèn phần tử đó chiếm vị trí index (input) ạ, index bắt đầu từ 0 (chèn vào Head) đến n=Size List (Chèn vào Tail)

Lặp vô tận vì bạn chèn cùng 1 node (biến tên node1) vào 2 vị trí dẫn đến lặp vô tận vì pnext tham chiếu vào chính node đó.
Sau khi bạn gọi lần chèn thứ 2 thì giống với bạn gọi:

node* node1 = CreateNode(99);
node1->pnext = node1; // tử điểm
5 Likes

À là do em insert cùng 1 địa chỉ biến vào, nên nó mới lỗi, trên 1 list node không thể có cùng 2 node chung 1 ô nhớ, e nên làm thêm 1 hàm để kiểm tra nữa. E cảm ơn a ạ, e cứ nghĩ e gán sai hay gì đó, nên nghĩ mãi không ra :sweat_smile: :sweat_smile: !

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?