有序表操作

void listinsert(LNODE *h,ElemType e)//有序单链表的插入算法
{
    LNODE *res = h,*q;
    while(res->next&&res->next->node<e)
        res = res->next;
    q=malloc(sizeof(LNODE));
    q->node = e;
    q->next = res->next;
    res->next = q;
}




void listinsert(SqList *l,ElemType e)//有序顺序表的插入算法
{
    int i=0,j;
    while (i<l->length&&l->data[i]<e)
    i++;
    for(j=l->length;j>i;j--)
    {
    l->data[i]=l->data[j-1];
    l->data[i]=e;
    l->length++;
}


void unionlist(LNODE *h1,LNODE *h2,LNODE *h)
//有序表的归并算法,采用单链表存放。就是合并连个单链表放在h中。
{
    LNODE *res1 = h1->next;
    LNODE *res2 = h2->next;
    LNODE *s;
    LNODE *q = h;
    while(res1&&res2)
    {
        if(res1->node>res2->node)
        {
            s = malloc(sizeof(LNODE));
            s->node = res2->node;
            q->next = s;
            q = s;
            res2 = res2->next;
        }
        else
        {
            s = malloc(sizeof(LNODE));
            s->node = res1->node;
            q->next = s;
            q = s;
            res1 = res1->next;
        }
    }
    while(res1)
    {
            s = malloc(sizeof(LNODE));
            s->node = res1->node;
            q->next = s;
            q = s;
            res1 = res1->next;
    }
    while(res2)
    {
            s = malloc(sizeof(LNODE));
            s->node = res2->node;
            q->next = s;
            q = s;
            res2 = res2->next;

    }
    q->next = NULL;
}


//把三个有序单链表的公共元素放到h1中,无用节点全都删掉。
//核心思想:运用尾插法。
void commend(LNODE *h1,LNODE *h2,LNODE *h3)
{
    LNODE *res = h1;
    LNODE *q1 = h1->next,*q;
    LNODE *q2 = h2->next;
    LNODE *q3 = h3->next;
    while(q1)
    {
        while(q2&&q2->node<q1->node)
            q2 = q2->next;
        while(q3&&q3->node<q1->node)
            q3=q3->next;
        if(q2 && q3 && q2->node==q1->node && q3->node==q1->node)
        {
            res->next = q1;
            res = q1;
            q1 = q1->next;
        }
        else
        {
            q = q1;
            q1 = q1->next;
            free(q);
        }
    }
    res->next = NULL;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值