程序代码:
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
// 单链表
struct node{
int data;
struct node *next;
};
// 双向链表
struct node2{
int data;
struct node2 *prev;
struct node2 *next;
};
// 初始化单链表
struct node* initLink(int arr[], int len){
if(len<1)return NULL;
struct node *h = (struct node*)malloc(sizeof(struct node));
h->data=arr[0];
h->next=NULL;
struct node *p=h;
for(int i=1;i<len;i++){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data=arr[i];
newNode->next=NULL;
p->next=newNode;//尾插
p=p->next;
}
return h;
}
// 遍历单链表
void display(struct node *h){
printf("遍历单链表:\n");
int cols=0;
while(h!=NULL){
printf("%d\t", h->data);
h=h->next;
printf(++cols%10==0?"\n":"");
}
printf("\n");
}
struct node* deleteNum(struct node*h,int mark_num){
struct node *p=h->next;
while(h!=NULL&&h->data%mark_num==0){// 开头被整除
struct node *q=h->next;
free(h);//删除节点
h=q;
}
while(p!=NULL){
if(p==NULL)break;
if(p->next!=NULL){
if(p->next->data%mark_num==0){//中间被整除
struct node *q=p->next->next;
free(p->next);//删除节点
p->next=q;
}
}
p=p->next;
}
return h;
}
// 单向转双向
struct node2* trans(struct node* h){
struct node2 *h2 = (struct node2*)malloc(sizeof(struct node2));
h2->prev=NULL;
h2->next=NULL;
struct node2 *p=h2;
if(h==NULL){
return h2;
}
if(h!=NULL){
struct node2 *newNode = (struct node2*)malloc(sizeof(struct node2));
p->data=h->data;
h=h->next;
}
while(h!=NULL){
struct node2 *newNode = (struct node2*)malloc(sizeof(struct node2));
newNode->data=h->data;
newNode->next=NULL;
newNode->prev=NULL;
p->next=newNode;
p=p->next;
h=h->next;
}
p=h2;
while(p->next!=NULL){
p->next->prev=p;
p=p->next;
}
return h2;
}
// 遍历单链表
void display2(struct node2 *h){
printf("遍历单链表:\n");
int cols=10;
while(h->next!=NULL){
printf("%d\t", h->data);
h=h->next;
printf(++cols%10==0?"\n":"");
}
printf("%d\t", h->data);
printf("\nback---\n");
cols=0;
while(h!=NULL){
printf("%d\t", h->data);
h=h->prev;
printf(++cols%10==0?"\n":"");
}
printf("\n");
}
int main() {
int len=100;
int arr[len];
printf("生产%d个随机数:\n",len);
srand((unsigned)time(NULL));
for(int i=0;i<len;i++){
arr[i] = rand() % 100;
printf("%d\t", arr[i]);
}
printf("\n");
struct node *h = initLink(arr,len);
display(h);
int mark_num=3;
h=deleteNum(h,mark_num);
printf("删除被%d整除的数后:",mark_num);
display(h);
// 双向链表创建
struct node2*h2 = trans(h);
printf("双向链表遍历");
display2(h2);
printf("释放");
while(h!=NULL){
struct node*p=h;
h=h->next;
free(p);
}
while(h2!=NULL){
struct node2*p=h2;
h2=h2->next;
free(p);
}
return 0;
}
生产100个随机数:
1 11 11 31 22 50 67 47 70 74 11 34 95 41 41 63 81 23 93 86 61 90 67 74 58 60 84 94 74 23 48 27 34 59 11 9 61 78 8 31 4 19 18 99 60 59 62 93 34 7 31 95 97 99 70 7 59 6 1 85 30 1 64 16 60 75 77 74 5 85 57 9 56 75 60 68 87 74 61 21 81 93 17 30 92 39 37 3 45 90 88 27 91 52 44 52 80 73 78 37
遍历单链表:
1 11 11 31 22 50 67 47 70 74
11 34 95 41 41 63 81 23 93 86
61 90 67 74 58 60 84 94 74 23
48 27 34 59 11 9 61 78 8 31
4 19 18 99 60 59 62 93 34 7
31 95 97 99 70 7 59 6 1 85
30 1 64 16 60 75 77 74 5 85
57 9 56 75 60 68 87 74 61 21
81 93 17 30 92 39 37 3 45 90
88 27 91 52 44 52 80 73 78 37
删除被3整除的数后:遍历单链表:
1 11 11 31 22 50 67 47 70 74
11 34 95 41 41 81 23 86 61 67
74 58 84 94 74 23 27 34 59 11
61 8 31 4 19 99 59 62 34 7
31 95 97 70 7 59 1 85 1 64
16 75 77 74 5 85 9 56 60 68
74 61 81 17 92 37 45 88 91 52
44 52 80 73 37
双向链表遍历遍历单链表:
1 11 11 31 22 50 67 47 70 74
11 34 95 41 41 81 23 86 61 67
74 58 84 94 74 23 27 34 59 11
61 8 31 4 19 99 59 62 34 7
31 95 97 70 7 59 1 85 1 64
16 75 77 74 5 85 9 56 60 68
74 61 81 17 92 37 45 88 91 52
44 52 80 73 37
back---
37 73 80 52 44 52 91 88 45 37
92 17 81 61 74 68 60 56 9 85
5 74 77 75 16 64 1 85 1 59
7 70 97 95 31 7 34 62 59 99
19 4 31 8 61 11 59 34 27 23
74 94 84 58 74 67 61 86 23 81
41 41 95 34 11 74 70 47 67 50
22 31 11 11 1
释放