/*****************************************************************************
*  -------------------------------- list.c --------------------------------  *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "list.h"
/*****************************************************************************
*  ------------------------------- 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;
}



'Programing > 자료구조' 카테고리의 다른 글

이중연결리스트 노드클래스  (0) 2016.12.01
이중연결리스트.cpp  (0) 2016.12.01
단일연결리스트.h  (0) 2016.11.30
이중연결리스트  (0) 2016.11.30
단일연결리스트 head만 사용  (0) 2016.11.30

/*****************************************************************************
*  -------------------------------- 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



'Programing > 자료구조' 카테고리의 다른 글

이중연결리스트.cpp  (0) 2016.12.01
단일연결리스트.cpp  (0) 2016.11.30
이중연결리스트  (0) 2016.11.30
단일연결리스트 head만 사용  (0) 2016.11.30
단일 연결리스트 더미노드  (0) 2016.11.30
//****************************************************************************
//단일 연결리스트 head노드만을 이용 !!
//****************************************************************************

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

typedef struct _Node
{
int data;
struct _Node *next;
}Node;
//****************************************************************************
Node * head;
//****************************************************************************
Node *CreateNode(int data)
{
Node * newnode = (Node *)malloc(sizeof(Node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}//노드 생성 함수 !!
//****************************************************************************
void init()
{
head = CreateNode(0);
}
//****************************************************************************
// 삭제 함수
//****************************************************************************
//맨 뒤의 노드를 삭제 !!
//****************************************************************************
void pop_back()
{
Node *p, *s;
if(head->next == NULL){
puts("list empty !!");
}
else{
s= head;
p = head->next;
while(p->next != NULL){
s = p;
p = p->next;
}
}
s ->next = NULL;
free(p);
}
//****************************************************************************
//맨 앞의 노드를 삭제 !!
//****************************************************************************
void pop_front()
{
Node *p;
if(head->next == NULL){
printf("list empty !! \n");
}
else{
p = head->next;
head->next = p->next;
free(p);
}
}
//****************************************************************************
// data에 해당하는 노드 삭제!!
//****************************************************************************
void erase_value(int data)
{
Node *p,*s;
p = head->next;
s = head;
while(p->next != NULL){
if(p->data == data){
s->next = p->next;
free(p);
return;
}
else{
s=p;
p = p->next;
}
}
printf("No date");
puts("");
}
//****************************************************************************
// 1번째 3번째
//****************************************************************************
void erase_idx(int idx)
{
Node *p,*s;
p = head->next;
s = head;
int i=0;
while(p->next != NULL){
if(i == idx-1){
s->next = p->next;
free(p);
return;
}
else{
s = p;
p = p->next;
}
i++;
}
printf("No date");
puts("");

}
//****************************************************************************
//****************************************************************************
//입력 함수들
//****************************************************************************
void push_back(int data)
{
Node * p = head;
Node * newnode = CreateNode(data);
while(p->next != NULL)
{
p = p->next;
}
p ->next = newnode;
}
void push_front(int data)
{
Node *newnode = CreateNode(data);
newnode->next = head->next;
head->next = newnode;
}
void insert(int key, int data)
{
Node * p = head->next;
while(p->next != NULL){
if(p->next->data == key) {
Node * newnode =CreateNode(data);
newnode->data = data;
newnode->next = p->next;
p->next = newnode;
return;
}
else{
p = p->next;
}
}
push_back(data);
}
void insert_back(int key, int data)
{
Node *p = head->next;
while(p->next !=NULL){
if(p->data == key){
Node *newnode = CreateNode(data);
p->next->data = data;
newnode->next = p->next;
p->next = newnode;
return;
}
else{
p = p->next;
}
}
push_back(data);
}
void show()
{
Node * p = head->next;
while(p != NULL){
printf("%d --> ",p->data);
p = p->next;
}
puts("");
}
int Find_Number(int num)
{
Node *p=head;
while(p->next != NULL)
{
if(p->data == num)
{
printf("%d\n",p->data);
return p->data;
}
else
{
p = p->next;
}
}
puts("nonono");
return 0;
}
int Fine_idx(int data)
{
int i=0;
Node *p=head;
while(p->next != NULL){
if(i == data){
printf("%d\n",p->data);
return p->data;
}
else{
p = p->next;
}
i++;
}
puts("nonono");
return NULL;
}
int Fine_Back()
{
Node *p = head;
while(p->next != NULL){
p = p->next;
}
printf("%d\n",p->data);
return p->data;
}
int Fine_Start()
{
Node *p = head->next;
if(p){
printf("%d\n",p->data);
return p->data;
}
else{
puts("empty data");
}
return 0;
}

void main()
{
init();
push_back(5);
show();
push_back(4);
show();
push_front(2);
show();
push_front(15);
show();
push_front(17);
show();
insert(2,40);
show();
insert_back(15,33);
show();
Find_Number(2);
Fine_idx(1);

}


'Programing > 자료구조' 카테고리의 다른 글

단일연결리스트.cpp  (0) 2016.11.30
단일연결리스트.h  (0) 2016.11.30
이중연결리스트  (0) 2016.11.30
단일 연결리스트 더미노드  (0) 2016.11.30
자료구조  (0) 2016.11.30

+ Recent posts