양방향 연결 리스트


노드

typedef struct s_node
{
		int    value;
		t_node *prev;
		t_node *next;
}              t_node;

리스트

typedef struct s_list
{
		t_node *head;
		t_node *tail;
}              t_list;

head

tail

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

typedef struct s_node t_node;

typedef struct s_node
{
	int		value;
	t_node *prev;
	t_node *next;
}				t_node;

typedef struct s_list
{
	t_node *head;
	t_node *tail;
}				t_list;

void	makeNode(t_list *A, int value)
{
	t_node	*node = (t_node *)malloc(sizeof(t_node));

	node->value = value;
	if (A->head == NULL)
	{
		A->head = node;
		A->tail = node;
	}
	else
	{

		A->tail->next = node;
		node->prev = A->tail;
		A->tail = node;
	}
	// free(node);
}

int main()
{
	t_list *A;

	A->head = NULL;
	A->tail = NULL;

	makeNode(A, 1);
	printf("==%d\\n", A->head->value);
	makeNode(A, 2);	
	printf("==%d\\n", A->head->value);
	makeNode(A, 3);	
	printf("==%d\\n", A->head->value);
	return (0);
}

malloc 을 해줘야 하는 이유 (free를 하면 안되는 이유)

노드를 새로 추가할때 malloc을 안해주거나 free를 해주면 같은 주소값에 새로운 노드 값이 들어가기 때문에 덮어씌우기(?)가 된다.

따라서 malloc을 계속 해주어야 그 공간에 값이 있으므로 다른 주소값을 지정해주기 때문에 덮어씌워지는 것이 방지된다.