- 구조체 선언
- 초기화
- push_front
- push_back 까지 구현
앞으로 계속 나머지 진행..
--- List.h ---
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Node
{
int data;
struct _Node *next;
} Node;
typedef Node* pNode;
typedef struct _List
{
int size;
pNode head;
pNode cur;
pNode tail;
} List;
typedef List* pList;
pList List_Init(void)
{
pList list;
list = (pList)malloc(sizeof(List));
if (!list) {
// error
return (pList)NULL;
}
memset(list, 0x00, sizeof(List));
list->size = 0;
list->head = NULL;
list->cur = NULL;
list->tail = NULL;
return list;
}
void List_Deinit(pList *list)
{
if (!list) {
// error
return;
}
// delete nodes
free(list);
list = NULL;
}
int List_Push_front(void *l, int data)
{
pList list = (pList)l;
if (!list) {
// error
return -1;
}
pNode new_node = (pNode)malloc(sizeof(Node));
if (!new_node) {
// error
return -1;
}
new_node->data = data;
new_node->next = NULL;
if (list->size == 0) {
// if empty
list->tail = new_node;
}
else {
list->head->next = new_node;
}
list->head = new_node;
list->cur = new_node;
list->size++;
return 0;
}
int List_Push_back(void *l, int data)
{
pList list = (pList)l;
if (!list) {
// error
return -1;
}
pNode new_node = (pNode)malloc(sizeof(Node));
if (!new_node) {
// error
return -1;
}
new_node->data = data;
new_node->next = list->tail;
if (list->size == 0) {
// if empty
list->head = new_node;
}
list->tail = new_node;
list->cur = new_node;
list->size++;
return 0;
}
'Programming > C,C++' 카테고리의 다른 글
List (C, Double Linked) / ...ing (0) | 2017.04.20 |
---|---|
binary2decimal (0) | 2017.04.19 |
bubble sort 3 / int (0) | 2017.04.17 |
strstr 구현하기 (0) | 2017.04.17 |
간단한 thread 예제 (0) | 2017.04.16 |