链表

链表的排序(带头结点)

PolyList SortList(PolyList head1){
       int minsize;
       PolyList head2,p,rear,minp,temp,q;
       head2=(PolyList)malloc(sizeof(PolyNode));
       temp=(PolyList)malloc(sizeof(PolyNode));
       p=head2;
       rear=head1;

       while(head1->next!=NULL){
            q=head1;
           for(rear=q,minsize=q->next->exp;rear->next!=NULL;rear=rear->next){
             if(rear->next->exp<=minsize){
                minp=rear;
                temp=rear->next;
                minsize=temp->exp;
              }
            }    
             minp->next=temp->next;
             p->next=temp;
             p=temp;
       }
       p->next=NULL;
       return head2;

}

 

链表(带头节点)多项式相加(原链表成升序排列)

PolyList add(PolyList head1,PolyList head2)
{
    PolyNode *p,*q,*tail,*temp;
    float sum;
    p=head1->next;
    q=head2->next;
    tail=head1;
    while(p!=NULL&&q!=NULL)
    {
        if(p->exp>q->exp)
        {
            tail->next=p;
            tail=p;
            p=p->next;
        }
        else if(p->exp==q->exp)
        {
            sum=p->coef+q->coef;
            if(sum!=0)
            {
                p->coef=sum;
                tail->next=p;
                tail=p;
                p=p->next;
                temp=q;
                q=q->next;
                free(temp);
            }
            else
            {
                temp=p;p=p->next;free(temp);
                temp=q;q=q->next;free(temp);
            }
        }
        else
        {
            tail->next=q;
            tail=q;
            q=q->next;
        }
    }
    if(p!=NULL)
        tail->next=p;
    else
        tail->next=q;

    return head1;
}

 

链表倒序(创建了一个新的链表 采用不带头结点的头插法)

PolyList turn(PolyList head3){

     PolyList head4,rear,p,temp;
     head4=(PolyList)malloc(sizeof(PolyNode));
     head4=NULL;
     p=head3->next;

     while(p!=NULL){
        temp=p;
        p=p->next;
        if (head4 == NULL)
        {
            head4 = temp;
            head4->next = NULL;
        }
        else
        {
            temp->next = head4;
            head4 = temp;
        }

     }
   return head4;

}

 

 链表倒序(创建了一个新的链表 带头结点的头插法)

ListNode turn(ListNode head3){

     ListNode head4,rear,p,temp;
     head4=(ListNode)malloc(sizeof(ListNode));
     head4->next=NULL;
     p=head3->next;
     rear=head4;
     while(p!=NULL){
        temp=p;
        p=p->next;
        temp->next=rear->next;
        rear->next=temp;
      
     }
     return head4;
}

 

posted @ 2019-03-07 18:59  像走了一光年  阅读(174)  评论(0编辑  收藏  举报