链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现

来源:互联网 发布:第三代火影 知乎 编辑:程序博客网 时间:2024/05/19 12:40

演示图例1
这里写图片描述
演示图例2
这里写图片描述
演示图例3
这里写图片描述

// 链表基本操作.cpp : 定义控制台应用程序的入口点。//// 链表基本操作.cpp : 定义控制台应用程序的入口点。//#include<stdio.h> #include<malloc.h> #include<stdlib.h>typedef struct Lnode //定义链表结点的数据结构 {    int data;    struct Lnode *next;}node;node *headinsert()//头插法创建链表 {    int x;    node *h = (node *)malloc(sizeof(node));    h->next = NULL;    scanf("%d", &x);    while (x != -1)    {        node *s = (node *)malloc(sizeof(node));        s->data = x;        s->next = h->next;        h->next = s;        scanf("%d", &x);    }    return h;}node *tailinsert()//尾插法创建链表 {        int x;    node *h, *t;    t = h = (node *)malloc(sizeof(node));    h->next = NULL;    scanf("%d", &x);    while (x != -1)    {        node *s = (node *)malloc(sizeof(node));        s->data = x;        s->next = NULL;        t->next = s;        t = s;        scanf("%d", &x);    }    return h;}node *del(node *h, int n)//删除指定元素的结点 {    node *p = h;    node *q;        while (p->next!= NULL)        if((p->next)->data == n)        {            q = p->next;            p->next=q->next;            free(q);        //  printf("删除3");        }        else            p = p->next;    return h;}node *insert(node *h, int n, int m, int b) {    node *p;    if (h == NULL)        return h;    p = h->next;    if (b == 0)    {        if (p->data == n)        {            node *tem = (node *)malloc(sizeof(node));            tem->data = m;            tem->next = p;            h->next = tem;        }        while (p->next != NULL)        {            if ((p->next->data) == n)            {                node *tem = (node *)malloc(sizeof(node));                tem->data = m;                tem->next = p->next;                p->next = tem;                p = p->next;            }            p = p->next;        }    }    else    {        if ((h->next->data == n))        {            node *tem = (node *)malloc(sizeof(node));            tem->data = m;            tem->next = h->next->next;            h->next->next = tem;            p=p->next;        }        while (p!= NULL)        {            if (p->data == n)            {                node *tem = (node *)malloc(sizeof(node));                tem->data = m;                tem->next = p->next;                p->next = tem;            }            p = p->next;        }    }    return h;}int select(node *h, int n)//查找指定元素在链表中的第一次出现的位置 {    int i = 1;    while (h->data != n)    {        if (h->next == NULL)            return 0;        h = h->next;        i++;    }    return i - 1;}int getlength(node *h){    node *p = h;    int i = 0;    while (p->next != NULL)    {        p = p->next;        i++;    }    return i;}node *get(node *h, int n)//获取链表指定位置的结点指针 {    node *p = h;    int i;    if (n>getlength(h) || n <= 0)        return h;    for(i = 1;i <= n;i++)        p = p->next;    return p;}void print(node *h)//打印链表的所有节点 {    node *p = h->next;    if (h == NULL)        return;    do    {        printf("%d  ", p->data);        p = p->next;    } while (p != NULL);    printf("\n");}void Reverse(node *h)//将原链表就地倒置 {    node *p = h->next;    node *q;    h->next = NULL;    while (p != NULL)    {        q = p;        p = p->next;        q->next = h->next;        h->next = q;    }}void pur_linklist(node *h)//删除链表中的所有重复元素 {    node *p = h->next;    if (!p || !(p->next))        return;    while (p!= NULL)    {        node *q = p;        node *tem;        while (q->next != NULL)        {            if (q->next->data == p->data)            {                tem = q->next;                q->next = q->next->next;                free(tem);            }            else q = q->next;        }        p = p->next;    }}void difference(node *A, node *B)//两个链表构成的集合求差运算,即A-B {    node *q=B;    node *p;    node *pre,*r;     pre=A;    p=A->next;    if (!p)        return ;    while (p)    {            //printf("进入3");        q=B->next;         while(q!=NULL&&p->data!=q->data)        {        //  printf("进入1");            q=q->next;        }        if(q!=NULL)        {        //  printf("进入2");            r=p;            pre->next=p->next;            p=p->next;            free(r);        }        else        {            pre=p;            p=p->next;        }    }}void bubblesort1(node *h)//冒泡排序交换结点{    int w,i,change,j;    node *p1,*p2,*p3;    for(i=getlength(h),change =1;i>1&&change;i--)    {        change=0;        for( j=1;j<i;++j)             if(get(h,j)->data>get(h,j+1)->data)            {            p1=get(h,j-1);            p2=get(h,j);            p3=get(h,j+1);            p2->next=p3->next;            p3->next=p2;            p1->next=p3;            change=1;            }    }}void bubblesort2(node *h)//冒泡排序交换结点值{    int w,i,change,j;    node *tem;    for(i=getlength(h),change =1;i>1&&change;i--)    {        change=0;        for( j=1;j<i;++j)            if(get(h,j)->data>get(h,j+1)->data)            {                w=get(h,j)->data;                get(h,j)->data=get(h,j+1)->data;                get(h,j+1)->data=w;                change=1;            }    }}node  *linkcombine(node *A,node *B)//将有序数组B合并到有序数组A,并使A有序{    node *p=A;node *q=B->next;node *prep=A;    node *preq=B;node *tem;    if(p==NULL)         return B;    if(q==NULL)      return B;    while(q!=NULL)    {        printf("当前A:\n");        print(A);          while(p->next!=NULL&&p->next->data<=q->data)//此处尤其注意两个条件的先后顺序,否则p->next->data因为无值程序崩溃        {            p=p->next;        }        tem=q;        q=q->next;        tem->next=p->next;        p->next=tem;        p=p->next;                      }} int  main(void)//测试 {    node *h1, *h2;    printf("h1为头插法");    h1 = headinsert();    //printf("%d %d %d",h1->next->data,h1->next->next->data,h1->next->next->next->data);    printf("h2为尾插法");    //  printf("%d",h1->next->next->data);    h2 = tailinsert();    printf("h1:\n");    print(h1);    printf("h2:\n");    print(h2);    printf("3在h1的%d号位置,3在h2的%d号位置\n", select(h1, 3), select(h2, 3));    h1 = del(h1, 3);    h2 = del(h2, 3);    printf("删除3以后h1:\n");    print(h1);    printf("删除3以后h2:\n");    print(h2);    printf("在h1的4前添加10,在h2的4后添加10:\n");    h1 = insert(h1, 4, 10, 0);    printf("h1:\n");    print(h1);    h2 = insert(h2, 4, 10, 1);    printf("h2:\n");    print(h2);    printf("将h1就地倒置:\n");    Reverse(h1);    print(h1);    printf("删除h2中的所有重复元素后:\n");    pur_linklist(h2);    print(h2);    printf("求集合运算h1-h2后的h1:\n");    difference(h1, h2);    print(h1);    printf("h1交换结点冒泡排序后:\n");    bubblesort1(h1);    print(h1);    printf("h1交换结点值冒泡排序后:\n");    bubblesort2(h1);    print(h1);    printf("h2交换结点值冒泡排序后:\n");    bubblesort2(h2);    print(h2);    printf("h1与h2合并后:\n");    linkcombine(h1,h2);    print(h1);}
阅读全文
0 0
原创粉丝点击