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;
}
有序表操作
最新推荐文章于 2024-04-12 14:08:53 发布