Exception thrown: write access violation. l.pTail was nullptr. Mình lỗi ngay dòng này l.pTail->pNext = p; XIN CAO NHÂN GIÚP ĐỠ
#include<iostream>
using namespace std;
struct Node
{
int Data;
struct Node* pNext;
};
typedef struct Node NODE;
struct List
{
NODE* pHead;
NODE* pTail;
};
typedef struct List LIST;
void Init(LIST& l)
{
l.pHead = NULL;
l.pTail = NULL;
}
NODE* GetNode(int x)
{
NODE* p = new NODE;
if (p == NULL)
return NULL;
p->Data = x;
p->pNext = NULL;
return p;
}
void AddHead(LIST& l, NODE* p)
{
if (l.pHead = NULL)
{
l.pHead = l.pTail = p;
}
else {
p->pNext = l.pHead;
l.pHead = p;
}
}
void AddTail(LIST& l, NODE* p)
{
if (l.pHead = NULL)
{
l.pHead = l.pTail = p;
}
else {
l.pTail->pNext = p;
l.pTail = p;
}
}
void Input(LIST& l)
{
int n;
printf("\nban muon nhap bao nhieu so: ");
scanf_s("%d", &n);
Init(l);
for (int i = 1; i <= n; i++)
{
int x;
printf("\nNhap vao bao nhieu node: ");
scanf_s("%d", &x);
NODE* p = GetNode(x);
AddTail(l, p);
}
}
void OutPut(LIST l)
{
for (NODE* p = l.pHead; p != NULL; p = p->pNext)
{
printf_s("%4d", p->Data);
}
}
int main()
{
LIST l;
Input(l);
OutPut(l);
system("pause");
return 0;