链式有序表的合并

又接着看了链式有序表的合并๑乛◡乛๑



head.h头文件——包含了 各种声明还有结构体定义

#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED


#define ElemType int
#define Status int


typedef struct Node
{
    ElemType data;
    struct Node *next;
}Node,*pNode;


Status InitList(pNode &h);    //初始化链表
Status GetElem(pNode L,int i,ElemType e);    //取值
Node *LocateElem(pNode L,ElemType e);        //查找
Status ListInsert(pNode L,int i,ElemType e); //插入
Status ListDelete(pNode L,int i);            //删除
void output(pNode h);                        //打印
pNode creat_h(int n);                        //创建
pNode creat_r(int n);                        //创建


#endif // HEAD_H_INCLUDED

合并功能函数(单独拿出来的啦๑乛◡乛๑)

void MergeList(pNode &A,pNode &B,pNode &C)
{
    pNode pa,pb,pc;
    pa=A->next;pb=B->next;   //pa和pb的初值分别指向两个表的第一个结点
    C=A;                     //用A的头结点作为C的头结点
    pc=C;                    //pc指向C的头结点
    while(pa&&pb)
    {//A和B均未达到表尾,则依次“摘取”两表中较小的结点插入到C的最后
        if(pa->data <= pb->data)
        {
            pc->next=pa;
            pc=pc->next;
            pa=pa->next;
        }
        else
        {
            pc->next=pb;
            pc=pc->next;
            pb=pb->next;
        }
    }
    pc->next=pa? pa:pb;      //将非空表的剩余段插入到pc所指结点之后
    delete B;
}

main.c文件

#include <iostream>
#include "head.h"

using namespace std;


void MergeList(pNode &A,pNode &B,pNode &C);

int main()
{
    int n;
    pNode A,B,C;
    cout<<"input n data of A:";
    cin>>n;
    A=creat_r(n);
    cout<<"input n data of B:";
    cin>>n;
    B=creat_r(n);
    InitList(C);
    MergeList(A,B,C);
    output(C);
    return 0;
}

void MergeList(pNode &A,pNode &B,pNode &C)
{
    pNode pa,pb,pc;
    pa=A->next;pb=B->next;
    C=A;
    pc=C;
    while(pa&&pb)
    {
        if(pa->data <= pb->data)
        {
            pc->next=pa;
            pc=pc->next;
            pa=pa->next;
        }
        else
        {
            pc->next=pb;
            pc=pc->next;
            pb=pb->next;
        }
    }
    pc->next=pa? pa:pb;
    delete B;
}

Status InitList(pNode &h)  //初始化
{
    h=new Node;
    h->next=NULL;
    return 1;
}

Status GetElem(pNode L,int i,ElemType e)  //取值
{
    pNode p=L->next;
    int j=1;
    while(p&&j<i)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i)
        return -1;
    e=p->data;
    return 1;
}

Node *LocateElem(pNode L,ElemType e)   //查找
{
    pNode p=L->next;
    while(p&&p->data!=e)
        p=p->next;
    return p;
}

Status ListInsert(pNode L,int i,ElemType e)  //插入
{
    pNode p=L;
    int j=0;
    while(p&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(!p||j>i-1)  return -1;
    pNode q=new Node;
    q->data=e;
    q->next=p->next;
    p->next=q;
    return 1;

}

Status ListDelete(pNode L,int i)       //删除
{
    pNode p=L;
    int j=0;
    while(p->next&&j<i-1)
    {
        p=p->next;
        j++;
    }
    if(!p->next||j>i-1)  return -1;
    pNode q=p->next;
    p->next=p->next->next;
    delete q;
    return 1;

}

pNode creat_h(int n)     //创建-头插法
{
    pNode L;
    L=new Node;
    L->next=NULL;
    for(int i=0;i<n;i++)
    {
        pNode p=new Node;
        cin>>p->data;
        p->next=L->next;
        L->next=p;
    }
    return L;
}

pNode creat_r(int n)      //创建-尾插法
{
    pNode h,q,p;
    h=q=new Node;
    h->next=NULL;
    for(int i=0;i<n;i++)
    {
        p=new Node;
        p->next=NULL;
        cin>>p->data;
        q->next=p;
        q=q->next;
    }
    return h;
}

void output(pNode h)  //打印链表
{
    pNode p=h->next;
    cout<<"打印链表:";
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}



posted @ 2017-09-21 20:18  詹晔晔(๑>؂<๑)  阅读(583)  评论(0编辑  收藏  举报