스 레 드 탱크 - 링크

(1) constant. h 파일
전역 의 매크로 정의 와 방법 을 저장 합 니 다.
 
#ifndef CONSTANT_H
#define CONSTANT_H

#include <malloc.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

typedef   enum status_t status_t;
enum status_t
{
	FAILED=0,
       SUCCESS=1, 
	NOT_FOUND=2,
};

typedef unsigned char u_int8_t;
typedef unsigned short u_int16_t;
typedef unsigned int u_int32_t;

#define malloc_thing(thing) (thing *)malloc(sizeof(thing))

//create bool
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# ifndef HAVE__BOOL
#  define _Bool signed char
# endif /* HAVE__BOOL */
# define bool _Bool
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
#endif /* HAVE_STDBOOL_H */
#ifndef FALSE
# define FALSE false
#endif /* FALSE */
#ifndef TRUE
# define TRUE  true
#endif /* TRUE */

#define max(x,y) ({ \
	typeof(x) _x = (x); \
	typeof(y) _y = (y); \
	_x > _y ? _x : _y; })


#define min(x,y) ({ \
	typeof(x) _x = (x); \
	typeof(y) _y = (y); \
	_x < _y ? _x : _y; })

#endif

 
(2)linked_list. h 파일
링크 의 대외 인터페이스 정의
 
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include "constant.h"

typedef status_t (*linked_list_match_t)(void *item, ...);
typedef struct linked_list_t  linked_list_t;
struct linked_list_t
{
	void (*insert_first)(linked_list_t *this,void *item); //     
	void (*insert_last)(linked_list_t *this,void *item); //     

                void *  (*find_first)(linked_list_t *this);                  //      
	void * (*find_last)(linked_list_t *this);                    //      
	void * (*find_at)(linked_list_t *this,linked_list_match_t match,void *item,...);
                                                                                                //    
                status_t  (*remove_first)(linked_list_t *this,void **item);  //      
	status_t  (*remove_last)(linked_list_t *this,void **item);  //        
	status_t (*remove_at)(linked_list_t *this,linked_list_match_t match,void **item,...);

               u_int32_t  (*get_count)(linked_list_t *this);           //    
               void (*destroy)(linked_list_t *this);                         //    
};
//      
linked_list_t * linked_list_create();
#endif

 
(2)linked_list. h 파일
링크 의 구체 적 실현
#include "linked_list.h"
#include <stdio.h>

#define DUG_LINKED_LIST  0
//      
typedef struct element_t element_t;
struct element_t
{
	element_t *next;
	element_t *pre;
	void *data;
};

element_t * element_create(void *data)
{
	element_t *e=malloc_thing(element_t);
	e->next=NULL;
	e->pre=NULL;
	e->data=data;
	return e;
}

void element_destroy(element_t *e)
{
    free(e);
}
//       
typedef struct private_linked_list_t private_linked_list_t;
struct private_linked_list_t
{
                  linked_list_t public; //    
	  element_t *first;     //   
	  element_t *last;     //   
                  u_int32_t count;     //    
};

static element_t* remove_element(private_linked_list_t *this,element_t *e)
{
	element_t *next, *previous;

	next = e->next;
	previous = e->pre;
	free(e);
	if (next)
	{
		next->pre = previous;
	}
	else
	{
		this->last = previous;
	}
	if (previous)
	{
		previous->next = next;
	}
	else
	{
		this->first = next;
	}
	if (--this->count == 0)
	{
		this->first = NULL;
		this->last = NULL;
	}
	return next;
}



static void insert_first(private_linked_list_t *this,void *item)
{
         element_t *e=element_create(item);
         if(this->count==0)
         {
		   this->first=e;
		   this->last=e;
         }
	  else
	  {
	         element_t *old=this->first;
	         e->next=old;
	         old->pre=e;
		  this->first=e;
	  }
	  this->count++;
}
static void insert_last(private_linked_list_t *this,void *item)
{
         element_t *e=element_create(item);
         if(this->count==0)
         {
		   this->first=e;
		   this->last=e;
         }
	  else
	  {
	         element_t *old=this->last;
	         e->pre=old;
	         old->next=e;
		  this->last=e;
	  }
	  this->count++;
}

static void * find_first(private_linked_list_t *this)
{
       if(this->count==0)
       {
               return NULL;
       }
	 else
	 {
	        return this->first->data;
	 }
}
static void * find_last(private_linked_list_t *this)
{
        if(this->count==0)
       {
               return NULL;
       }
	 else
	 {
	        return this->last->data;
	 }
}

static void * find_at(private_linked_list_t *this,linked_list_match_t match,void *item,void *p1,void *p2,void *p3)
{
       element_t *e=this->first;
       while(e)
       {
               if((match&&match(e->data,p1,p2,p3))||(!match&&e->data==item))
               {
                       return e->data;
               }
	        e=e->next;
       }
       return NULL;
}



static  status_t  remove_first(private_linked_list_t *this,void **item)
{
        if(this->count==0)
        {
                *item=NULL;
                return NOT_FOUND;
        }
	 else
	 {
		 
	       *item=this->first->data;
	 	remove_element(this, this->first);
		return SUCCESS;
	 }
	 
}
 static status_t remove_last(private_linked_list_t *this,void **item)
{
         if(this->count==0)
        {
                *item=NULL;
                return NOT_FOUND;
        }
	 else
	 { 
	       *item=this->last->data;
	 	remove_element(this, this->last);
		return SUCCESS;
	 }
}
 static status_t remove_at(private_linked_list_t *this,linked_list_match_t match,void **item,void *p1,void *p2,void *p3)
{
       element_t *e=this->first;
	 *item=NULL;
       while(e)
       {
               if((match&&match(e->data,p1,p2,p3))||(!match&&e->data== *item))
               {
                       #if  DUG_LINKED_LIST == 1
				    printf("linked_list_t => revove_at : find the element");
			  #endif
                       *item=e->data;
                       remove_element(this, e);
                       return SUCCESS;
               }
	        e=e->next;
       }
	#if  DUG_LINKED_LIST == 1
		printf("linked_list_t => revove_at : not  find the element");
	#endif
       return NOT_FOUND;
}

u_int32_t get_count(private_linked_list_t *this)
{
          return this->count;
}
void destroy(private_linked_list_t *this)
{
          void *item;
	  #if  DUG_LINKED_LIST == 1
			printf("linked_list_t => destroy
"); #endif while(remove_last(this, &item)==SUCCESS); free(this); } linked_list_t * linked_list_create() { private_linked_list_t *this=malloc_thing(private_linked_list_t); this->public.insert_first=(void (*)(linked_list_t *,void *))insert_first; this->public.insert_last=(void (*)(linked_list_t *,void *))insert_last; this->public.find_first=(void * (*)(linked_list_t *))find_first; this->public.find_last=(void * (*)(linked_list_t *))find_last; this->public.find_at=(void * (*)(linked_list_t *,linked_list_match_t,void *,...))find_at; this->public.remove_first=( status_t (*)(linked_list_t *,void **))remove_first; this->public.remove_last=( status_t (*)(linked_list_t *,void **))remove_last; this->public.remove_at=( status_t (*)(linked_list_t *,linked_list_match_t,void **,...))remove_at; this->public.get_count=(u_int32_t (*)(linked_list_t *))get_count; this->public.destroy=(void (*)(linked_list_t *))destroy; this->count=0; this->first=NULL; this->last=NULL; return &this->public; }

좋은 웹페이지 즐겨찾기