救救孩子  线性表的链式储存
											1)    用C语言编写程序LinkList.cpp实现单链表的各种基本运算。2) 初始化单链表L,依次从表尾插入100个整数,并按插入顺序输出这100个整数;
3) 删除单链表L中能被3整除的元素,并把剩余元素依次输出;
4) 简单修改程序,实现双向循环链表存储结构
 程序代码:
程序代码:#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;
}
