码迷,mamicode.com
首页 > 其他好文 > 详细

双链表

时间:2014-06-28 22:41:23      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color      c   type   

#include "stdafx.h"
#include "stdlib.h"

typedef int ElemType;
typedef struct Node
{
    ElemType data;
    struct Node *prev;
    struct Node *next;
} DLinkList;

void PrintList(DLinkList *&List)
{
    DLinkList *p = NULL;
    if (NULL == List || NULL == List->next)
        return;

    p = List->next;
    while (p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

void DLinkListCreat(DLinkList *&List)
{
    if (List)
        return;

    List = (DLinkList *)malloc(sizeof(DLinkList));
    if (!List)
        return;

    List->data = 0;
    List->prev = List->next = NULL;
}

void DLinkListInsert(DLinkList *&List, ElemType data)
{
    DLinkList *p    = NULL;
    DLinkList *node = NULL;
    node = (DLinkList *)malloc(sizeof(DLinkList));
    if (!node)
        return;
    node->data = data;

    p = List;
    while (p->next)
    {
        p = p->next;
    }

    node->prev = p;
    node->next = p->next;
    p->next = node;
}

void DLinkListMove(DLinkList *&List, ElemType data)
{
    DLinkList *p    = NULL;
    DLinkList *q    = NULL;

    if (NULL == List || NULL == List->next)
        return;

    p = List;
    while (p->next)
    {
        q = p->next;
        if (q->data == data)
        {
            if (NULL != q->next)
                q->next->prev = p;
            p->next = q->next;
            free(q);
            break;
        }
        
        p = p->next;
    }
}

int main(void)
{
    DLinkList *List = NULL;
    DLinkListCreat(List);
    DLinkListInsert(List, 10);
    DLinkListInsert(List, 8);
    DLinkListInsert(List, 3);
    DLinkListInsert(List, 7);
    DLinkListInsert(List, 2);
    DLinkListInsert(List, 1);
    DLinkListInsert(List, 5);
    DLinkListInsert(List, 3);
    DLinkListInsert(List, 4);
    DLinkListInsert(List, 9);
    PrintList(List);
    DLinkListMove(List, 10);
    PrintList(List);
    DLinkListMove(List, 9);
    PrintList(List);
    DLinkListMove(List, 11);
    PrintList(List);
    DLinkListMove(List, 1);
    PrintList(List);
    DLinkListMove(List, 5);
    PrintList(List);
    return 0;
}

 

 

双链表,布布扣,bubuko.com

双链表

标签:style   blog   color      c   type   

原文地址:http://www.cnblogs.com/jingmoxukong/p/3794307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!