ft_lstnew - chanhl22/libft GitHub Wiki

  1 /* ************************************************************************** */
  2 /*                                                                            */
  3 /*                                                        :::      ::::::::   */
  4 /*   ft_lstnew.c                                        :+:      :+:    :+:   */
  5 /*                                                    +:+ +:+         +:+     */
  6 /*   By: chanhlee <[email protected].>       +#+  +:+       +#+        */
  7 /*                                                +#+#+#+#+#+   +#+           */
  8 /*   Created: 2021/01/29 10:57:19 by chanhlee          #+#    #+#             */
  9 /*   Updated: 2021/01/29 15:28:59 by chanhlee         ###   ########.fr       */
 10 /*                                                                            */
 11 /* ************************************************************************** */
 12 
 13 #include "libft.h"
 14 
 15 t_list  *ft_lstnew(void *content)
 16 {
 17     t_list *node; 
 18 
 19     if (!(node = malloc(sizeof(t_list))))    //๋…ธ๋“œ ์ƒ์„ฑ
 20         return (NULL); 
 21     node->content = content;                 // ์ƒ์„ฑ๋œ ๋…ธ๋“œ ๋‹ค์Œ์˜ ๋…ธ๋“œ
 22     node->next = NULL;                       // ๋‹ค์Œ์€ ๋…ธ๋“œ๊ฐ€ ์—†์Œ(NULL)
 23     return (node);                                                                        
 24 }

์ƒˆ๋กœ์šด ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , content ์— ์ž…๋ ฅ ๋ฐ›์€ ์š”์†Œ๋ฅผ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

  1. t_list ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ž…๋ ฅ ๋ฐ›์€ content ๋ฅผ ์š”์†Œ์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ด์คŒ.
  2. next ์—๋Š” NULL์„ ๋‹ด์•„์ฃผ์–ด์•ผ ํ•จ.
typedef struct  s_list             // ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ ๊ตฌ์กฐ์ฒด
{
    void           *content;       // ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋ฉค๋ฒ„
    struct s_list  *next;          // ๋‹ค์Œ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•  ํฌ์ธํ„ฐ
}                t_list;