- Single Linked에서 reverse할 때의 어려움이 있어서 먼저 구현함.



#pragma once


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct _Node

{

int data;

struct _Node *next;

struct _Node *prev;

} 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_Pop_front(void *_list);


void List_Deinit(void *_list)

{

pList list = (pList)_list;


if (!list) {

// error

return;

}

// delete nodes

while (list->size > 0) {

List_Pop_front((void *) list);

}


free(list);

list = NULL;

}


int List_Push_front(void *_list, int _data)

{

pList list = (pList)_list;

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;

new_node->prev = list->tail;


if (list->size == 0) {

// if list is empty

list->head = new_node;

//list->tail = new_node;

}

else {

// if list is not empty

list->tail->next = new_node;

//list->tail = new_node;

}


list->tail = new_node;

list->cur = new_node;

list->size++;


return 0;

}


int List_Push_back(void *_list, int _data)

{

pList list = (pList)_list;

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

new_node->prev = NULL;


if (list->size == 0) {

// if list is empty

//list->head = new_node;

list->tail = new_node;

}

else {

// if list is not empty

list->head->prev = new_node;

//list->head = new_node;

}

list->head = new_node;

list->cur = new_node;

list->size++;


return 0;

}


void List_Pop_front(void *_list)

{

pList list = (pList)_list;

pNode node;


if (!list) {

// error

return;

}

if (list->size == 0) {

// list is empty

return;

}


node = list->tail;

list->tail = list->tail->prev;

if (list->tail == NULL) {

list->head = NULL;

list->cur = NULL;

}

else {

list->tail->next = NULL;

list->cur = list->tail;

}

free(node);

node = NULL;

list->size--;

}


void List_Pop_back(void *_list)

{

pList list = (pList)_list;

pNode node;


if (!list) {

// error

return;

}

if (list->size == 0) {

// list is empty

return;

}


node = list->head;

list->head = list->head->next;

if (list->head == NULL) {

list->tail = NULL;

list->cur = NULL;

}

else {

list->head->prev = NULL;

list->cur = list->head;

}


free(node);

node = NULL;

list->size--;

}

'Programming > C,C++' 카테고리의 다른 글

queue in c  (0) 2017.10.08
십진수 수에서 '0' 개수 세기  (0) 2017.04.20
binary2decimal  (0) 2017.04.19
List (C, Single Linked) / ..ing  (0) 2017.04.18
bubble sort 3 / int  (0) 2017.04.17
Posted by 루나s
,