#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct Node{
int Data;
struct Node*Next,*privous;
};
struct Node* Creat(){
struct Node*L;
L=(struct Node*)malloc(sizeof(struct Node));
L->Next=L;
L->privous=L;
return L;
}
int length_Node(struct Node*L){
int i;
struct Node*p;
p=L->Next;
while(p!=L){
i++;
p=p->Next;
}
return i;
}
void Print(struct Node *L){
struct Node *q;
int i=length_Node(L);
int j=0;
q=L->Next;
while(j<i){
printf("%d ",q->Data);
q=q->Next;
j++;
}
}
void Print2(struct Node *L){
struct Node *q;
q=L->privous;
int i=length_Node(L);
int j=0;
while(j<i){
printf("%d ",q->Data);
q=q->privous;
j++;
}
}
void Insert(struct Node*L,int n){
int i=length_Node(L);
int j=0;
struct Node*p,*q;
p=L;
q=(struct Node*)malloc(sizeof(struct Node));
while(j<i){
p=p->Next;
j++;
}
q->Data=n;
q->Next=p->Next;
q->privous=p;
p->Next=q;
q->Next->privous=q;
}
int main(){
struct Node *L;
L=Creat();
srand((int)time(NULL));
int i;
for(i=0;i<10;i++){
Insert(L,rand()%100);
}
Print(L);
printf("\n");
printf("\n");
Print2(L);
return 0;
}
实验2.6随机生成10个整数存入一个循环双向链表中,然后按序输出这些整数和逆序输出这些整数。
最新推荐文章于 2022-05-13 21:38:50 发布