01/10/2018, 09:38
Hỏi về khởi tạo danh sách liên kết đơn
Chào mọi người, em có viết 1 chương trình khởi tạo danh sách liên kết đơn như này
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct node* Ref;
struct node
{
int key;
Ref next;
};
Ref getnode(int k)
{
Ref p;
p = (Ref)malloc(sizeof(struct node));
if (p = NULL)
{
cout << "Loi khong du bo nho" << endl;
exit(0);
}
p->key = k;
p->next = NULL;
return p;
}
void addFirst(Ref &head, Ref &tail, int k)
{
Ref p = getnode(k);
if (head == NULL)
{
head = tail = NULL;
}
else
{
p->next = head;
head = p;
}
}
void addTail(Ref &head, Ref &tail, int k)
{
Ref p = getnode(k);
if (head == NULL)
{
head = tail = NULL;
}
else
{
tail->next = p;
tail = p;
}
}
void main()
{
int k;
Ref head = NULL, tail = NULL;
while (1)
{
cout << "Nhap so nguyen ( nhap so 0 de thoat)" << endl;
cin >> k;
if (k == 0) break;
addFirst(head, tail, k);
}
system("pause");
}
Nhưng nó lại không chạy được ạ, báo lỗi Unhandled exception at 0x00B05912 in KhoiTao.exe: 0xC0000005: Access violation writing location 0x00000000. Mọi người cho em thao khảo ý kiến với ạ
Bài liên quan
if(p = NULL)
lên chảoặc, cảm ơn , xin lỗi đã làm phiền, haizz, sao cứ nhìn mãi ko ra :((