Glib 학습 (2) 양 방향 링크

14811 단어 체인 테이블glib
먼저 glib 라 이브 러 리 의 도움말 문서 주 소 를 붙 입 니 다.
http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Doubly-Linked-Lists.html#g-list-find
양 방향 링크 와 단 방향 링크 의 많은 함수 기능 과 명칭 이 똑 같 기 때문에 여 기 는 일부 함수 에 대해 자세히 말 하지 않 고 구체 적 으로 모 르 는 것 이 있 으 면 위 에서 준 인터넷 주 소 를 통 해 알 수 있 습 니 다.
함수 양 방향 링크 의 구조 체 를 먼저 말씀 드 리 겠 습 니 다.
GList
typedef struct {
  gpointer data;
  GList *next;
  GList *prev;
} GList;

Doubly-Linked Lists — linked lists containing integer values or pointers to data, with the ability to iterate over the list in both directions。
이것 은 양 방향 링크 의 공식 적 인 설명 입 니 다. 그 중에서 말 하 는 데 이 터 는 int 형의 값 이나 포인터 유형 일 수 있 습 니 다. 저 는 32 가 표시 할 수 있 는 것 이면 된다 고 생각 합 니 다. 원 하신 다 면 char 형 도 저장 할 수 있 고 공간 을 낭비 하 는 것 이 라 고 생각 합 니 다.
그 다음은 그 가 지원 하 는 기능 함수 이다.
Synopsis
#include <glib.h>


                    GList;

GList*              g_list_append                       (GList *list,
                                                         gpointer data);
GList*              g_list_prepend                      (GList *list,
                                                         gpointer data);
GList*              g_list_insert                       (GList *list,
                                                         gpointer data,
                                                         gint position);
GList*              g_list_insert_before                (GList *list,
                                                         GList *sibling,
                                                         gpointer data);
GList*              g_list_insert_sorted                (GList *list,
                                                         gpointer data,
                                                         GCompareFunc func);
GList*              g_list_remove                       (GList *list,
                                                         gconstpointer data);
GList*              g_list_remove_link                  (GList *list,
                                                         GList *llink);
GList*              g_list_delete_link                  (GList *list,
                                                         GList *link_);
GList*              g_list_remove_all                   (GList *list,
                                                         gconstpointer data);
void                g_list_free                         (GList *list);

GList*              g_list_alloc                        (void);
void                g_list_free_1                       (GList *list);
#define             g_list_free1

guint               g_list_length                       (GList *list);
GList*              g_list_copy                         (GList *list);
GList*              g_list_reverse                      (GList *list);
GList*              g_list_sort                         (GList *list,
                                                         GCompareFunc compare_func);
gint                (*GCompareFunc)                     (gconstpointer a,
                                                         gconstpointer b);
GList*              g_list_insert_sorted_with_data      (GList *list,
                                                         gpointer data,
                                                         GCompareDataFunc func,
                                                         gpointer user_data);
GList*              g_list_sort_with_data               (GList *list,
                                                         GCompareDataFunc compare_func,
                                                         gpointer user_data);
gint                (*GCompareDataFunc)                 (gconstpointer a,
                                                         gconstpointer b,
                                                         gpointer user_data);
GList*              g_list_concat                       (GList *list1,
                                                         GList *list2);
void                g_list_foreach                      (GList *list,
                                                         GFunc func,
                                                         gpointer user_data);
void                (*GFunc)                            (gpointer data,
                                                         gpointer user_data);

GList*              g_list_first                        (GList *list);
GList*              g_list_last                         (GList *list);
#define             g_list_previous                     (list)
#define             g_list_next                         (list)
GList*              g_list_nth                          (GList *list,
                                                         guint n);
gpointer            g_list_nth_data                     (GList *list,
                                                         guint n);
GList*              g_list_nth_prev                     (GList *list,
                                                         guint n);

GList*              g_list_find                         (GList *list,
                                                         gconstpointer data);
GList*              g_list_find_custom                  (GList *list,
                                                         gconstpointer data,
                                                         GCompareFunc func);
gint                g_list_position                     (GList *list,
                                                         GList *llink);
gint                g_list_index                        (GList *list,
                                                         gconstpointer data);

