#include<stdio.h>
#include<stdlib.h>
struct DNode{
int Data;
struct DNode *Next;
struct DNode *Previous;
};
struct DNode *create(){
struct DNode *head;
head=(struct DNode *)malloc(sizeof(struct DNode));
head->Next=head;
head->Previous=head;
}
void insert(struct DNode *head,int num){
struct DNode *p,*q;
p=head;
while(p->Next!=head){
p=p->Next;
}
q=(struct DNode *)malloc(sizeof(struct DNode));
q->Data=num;
q->Next=p->Next;
p->Next=q;
q->Previous=p;
q->Next->Previous=q;
}
void show(struct DNode *head){
struct DNode *p;
p=head->Next;
while(p!=head){
printf("%d\t",p->Data);
p=p->Next;
}
printf("\n");
}
void reShow(struct DNode *head){
struct DNode *p;
p=head->Previous;
while(p!=head){
printf("%d\t",p->Data);
p=p->Previous;
}
printf("\n");
}
int main(){
struct DNode *head;
head=create();
srand(time(NULL));
for(int i=0;i<10;i++){
insert(head,rand());
}
printf("正序输出:\n");
show(head);
printf("逆序输出:\n");
reShow(head);
}
|