双向链表与循环链表

通过宏,可配置为循环链表或非循环链表。程序功能如下:
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\n", 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\n", CalcLengthOfDualLinkList(List));


    printf("2、前插法,输入插入的数据:");
    scanf_s("%d", &data);
    insertBeforeDualLinkList(List, data);

    printDualLinkList(List);
    printf("链表长度:%d\n", CalcLengthOfDualLinkList(List));

    printf("3、尾插法,输入插入的数据:");
    scanf_s("%d", &data);
    insertTailDualLinkList(List, data);

    printDualLinkList(List);
    printf("链表长度:%d\n", CalcLengthOfDualLinkList(List));

    printf("4、删除的节点,其数据为:");
    scanf_s("%d", &data);
    deleteDualLinkList(List, data);

    printDualLinkList(List);
    printf("链表长度:%d\n", CalcLengthOfDualLinkList(List));


    system("pause");



    return 0;
}

你可能感兴趣的:(c,链表)