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

struct Node {
  int data;
  struct Node *next;
};

typedef struct Node NODE;

struct List {
  NODE *pHead;
  NODE *pTail;
};

typedef struct List LIST;

void KhoiTao(LIST *ds) {
  ds->pHead = NULL;
  ds->pTail = NULL;
}

int KiemTraRong(const LIST *ds) {
  return (ds->pHead == NULL);
}

NODE* TaoNode(int x) {
  NODE *p = (NODE*)malloc(sizeof(NODE));
  if (p == NULL) {
    perror("KHONG DU BO NHO");
    return NULL;
  }
  p->data = x;
  p->next = NULL;
  return p;
}

void ChenCuoi(LIST *ds, NODE *p) {
  if (ds->pHead == NULL) {
    ds->pHead = p;
    ds->pTail = p;
  } else {
    ds->pTail->next = p;
    ds->pTail = p;
  }
}

void HienThiDanhSach(const LIST *ds) {
  if (KiemTraRong(ds)) {
    printf("Danh sach rong.\n");
    return;
  }

  NODE *current = ds->pHead;
  while (current != NULL) {
    printf("%d ", current->data);
    current = current->next;
  }
  printf("\n");
}

int main(void) {
  LIST ds;
  KhoiTao(&ds);
  int x = 10;
  int y = 20;
  NODE *p = TaoNode(x);
  NODE *a = TaoNode(y);
  ChenCuoi(&ds, p);
  ChenCuoi(&ds, a);

  HienThiDanhSach(&ds);

  
  

  printf("Danh sach sau khi them node moi: ");
  HienThiDanhSach(&ds);

  // Giải phóng bộ nhớ
  NODE *current = ds.pHead;
  while (current != NULL) {
    NODE *temp = current;
    current = current->next;
    free(temp);
  }

  return 0;
}