测试输入:
1 2 3 4 5 -1
测试输出:
Link next:5,4,3,2,1,
Link prior:1,2,3,4,5,
NewL next:5,4,3,2,1,2,3,4,5,
NewL prior:5,4,3,2,1,2,3,4,5,
#include <stdio.h>
#include <stdlib.h>typedef int ElemType;
typedef struct DuLNode
{ElemType data; // 数据域struct DuLNode *prior; // 指向前驱的指针域struct DuLNode *next; // 指向后继的指针域
} DuLNode, *DuLinkList;// 函数原型
void out_next(DuLinkList);
void out_prior(DuLinkList);
void rcopy(DuLinkList); //这是你要编写的函数// 函数定义
void out_next(DuLinkList DHead)
{DuLinkList p = DHead->next;while (p != DHead){printf("%d,", p->data);p = p->next;}printf("\n");
}void out_prior(DuLinkList DHead)
{DuLinkList p = DHead->prior;while (p != DHead){printf("%d,", p->data);p = p->prior;}printf("\n");
}int main()
{DuLinkList DHead, p;int num;DHead = (DuLNode *)malloc(sizeof(DuLNode));DHead->data = -1;DHead->prior = DHead->next = DHead; // 生成表头scanf("%d", &num);while (num != -1){p = (DuLNode *)malloc(sizeof(DuLNode));p->data = num;p->next = DHead->next; // 1.链接p的next链DHead->next = p; // 2.链接DHead的next链p->next->prior = p; // 3.链接p的next的prior链p->prior = DHead; // 4.链接p的prior链scanf("%d", &num);}printf("Link next:");out_next(DHead);printf("Link prior:");out_prior(DHead);rcopy(DHead);printf("NewL next:");out_next(DHead);printf("NewL prior:");out_prior(DHead);return 0;
}void rcopy(DuLinkList DHead)
{DuLinkList r = DHead->next, s = DHead->next, t;while (r->next != DHead){r = r->next;s = s->next;}while (r->prior != DHead){r = r->prior;t = (DuLinkList)malloc(sizeof(DuLNode));t->data = r->data;t->next = s->next;s->next = t;t->prior = s;s = s->next;DHead->prior = t;}
}