void                g_list_push_allocator               (gpointer allocator);
void                g_list_pop_allocator                (void);

아래 에 예 프로그램 을 붙 여 놓 으 면 여 기 는 따로 설명 하지 않 습 니 다. 왜냐하면 단일 체인 표 와 비슷 한 부분 이 많 기 때 문 입 니 다.
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
//#include <glib/gprintf.h>

static gint
sort(gconstpointer p1, gconstpointer p2)//    ,    
{
    gint32 a, b;
    
    a = GPOINTER_TO_INT(p1);
    b = GPOINTER_TO_INT(p2);

    return (a > b ? +1 : a == b ? 0 : -1);
}

static gint
sort_r(gconstpointer p1, gconstpointer p2)//    
{
    gint32 a, b;
    
    a = GPOINTER_TO_INT(p1);
    b = GPOINTER_TO_INT(p2);

    return (a < b ? +1 : a == b ? 0 : -1);
}

static void
print(gpointer p1, gpointer p2)//    ,   P1
{
    g_printf("%d,", *(gint*)p1);
}

static void
test_list(void)
{
    GList *list = NULL;
    gint nums[10] = {0,1,2,3,4,5,6,7,8,9};

// GList* g_list_append(GList *list, gpointer data);//  ,          
    list = g_list_append(list, &nums[1]);
    g_printf("The first item should be '%d' now.\t\tResult: %d.
", nums[1], *(gint*)list->data); // GList* g_list_prepend(GList *list, gpointer data);// list = g_list_prepend(list, &nums[0]); // GList* g_list_first(GList *list);// g_printf("The first item should be '%d' now.\t\tResult: %d.
", nums[0], *(gint*)g_list_first(list)->data); // GList* g_list_insert(GList *list, gpointer data, gint position);// position data list = g_list_insert(list, &nums[2], 2); // GList* g_list_last(GList *list); g_printf("The last item should be '%d' now.\t\tResult: %d.
", nums[2], *(gint*)g_list_last(list)->data); // GList* g_list_insert_before(GList *list, GList *sibling, gpointer data); sibling data ,NULL list = g_list_insert_before(list, NULL, &nums[3]); g_printf("The last item should be '%d' now.\t\tResult: %d.
", nums[3], *(gint*)g_list_last(list)->data); // #define g_list_next (list) list , next g_printf("The second item should be '%d' now.\t\tResult: %d.
", nums[1], *(gint*)g_list_next(list)->data); // #define g_list_previous (list) list , , g_printf("The first item should be '%d' now.\t\tResult: %d.
", nums[0], *(gint*)g_list_previous(g_list_next(list))->data); // gint g_list_index(GList *list, gconstpointer data); data g_printf("The index of '%d' should be '%d' now.\t\tResult: %d.
", nums[2], 2, g_list_index(list, &nums[2])); // gint g_list_position(GList *list, GList *llink); link list g_printf("The position of the third item should be 2 now.\t\tResult: %d.
", g_list_position(list, g_list_next(list)->next)); // guint g_list_length(GList *list); g_printf("The length of list should be 4 now.\t\tResult: %d.
", g_list_length(list)); GList *lt = NULL; gint i; // GList* g_list_insert_sorted(GList *list, gpointer data, GCompareFunc func); func for (i = 4; i < 10; i++) lt = g_list_insert_sorted(lt, &nums[i], sort_r);// ,9,8,7,6,5,4 // GList* g_list_reverse(GList *list); , ,4,5,6,7,8,9 lt = g_list_reverse(lt); g_printf("The second half of list should be sored now.
Result:"); // gpointer g_list_nth_data(GList *list, guint n); n for (i = 4; i < 10; i++) g_printf("%d,",*(gint*)(g_list_nth_data(lt, i-4))); g_printf("
"); // GList* g_list_concat(GList *list1, GList *list2); list1 list2, list2 , list = g_list_concat(list, lt); g_printf("The list should have all items which should be sored now.
Result:"); // void g_list_foreach(GList *list, GFunc func, gpointer user_data); func list , print list ,user_data func g_list_foreach(list, print, NULL); g_printf("
"); // GList* g_list_sort(GList *list, GCompareFunc compare_func); func list = g_list_sort(list, sort_r);// g_printf("The list should have all items which should be sored reversed now.
Result:"); g_list_foreach(list, print, NULL); g_printf("
"); GList *lb = NULL; // GList* g_list_copy(GList *list); , , lb = g_list_copy(list); g_printf("The backup list should have the same item and sequence now.
Result:"); // GList* g_list_nth(GList *list, guint n); n for (i = 0; i < 10; i++) { GList *ltmp = g_list_nth(lb, i); g_printf("%d,", *(gint*)ltmp->data); } g_printf("
"); // GList* g_list_sort_with_data(GList *list, GCompareDataFunc compare_func, gpointer user_data); g_list_sort() , user_data lb = g_list_sort_with_data(lb, (GCompareDataFunc)sort, NULL); g_printf("The backup list should have all items which should be sored now.
Result:"); g_list_foreach(lb, print, NULL); g_printf("
"); GList *lall = NULL; lall = g_list_concat(list, lb); g_printf("The concated list should have all items now.
Result:"); g_list_foreach(lall, print, NULL); g_printf("
"); // GList* g_list_remove(GList *list, gconstpointer data); data, lall = g_list_remove(lall, &nums[0]); g_printf("The list should have only one '%d' item now.
Result:", nums[0]); g_list_foreach(lall, print, NULL); g_printf("
"); // GList* g_list_remove_all(GList *list, gconstpointer data); data, , lall = g_list_remove_all(lall, &nums[9]); g_printf("The list should not have '%d' item now.
Result:", nums[9]); g_list_foreach(lall, print, NULL); g_printf("
"); GList *ll = NULL; // GList* g_list_find(GList *list, gconstpointer data); data , g_printf("The list should find '%d' now.\t\tResutl: %d.
", nums[0], (ll = g_list_find(lall, &nums[0])) ? *(gint*)ll->data : -1); // GList* g_list_find_custom(GList *list, gconstpointer data, GCompareFunc func); func data, null g_printf("The list should not find '%d' now.\t\tResutl: %d.
", nums[9], (ll = g_list_find_custom(lall, &nums[9], sort)) ? *(gint*)ll->data : -1); // void g_list_free(GList *list); g_list_free(lall); } int main(void) { printf("BEGIN:
************************************************************
"); test_list(); printf("
************************************************************
DONE
"); return 0; }

다음은 실행 결과 입 니 다.
linux@ubuntu:~/16021/glibdemo$ ls Doubly_Linked_Lists.c  glist  hello  helloworld.c  slist  slist.c linux@ubuntu:~/16021/glibdemo$ gcc -o Doubly_Linked_Lists Doubly_Linked_Lists.c -lglib-2.0 linux@ubuntu:~/16021/glibdemo$ ls Doubly_Linked_Lists  Doubly_Linked_Lists.c  glist  hello  helloworld.c  slist  slist.c linux@ubuntu:~/16021/glibdemo$ ./Doubly_Linked_Lists  BEGIN: ************************************************************ The first item should be '1' now.               Result: 1. The first item should be '0' now.               Result: 0. The last item should be '2' now.                Result: 2. The last item should be '3' now.                Result: 3. The second item should be '1' now.              Result: 1. The first item should be '0' now.               Result: 0. The index of '2' should be '2' now.             Result: 2. The position of the third item should be 2 now.         Result: 2. The length of list should be 4 now.             Result: 4. The second half of list should be sored now. Result:4,5,6,7,8,9, The list should have all items which should be sored now. Result:0,1,2,3,4,5,6,7,8,9, The list should have all items which should be sored reversed now. Result:9,8,7,6,5,4,3,2,1,0, The backup list should have the same item and sequence now. Result:9,8,7,6,5,4,3,2,1,0, The backup list should have all items which should be sored now. Result:0,1,2,3,4,5,6,7,8,9, The concated list should have all items now. Result:9,8,7,6,5,4,3,2,1,0,0,1,2,3,4,5,6,7,8,9, The list should have only one '0' item now. Result:9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9, The list should not have '9' item now. Result:8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8, The list should find '0' now.           Resutl: 0. The list should not find '9' now.               Resutl: -1. ************************************************************ DONE linux@ubuntu:~/16021/glibdemo$

좋은 웹페이지 즐겨찾기