线性表之单链表算法

来源:互联网 发布:linux c syslog 编辑:程序博客网 时间:2024/06/05 07:47
#include<stdio.h>#include<stdlib.h>typedef int datatype;typedef struct node{    int data;    struct node *next;}linklist;typedef linklist *linknode;//置空表linknode init(){    linknode head;    head=(linknode*)malloc(sizeof(linklist));    head->next= NULL;    return head; } //头插法建立链表 linknode creat1(datatype a[],int n){    linknode head;    linknode s;    int i;    head=(linknode*)malloc(sizeof(linklist));    head->next=NULL;    for(i=0;i<n;i++)    {        s=(linknode*)malloc(sizeof(linklist));        s->data=a[i];        s->next=head->next;        head->next=s;    }    return head;}//尾插法建立链表linknode creat2(datatype a[],int n){    linknode head,s,r;    int i;    head=(linknode*)malloc(sizeof(linklist));    r=head;    for(i=0;i<n;i++)    {        s=(linknode*)malloc(sizeof(linklist));        s->data=a[i];        r->next=s;        r=s;    }    r->next=NULL;    return head;}linknode creat3(){    linknode head;    linknode s,r;    head=r=(linknode*)malloc(sizeof(linklist));    int x;    printf("请输入一组以0结尾数:\n");    scanf("%d",&x);    while(x)    {        s=(linknode*)malloc(sizeof(linklist));        s->data=x;        r->next=s;        r=s;        scanf("%d",&x);    }    r->next=NULL;    return head;}//输出单链表中结点值 void print(linknode head){    linknode p=head->next;    while(p!=NULL)    {        printf("%d ",p->data);        p=p->next;    }}//在链表中查找元素linknode search(linknode head,datatype x){    linknode p=head->next;    while(p!=NULL&&p->data!=x)    {        p=p->next;    }    if(p==NULL)    {        printf("没找到!");        return NULL;    }    return p; } //有序单链表的归并算法void merge(linknode s1,linknode s2,linknode s3){    linknode p1,p2,r,s;    p1=s1->next;    p2=s2->next;    s3=(linknode*)malloc(sizeof(linklist));    r=s3;    while(p1!=NULL&p2!=NULL)    {        if(p1->data<p2->data)        {            s=(linknode*)malloc(sizeof(linklist));            s->data=p1->data;            p1=p1->next;            r->next=s;            r=s;        }        if(p1->data>p2->data)        {            s=(linknode*)malloc(sizeof(linklist));            s->data=p2->data;            p2=p2->next;            r->next=s;            r=s;        }    }    if(p2!=NULL)    {        p1=p2;    }    while(p1)    {        s=(linknode*)malloc(sizeof(linklist));        s->data=p1->data;        p1=p1->next;        r->next=s;        r=s;    }    r->next=NULL; } //删除值为x的结点int deletex(linknode head,datatype x){    linknode pre=head,p=pre->next;    while(p&&p->data!=x)    {        pre=p;        p=p->next;    }    if(p!=NULL)    {        pre->next=p->next;        free(p);        return 1;    }    else    {        return 0;    } } //删除值为x的结点的前驱结点int delete_prex(linknode head,datatype x){    linknode prepre,pre,p;    prepre=head;    p=pre->next;    pre=prepre->next;    if(pre->data==0)    {        return 0;    }    while(p!=NULL&&p->data!=x)    {        prepre=pre;        pre=p;        p=p->next;    }    if(p!=NULL)    {        prepre->next=p;        free(pre);        return 1;    }    return 0;}//判断单链表是否递增(与前驱结点比较)int increase(linknode head){    linknode pre=head->next,p=pre->next;     while(p!=NULL)    {        if(p->data>=pre->data)        {            pre=p;            p=p->next;        }        else        {            return 0;        }    }    return 1; } //删除链表中最小值的结点void delete_min(linknode head){    linknode pre=head,p=pre->next,min=p,minpre=pre;    while(p!=NULL)    {        if(p->data<min->data)        {            min=p;            minpre=pre;        }        pre=p;        p=p->next;    }    minpre->next=min->next;    free(min); } //删除释放链表中所有结点void delete_all(linknode head){    linknode pre=head,p=pre->next;    while(p)    {        free(pre);        pre=p;        p=p->next;    }    free(pre);    printf("NULL"); } //将一个线性表就地逆置void reverse(linknode head){    linknode p=head->next,q;    head->next=NULL;    while(p)    {        q=p->next;        head->next=p->next;        head->next=p;        q=p;    } } //将一个链表A拆散成两个链表A,B,一个存偶数,A,一个存奇数,Bvoid split(linknode s1,linknode s2) {    linknode p=s1->next,ra,rb;    s2=(linknode*)malloc(sizeof(linklist));    ra=s1;    rb=s2;    while(p)    {        if(p->data%2==0)        {            ra->next=p;            ra=p;            p=p->next;        }        if(p->data%2!=0)        {            rb->next=p;            rb=p;            p=p->next;        }    }    ra->next=rb->next;}//删除链表中奇数号结点,135……void deleteodd(linknode head){    linknode pre=head,p=pre->next;    while(p)    {        pre->next=p->next;        free(p);        pre=pre->next;        if(pre==NULL)        {            break;        }        p=pre->next;    } } //在有序链表中插入节点后仍递增有序void inorderlist(linknode head,datatype x){    linknode s,pre,p;    pre=head;    p=pre->next;    s=(linknode*)malloc(sizeof(linklist));    s->data=x;    s->next=NULL;    while(p&&p->data<x)    {        pre=p;        p=p->next;    }    s->next=p->next;    pre->next=s; } //设计一个算法使链表递增有序void sort(linknode head){    linknode p=head->next,pre,r;    r=p->next;//r保存p结点后继节点     p->next=NULL;    p=r;    while(p)    {        r=p->next;        pre=head;        while(pre->next!=NULL&pre->next->data<p->data)        {            pre=pre->next;//在有序表中找到p的前驱结点pre         }        p->next=pre->next;//在pre后插入p结点         pre->next=p;        p=r;    } }  //在一个单链表中删除重复的结点 void deleteSame(linknode head) {    linknode p=head->next,postq,t,q;    while(p)    {        q=p;//q指向P的后继节点         postq=q->next;//postq指向q的后继节点         while(postq)//查找是否有与p重复的结点         {            if(postq->data==p->data)//postq是重复的结点             {                t=postq->next;//t临时指向postq的后继结点                 q->next=t;//删除postq结点                 free(postq);//释放postq结点的空间                 postq=t;//让postq指向下一个结点              }             else//不是重复的结点则下移              {                q=postq;                postq=postq->next;             }         }         p=p->next;//p下移      }  }  //设计算法求集合的交集void interset(linknode A,linknode B,linknode C){    linknode pa=A->next,pb,s,r;    C=(linknode*)malloc(sizeof(linklist));    r=C;    while(pa!=NULL)    {        pb=B->next;        while(pb!=NULL&&pb->data!=pa->data)        {            pb=pb->next;        }        if(pb)        {            s=(linknode*)malloc(sizeof(linklist));            s->data=pa->data;            r->next=s;//s插入到C中             r=s;        }        pa=pa->next;    }    r->next=NULL;}int main(){//测试算法/*          int a[5]={1,3,5,3,4};    linknode head1,head2;    printf("头插法建立链表:");    head1=creat1(a,5);    print(head1);    printf("\n");    printf("尾插法建立链表:");    head2=creat2(a,5);    print(head2);    printf("\n输入数据建立链表:\n");    linknode head3;    head3=creat3();    print(head3);    printf("\n查找元素3:\n");    linknode p1;    p1=search(head1,3);    printf("%d ",p1->data);    printf("\n查找元素10:\n");    linknode p2;    p2=search(head1,10);    printf("%d ",p2->data);    printf("\n有序链表的归并算法:\n");    linknode s1,s2,s3;    s1=creat3();    s2=creat3();    merge(s1,s2,s3);    print(s3);    linknode head;    head=creat3();    print(head);    delete3(head,5);    printf("\n删除后:");    print(head);    linknode head;    head=creat3();    print(head);    delete_prex(head,5);    printf("\n");    print(head);    linknode head;    int a[5]={1,2,3,4,5};    head=creat2(a,5);    int n=increase(head);    printf("%d ",n);    linknode head;    head=creat3();    delete_min(head);    print(head);    linknode head;    head=creat3();    delete_all(head);    return 0;    */    //避免麻烦所以在测试函数中不需要的注释掉就OK,只保留需要的}