Danh sách liên kết trong C

Mọi người cho em hỏi em sai ở đâu mà không chạy được ạ. Em dùng ngôn ngữ C.
Bài làm:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
	int data;
	struct node* Next;
};
typedef struct node NODE;

struct list
{
	NODE* head = NULL;
	NODE* tail = NULL;
};
typedef struct list LIST;


NODE *khoitaoNODE(int DATA)
{
	NODE *temp;
	temp = (NODE *)malloc(sizeof(NODE));
	temp->Next = NULL;
	temp->data = DATA;
	return temp;
}

void themdau(LIST *l,NODE *temp)
{
	if (l->head == NULL)
	{
		l->head = l->tail = temp;
	}
	else
	{
		temp->Next = l->head; // trỏ tới head hiện tại
		l->head = temp; // temp bh là head
	}
}

void themcuoi(LIST *l, NODE* temp)
{
	if (l->head == NULL)
	{
		l->head = l->tail = temp;
	}
	else
	{
		l->tail->Next = temp;
		l->tail = temp;
	}
}
void xuat(LIST *l)
{
	for (NODE* p = l->head; p != NULL; p = p->Next)
	{
		printf("%4d", p->data);
	}
}
int main()
{
	int n;
	LIST* list = (LIST*)malloc(sizeof(LIST));
	printf("\nNhap so luong phan tu: ");
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int x;
		printf("\nnhap gia tri: ");
		scanf("%d", &x);
		NODE* p;
		p = khoitaoNODE(x);
		themdau(list,p);
	}
	xuat(list);
	_getch();
	return 0;
}
struct list
{
	NODE* head = NULL;
	NODE* tail = NULL;
};

Trong C không có kiểu default như này. :slight_smile:

Bỏ = NULL đi.


Lần sau bạn nhớ post code lên nha, không qua link, nhỡ đâu thằng codepad nó chết thì sao. :slight_smile:

2 Likes

mình cảm ơn ạ :smile:

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