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;
}