glib庫雙向鏈表GList介紹

來源:互聯網
上載者:User

標籤:

在上一篇文章裡我介紹了glib庫中單向鏈表的用法,這篇文章介紹glib庫雙向鏈表的用法,還是沿用上一篇文章的風格,採用在代碼中加入注釋來說明代碼,最後貼出程式的運行結果,然後加以少量說明。


雙向鏈表與單向鏈表的區別是,從一個節點,不僅能訪問到它的下一個節點,還能訪問到它的上一個節點,其定義如下:

struct GList
{
gpointer data;
   GList *next;
   GList *prev;
};

在這裡可以看到雙向鏈表比單向鏈表多了一個指向上一個節點的指標prev。下面給出一段雙向鏈表的示範代碼:

/*
 * file: g_list.c
 * desc: 這個檔案用於示範glib庫裡雙向鏈表GList的用法
 * compile: gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`
 */

#include <glib.h>

void display_list(GList *list)
{
    GList *it = NULL;

    /* 遍曆鏈表 */
    for (it = list; it; it = it->next) {
        printf("%s", it->data);
    }

    printf("/n");
}

int main(int argc, char *argv[])
{
    GList *list = NULL;

    /*
     * 向鏈表尾部追加節點
     */
    list = g_list_append(list, "one ");
    list = g_list_append(list, "two ");
    list = g_list_append(list, "three ");
    list = g_list_append(list, "four ");
    list = g_list_append(list, "five ");
    display_list(list);

    /* 在鏈表頭部插入*/
    list = g_list_prepend(list, "zero ");
    display_list(list);

    GList *it = NULL;

    /* 尋找鏈表中的節點 */
    it = g_list_find(list, "two ");
    printf("Find data "two ": %s/n", it->data);

    int index = 0;

    /* 確定鏈表指標指向鏈表中的第幾個節點 */
    index = g_list_position(list, it);
    printf("index of "two " is: %d/n", index);

    it = g_list_nth(list, 1);
    printf("%s/n", it->data);

    /* 從鏈表裡刪除一個節點 */
    printf("After remove data "three ", the list:/n");
    list = g_list_remove(list, "three ");
    display_list(list);

    /* 向鏈表裡插入一個節點 */
    printf("After insert string "INSERT " at index 3, the list:/n");
    list = g_list_insert(list, "INSERT ", 3);
    display_list(list);

    /* 採用內迴圈遍曆鏈表節點 */
    g_list_foreach(list, (GFunc)printf, NULL);
    printf("/n");

    GList *last = NULL;

    /* 取鏈表的最後一個節點 */
    last = g_list_last(list);

    /* 從最後一個節點開始遍曆鏈表 */
    for (it = last; it; it = it->prev) {        printf("%s", it->data);    }    printf("/n");    /* 銷毀鏈表 */    g_list_free(list);    return 0;}

在linux下使用下面的命令編譯:

gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`

編譯後程式啟動並執行結果如下:
$ ./g_list
one two three four five
zero one two three four five
Find data "two ": two
index of "two " is: 2
one
After remove data "three ", the list:
zero one two four five
After insert string "INSERT " at index 3, the list:
zero one two INSERT four five
zero one two INSERT four five
five four INSERT two one zero

現在你應該知道我為什麼在每個字串後面加一個空格了吧。

總結:在glib庫中,雙向鏈表的用法跟單向鏈表基本上是一致的,只是多了一個訪問上一個節點的指標prev。對於你的應用如果單向鏈表夠用,就沒有必要使用雙向鏈表,因為這裡要增加維護一個指標的開銷。

glib庫雙向鏈表GList介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.