1. 程式人生 > >ADT - DoublyLinkedList(雙向鏈表)

ADT - DoublyLinkedList(雙向鏈表)

pla 創建 printf fin conio.h one link include true

  本來還不會寫雙向鏈表的,但最近學習了二叉樹後,突然意識到這不就是雙向鏈表嘛,然後通過對二叉樹的理解,實現了一下雙向鏈表。

  Operation:

技術分享
//雙向鏈表
#define bool int
#define true 1
#define false 0;

typedef struct douLinkList
{
    struct douLinkList * next;        //下一個
    struct douLinkList * prev;        //前一個
    int Item;
}DouLL;


/* 實現插入功能 */
bool InsertLL();

/* 實現刪除功能 
*/ bool deleteLL(); bool InsertLL(DouLL * h, int X, int i) { DouLL * p; int j; j = 1; while(j < i - 1 && h) { h = h -> next; ++ j; } p = malloc(sizeof(DouLL)); p -> Item = X; p -> prev = h; p -> next = h -> next; h
-> next -> prev = p; h -> next = p; return true; } bool deleteLL(DouLL * h, int X) { DouLL * temp; if(!h) { fprintf(stderr, "鏈表已空!\n"); return false; } while(X != h -> Item && h) { h = h -> next; } temp = h; h
-> prev -> next = h -> next; h -> next -> prev = h -> prev; free(temp); return true; }
DoublyLinkedList.c

  創建部分可能會比較重要:/* main.c */

#define SIZE 10
int i; DouLL * head, * n, * p; head = malloc(sizeof(DouLL)); p = head;  /* p指針的作用很重要 */ p -> Item = 1; p -> prev = p -> next = NULL; for(i = 1; i < SIZE; i++) { n = malloc(sizeof(DouLL)); n -> Item = i + 1; n -> prev = p;  /* 防止找不到頭結點 */ p -> next = n; p = p -> next; } n -> next = NULL; /* 若要循環應改為: n -> next = head; */ head -> prev = NULL; /* head -> prev = n; */

  最後,因為比較簡單,加上只是為了測試一下,所以沒有多少功能,但更加加深了自己對鏈表的理解,雖然是學習了二叉樹之後才會寫的雙向鏈表(好像反了?)...

  示例代碼:

技術分享
//雙向鏈表
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

#define bool int
#define true 1
#define false 0;

typedef struct douLinkList
{
    struct douLinkList * next;        //下一個
    struct douLinkList * prev;        //前一個
    int Item;
}DouLL;


/* 實現插入功能 */
bool InsertLL();

/* 實現刪除功能 */
bool deleteLL();


bool InsertLL(DouLL * h, int X, int i)
{
    DouLL * p;
    int j;
    
    j = 1;
    while(j < i - 1 && h)
    {
        h = h -> next;
        ++ j;
    }

    p = malloc(sizeof(DouLL));
    p -> Item = X;
    p -> prev = h;
    p -> next = h -> next;
    h -> next -> prev = p;
    h -> next = p;
    return true;
}

bool deleteLL(DouLL * h, int X)
{
    DouLL * temp;

    if(!h)
    {
        fprintf(stderr, "鏈表已空!\n");
        return false;
    }
    while(X != h -> Item && h)
    {
        h = h -> next;
    }
    temp = h;
    h -> prev -> next = h -> next;
    h -> next -> prev = h -> prev;
    free(temp);
    return true;
}

int main()
{
    char c;
    int i, SIZE, X;
    DouLL * head, * n, * p;

    puts("初始鏈表長度:");
    scanf("%d", &SIZE);

    head = malloc(sizeof(DouLL));
    p = head;
    p -> Item = rand() % 100 + 50;
    p -> prev = p -> next = NULL;
    for(i = 1; i < SIZE; i++)
    {
        n = malloc(sizeof(DouLL));
        n -> Item = rand() % 1000 + 50;
        n -> prev = p;
        p -> next = n;
        p = p -> next;    
    }
    n -> next = NULL;
    head -> prev = NULL;

    puts("1) 顯示    2) 刪除");
    puts("3) 插入    4) 退出");
    while(1)
    {
        c = getch();
        if(c == 1)
        {
            puts("Order:");

            p = head;
            while(p)
            {
                printf("%d ", p -> Item);
                p = p -> next;
            }
            printf("NULL\n");

            puts("ReveOrder:");
            p = n;
            while(p)
            {
                printf("%d ", p -> Item);
                p = p -> prev;
            }
            printf("NULL\n");
        }
        if(c == 2)
        {
            printf("\n刪除:");
            scanf("%d", &X);
            p = head;
            deleteLL(p, X);
        }
        if(c == 3)
        {
            printf("\n插入(數據 位置):");
            scanf("%d %d", &X, &i);
            p = head;
            InsertLL(p, X, i);
        }
        if(c == 4)
            break;
    }
    return 0;
}
DoublyLinkedList.c

ADT - DoublyLinkedList(雙向鏈表)