双方向チェーンテーブルとループチェーンテーブル


マクロにより、ループチェーンテーブルまたは非ループチェーンテーブルとして構成できます.プログラム機能は以下の通りである:1、チェーンテーブルの作成2、前挿法挿入ノード3、尾挿法挿入ノード4、出力ノード及びチェーンテーブル長5、ノードの削除
くだらないことは言わないで、直接干物に行きます.
// LinkList.cpp :              。
//

#include "stdafx.h"

#define CYCLE_LINK_LIST 0  // 1:     , 0:     


typedef struct DualLinkListNode
{
    int data;

    struct DualLinkListNode *prior;
    struct DualLinkListNode *next;

}DualLinkList;

/****************************************************
     
*****************************************************/
DualLinkList* creatDualLinkList(void)
{
    DualLinkList *h, *p, *s;
    int n = 0, i = 0, x = 0;

    h = (DualLinkList*)malloc(sizeof(DualLinkList));

    //     ,        
    #if CYCLE_LINK_LIST == 1
    h->next = h;
    h->prior = h;
    #endif

    p = h;

    printf("         :");
    scanf_s("%d", &n);

    for(i = 0; i < n; i++)
    {
        printf("   %d   :", i+1);
        scanf_s("%d", &x);

        s = (DualLinkList*)malloc(sizeof(DualLinkList)); //      

        s->data = x;
        s->prior = p;

        #if CYCLE_LINK_LIST == 1
        s->next = h;//     
        h->prior = s;
        #else
        s->next = NULL;
        h->prior = NULL;

        #endif
        p->next = s;
        p = s;
    }

    return h;
}

/****************************************************
        ,   data
*****************************************************/
void insertBeforeDualLinkList(DualLinkList* list, int data)
{
    DualLinkList *p, *s;

    p = list;
    p = p->next; // p       (     )

    s = (DualLinkList*)malloc(sizeof(DualLinkList));

    s->data = data;
    s->prior = p->prior;
    s->next = p;
    p->prior->next = s;//   2    
    p->prior = s;

}

/****************************************************
        ,    data
*****************************************************/
void insertTailDualLinkList(DualLinkList* list, int data)
{
    DualLinkList *p, *s;

    p = list;
    p = p->next;

    #if CYCLE_LINK_LIST == 1
    while (p->next != list)
    #else
    while (p->next != NULL)
    #endif
    {
        p = p->next;
    }

    s = (DualLinkList*)malloc(sizeof(DualLinkList));

    s->data = data;
    s->prior = p;


    #if CYCLE_LINK_LIST == 1
    s->next = p->next;
    p->next->prior = s;
    #else
    s->next = NULL;

    #endif
    p->next = s;
    p = s;

}

/****************************************************
       
*****************************************************/
int CalcLengthOfDualLinkList(DualLinkList* list)
{
    DualLinkList *p;
    int len = 0;

    p = list;

    p = p->next;

    #if CYCLE_LINK_LIST == 1
    while (p != list)
    #else
    while (p != NULL)
    #endif
    {
        len++;
        p = p->next;
    }

    return len;
}

/****************************************************
      data   
*****************************************************/
void deleteDualLinkList(DualLinkList* list, int data)
{
    DualLinkList *p;

    p = list;

    p = p->next;

    while(p->data != data)
    {
        p = p->next;
    }

    p->prior->next = p->next;
    p->next->prior = p->prior;

    free(p);
}


/****************************************************
         
*****************************************************/
void printDualLinkList(DualLinkList* list)
{
    DualLinkList *p;

    p = list;

    p = p->next;

    #if CYCLE_LINK_LIST == 1
    while(list != p)
    #else
    while(p != NULL)
    #endif
    {
        printf("%d
", p->data);
p = p->next; } } /**************************************************** main *****************************************************/ int main(int argc, _TCHAR* argv[]) { DualLinkList* List; int data = 0; printf("1、 :"); List = creatDualLinkList(); printDualLinkList(List); printf(" :%d
", CalcLengthOfDualLinkList(List));
printf("2、 , :"); scanf_s("%d", &data); insertBeforeDualLinkList(List, data); printDualLinkList(List); printf(" :%d
", CalcLengthOfDualLinkList(List));
printf("3、 , :"); scanf_s("%d", &data); insertTailDualLinkList(List, data); printDualLinkList(List); printf(" :%d
", CalcLengthOfDualLinkList(List));
printf("4、 , :"); scanf_s("%d", &data); deleteDualLinkList(List, data); printDualLinkList(List); printf(" :%d
", CalcLengthOfDualLinkList(List));
system("pause"); return 0; }