#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; }
原文地址:http://www.cnblogs.com/jingmoxukong/p/3794307.html