/*****************************************************************************
* *
* -------------------------------- list.c -------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "list.h"
int select(void);
void print();
void search(void *data);
List start;
void main()
{
list_init(&start,NULL);
select();
}
int select(void)
{
int input;
void *add;
while(1)
{
printf("n");
printf(" ┏━━━ list menu v1.0 ━━━┓ n");
printf(" ┃ 1 -> list init ┃n");
printf(" ┃ 2 -> list insert ┃n");
printf(" ┃ 3 -> list remove ┃n");
printf(" ┃ 4 -> list search ┃n");
printf(" ┃ 5 -> list print ┃n");
printf(" ┃ 0 -> Exit ┃n");
printf(" ┣━━━━━━━━━━━━━━┫n");
printf(" ┃select number ┃n");
printf(" ┗━━━━━━━━━━━━━━┛n");
printf(" ==> ");
scanf("%d",&input);
switch(input)
{
case 1:
list_destroy(&start);
list_init(&start,NULL);
system("cls");
break;
case 2:
system("cls");
printf(" Data insert -> ");
scanf("%d",&add);
if((list_ins_next(&start,NULL,add)) !=0 )
{
printf(" Not Insert ");
return -1;
}
printf(" Insert Ok!nn");
break;
case 3:
system("cls");
printf(" Remove Data ");
scanf("input",add);
list_rem_next(&start,NULL,&add);
break;
//원하는 데이타 삭제방법 두번째 파라미터에 넣는값을 모름
case 4:
system("cls");
printf("==>");
scanf("%d",&add);
search(add);
break;
case 5:
system("cls");
print();
break;
case 0:
list_destroy(&start);
return 0;
default :
system("cls");
printf(" You select wrong number Try to again selectnn");
break;
}
}
}
void print()
{
ListElmt *copy;
copy = (&start)->head;
printf(" This list size is %d n",list_size(&start));
printf(" list");
while(copy != NULL)
{
printf(" --> %d",copy->data);
copy = list_next(copy);
}
printf(" --> NULL n");
return;
}
void search(void *data)
{
ListElmt *it;
it = (&start)->head;
if(it == NULL)
{
printf(" No Data in List ");
return;
}
for(it = (&start)->head;it!=NULL;it = list_next(it))
{
if(it->data == data)
{
printf(" Data Find OK ");
return;
}
}
printf(" --> Data NO Find n");
return;
}
/*****************************************************************************
* *
* ------------------------------- list_init ------------------------------ *
* *
*****************************************************************************/
void list_init(List *list, void (*destroy)(void *data)) {
/*****************************************************************************
* *
* Initialize the list. *
* *
*****************************************************************************/
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
return;
}
/*****************************************************************************
* *
* ----------------------------- list_destroy ----------------------------- *
* *
*****************************************************************************/
void list_destroy(List *list) {
void *data;
/*****************************************************************************
* *
* Remove each element. *
* *
*****************************************************************************/
while (list_size(list) > 0) {
if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy !=
NULL) {
/***********************************************************************
* *
* Call a user-defined function to free dynamically allocated data. *
* *
***********************************************************************/
list->destroy(data);
}
}
/*****************************************************************************
* *
* No operations are allowed now, but clear the structure as a precaution. *
* *
*****************************************************************************/
memset(list, 0, sizeof(List));
return;
}
/*****************************************************************************
* *
* ----------------------------- list_ins_next ---------------------------- *
* *
*****************************************************************************/
int list_ins_next(List *list, ListElmt *element, const void *data) {
ListElmt *new_element;
/*****************************************************************************
* *
* Allocate storage for the element. *
* *
*****************************************************************************/
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the element into the list. *
* *
*****************************************************************************/
new_element->data = (void *)data;
if (element == NULL) {
/**************************************************************************
* *
* Handle insertion at the head of the list. *
* *
**************************************************************************/
if (list_size(list) == 0)
list->tail = new_element;
new_element->next = list->head;
list->head = new_element;
}
else {
/**************************************************************************
* *
* Handle insertion somewhere other than at the head. *
* *
**************************************************************************/
if (element->next == NULL)
list->tail = new_element;
new_element->next = element->next;
element->next = new_element;
}
/*****************************************************************************
* *
* Adjust the size of the list to account for the inserted element. *
* *
*****************************************************************************/
list->size++;
return 0;
}
/*****************************************************************************
* *
* ----------------------------- list_rem_next ---------------------------- *
* *
*****************************************************************************/
int list_rem_next(List *list, ListElmt *element, void **data) {
ListElmt *old_element;
/*****************************************************************************
* *
* Do not allow removal from an empty list. *
* *
*****************************************************************************/
if (list_size(list) == 0)
return -1;
/*****************************************************************************
* *
* Remove the element from the list. *
* *
*****************************************************************************/
if (element == NULL) {
/**************************************************************************
* *
* Handle removal from the head of the list. *
* *
**************************************************************************/
*data = list->head->data;
old_element = list->head;
list->head = list->head->next;
if (list_size(list) == 1)
list->tail = NULL;
}
else {
/**************************************************************************
* *
* Handle removal from somewhere other than the head. *
* *
**************************************************************************/
if (element->next == NULL)
return -1;
*data = element->next->data;
old_element = element->next;
element->next = element->next->next;
if (element->next == NULL)
list->tail = element;
}
/*****************************************************************************
* *
* Free the storage allocated by the abstract data type. *
* *
*****************************************************************************/
free(old_element);
/*****************************************************************************
* *
* Adjust the size of the list to account for the removed element. *
* *
*****************************************************************************/
list->size--;
return 0;
}
/*****************************************************************************
* *
* -------------------------------- list.h -------------------------------- *
* *
*****************************************************************************/
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
/*****************************************************************************
* *
* Define a structure for linked list elements. *
* *
*****************************************************************************/
typedef struct ListElmt_ {
void *data;
struct ListElmt_ *next;
} ListElmt;
/*****************************************************************************
* *
* Define a structure for linked lists. *
* *
*****************************************************************************/
typedef struct List_ {
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
} List;
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
void list_init(List *list, void (*destroy)(void *data));
void list_destroy(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next)
#endif
* *
* -------------------------------- list.c -------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "list.h"
int select(void);
void print();
void search(void *data);
List start;
void main()
{
list_init(&start,NULL);
select();
}
int select(void)
{
int input;
void *add;
while(1)
{
printf("n");
printf(" ┏━━━ list menu v1.0 ━━━┓ n");
printf(" ┃ 1 -> list init ┃n");
printf(" ┃ 2 -> list insert ┃n");
printf(" ┃ 3 -> list remove ┃n");
printf(" ┃ 4 -> list search ┃n");
printf(" ┃ 5 -> list print ┃n");
printf(" ┃ 0 -> Exit ┃n");
printf(" ┣━━━━━━━━━━━━━━┫n");
printf(" ┃select number ┃n");
printf(" ┗━━━━━━━━━━━━━━┛n");
printf(" ==> ");
scanf("%d",&input);
switch(input)
{
case 1:
list_destroy(&start);
list_init(&start,NULL);
system("cls");
break;
case 2:
system("cls");
printf(" Data insert -> ");
scanf("%d",&add);
if((list_ins_next(&start,NULL,add)) !=0 )
{
printf(" Not Insert ");
return -1;
}
printf(" Insert Ok!nn");
break;
case 3:
system("cls");
printf(" Remove Data ");
scanf("input",add);
list_rem_next(&start,NULL,&add);
break;
//원하는 데이타 삭제방법 두번째 파라미터에 넣는값을 모름
case 4:
system("cls");
printf("==>");
scanf("%d",&add);
search(add);
break;
case 5:
system("cls");
print();
break;
case 0:
list_destroy(&start);
return 0;
default :
system("cls");
printf(" You select wrong number Try to again selectnn");
break;
}
}
}
void print()
{
ListElmt *copy;
copy = (&start)->head;
printf(" This list size is %d n",list_size(&start));
printf(" list");
while(copy != NULL)
{
printf(" --> %d",copy->data);
copy = list_next(copy);
}
printf(" --> NULL n");
return;
}
void search(void *data)
{
ListElmt *it;
it = (&start)->head;
if(it == NULL)
{
printf(" No Data in List ");
return;
}
for(it = (&start)->head;it!=NULL;it = list_next(it))
{
if(it->data == data)
{
printf(" Data Find OK ");
return;
}
}
printf(" --> Data NO Find n");
return;
}
/*****************************************************************************
* *
* ------------------------------- list_init ------------------------------ *
* *
*****************************************************************************/
void list_init(List *list, void (*destroy)(void *data)) {
/*****************************************************************************
* *
* Initialize the list. *
* *
*****************************************************************************/
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
return;
}
/*****************************************************************************
* *
* ----------------------------- list_destroy ----------------------------- *
* *
*****************************************************************************/
void list_destroy(List *list) {
void *data;
/*****************************************************************************
* *
* Remove each element. *
* *
*****************************************************************************/
while (list_size(list) > 0) {
if (list_rem_next(list, NULL, (void **)&data) == 0 && list->destroy !=
NULL) {
/***********************************************************************
* *
* Call a user-defined function to free dynamically allocated data. *
* *
***********************************************************************/
list->destroy(data);
}
}
/*****************************************************************************
* *
* No operations are allowed now, but clear the structure as a precaution. *
* *
*****************************************************************************/
memset(list, 0, sizeof(List));
return;
}
/*****************************************************************************
* *
* ----------------------------- list_ins_next ---------------------------- *
* *
*****************************************************************************/
int list_ins_next(List *list, ListElmt *element, const void *data) {
ListElmt *new_element;
/*****************************************************************************
* *
* Allocate storage for the element. *
* *
*****************************************************************************/
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the element into the list. *
* *
*****************************************************************************/
new_element->data = (void *)data;
if (element == NULL) {
/**************************************************************************
* *
* Handle insertion at the head of the list. *
* *
**************************************************************************/
if (list_size(list) == 0)
list->tail = new_element;
new_element->next = list->head;
list->head = new_element;
}
else {
/**************************************************************************
* *
* Handle insertion somewhere other than at the head. *
* *
**************************************************************************/
if (element->next == NULL)
list->tail = new_element;
new_element->next = element->next;
element->next = new_element;
}
/*****************************************************************************
* *
* Adjust the size of the list to account for the inserted element. *
* *
*****************************************************************************/
list->size++;
return 0;
}
/*****************************************************************************
* *
* ----------------------------- list_rem_next ---------------------------- *
* *
*****************************************************************************/
int list_rem_next(List *list, ListElmt *element, void **data) {
ListElmt *old_element;
/*****************************************************************************
* *
* Do not allow removal from an empty list. *
* *
*****************************************************************************/
if (list_size(list) == 0)
return -1;
/*****************************************************************************
* *
* Remove the element from the list. *
* *
*****************************************************************************/
if (element == NULL) {
/**************************************************************************
* *
* Handle removal from the head of the list. *
* *
**************************************************************************/
*data = list->head->data;
old_element = list->head;
list->head = list->head->next;
if (list_size(list) == 1)
list->tail = NULL;
}
else {
/**************************************************************************
* *
* Handle removal from somewhere other than the head. *
* *
**************************************************************************/
if (element->next == NULL)
return -1;
*data = element->next->data;
old_element = element->next;
element->next = element->next->next;
if (element->next == NULL)
list->tail = element;
}
/*****************************************************************************
* *
* Free the storage allocated by the abstract data type. *
* *
*****************************************************************************/
free(old_element);
/*****************************************************************************
* *
* Adjust the size of the list to account for the removed element. *
* *
*****************************************************************************/
list->size--;
return 0;
}
/*****************************************************************************
* *
* -------------------------------- list.h -------------------------------- *
* *
*****************************************************************************/
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
/*****************************************************************************
* *
* Define a structure for linked list elements. *
* *
*****************************************************************************/
typedef struct ListElmt_ {
void *data;
struct ListElmt_ *next;
} ListElmt;
/*****************************************************************************
* *
* Define a structure for linked lists. *
* *
*****************************************************************************/
typedef struct List_ {
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
} List;
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
void list_init(List *list, void (*destroy)(void *data));
void list_destroy(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);
#define list_size(list) ((list)->size)
#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_is_head(list, element) ((element) == (list)->head ? 1 : 0)
#define list_is_tail(element) ((element)->next == NULL ? 1 : 0)
#define list_data(element) ((element)->data)
#define list_next(element) ((element)->next)
#endif
출처 - C로 구현한 알고리즘 , 한빛미디어