数据结构与算法分析3.3-3.5

来源:互联网 发布:php date 编辑:程序博客网 时间:2024/05/21 22:23

//头文件

#ifndef _List_H
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List CreatList(void);
void Insert(int i,Position p,List l);
Position findPrevious(List l,int i);
void Delete(int i,Position p);
void Printlist(List l);
int IsEmpty(List l);
int IsLast(List l,Position p);
#endif



#include <stdio.h>

#include <stdlib.h>
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node{
    int element;
    Position next;
};
List CreatList(void)
{
    List l;
    l=(List)malloc(sizeof(struct Node));
    if(l==NULL)
    {
        printf("error");
        return NULL;
    }
    l->next=NULL;
    return l;
}
void Insert(int i,Position p,List l)
{
    Position tmp;
    tmp=(Position)malloc(sizeof(struct Node));
    if(tmp==NULL)
    {
        printf("error");
        return;
    }

    tmp->element=i;    
    tmp->next=p->next;
    p->next=tmp;
}


int IsEmpty(List l){
    return l->next==NULL;

}
Position findPrevious(List l,int i)
{
    Position p;
    p=l;
    while(p->next!=NULL && p->next->element!=i)
        p=p->next;
    return p;
}

int IsLast(List l,Position p)
{
    return p->next==NULL;
}
void Delete(int i,List l)
{
    Position p=findPrevious(l,i);
    Position tmp;
    if(!IsLast(l,p))
    {
        tmp=p->next;
        p->next=tmp->next;
        free(tmp);
    }
}
void Printlist(List l)
{
    Position p=l->next;
    while(p!=NULL)
    {
        printf("%d",p->element);
        p=p->next;
    }
}
/*3.2 打印链表L中由链表P指定位置的元素PrintLots(List L,List P)*/
void PrintLots(List L,List P)
{
    Position ptrP=P->next;
    Position ptrL=L;
    int i=0,index;
    while(ptrP!=NULL)
    {
        index=ptrP->element;

        while(i<index)
        {
            ptrL=ptrL->next;
            i++;
        }
        printf("%d",ptrL->element);
        ptrP=ptrP->next;
    }
}
/*3.4 L1 L2 两个有序链表的交集
    1. 建立新链表(结果列表)储存所得交集;
    2. p指针指向结果列表头,p1,p2分别指向L1 L2头节点下一节点;
       判断两个是否都为空,都为空返回NULL;
       建立循环,循环结束条件:两个链表中有一个遍历完毕。while(p1!=NULL&&p2!=NULL)
             比较p1 p2指针所指节点的值;1)所指的值小的指针向后移动,其他不变; if(p1->element<p2->element) p1=p1->next;
                                       2)相等时则向结果列表新增节点存储这个值,
                                       两个指针p1,p2都向后移动,存结果的指针也向后移动。继续进行循环比较  
                                       Insert(p1->element,p,resultList);
                                       p1=p1->next;
                                       p2=p2->next;
                                       p=p->next;
    3. 返回结果列表

*/
List intersection(List L1,List L2)
{
    List resultList;
    Position p,p1,p2;
    resultList=CreatList();
    p=resultList;
    p1=L1->next;
    p2=L2->next;
    if(p1==NULL&&p2==NULL)
        return NULL;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->element<p2->element)
            p1=p1->next;
        else if(p2->element<p1->element)
            p2=p2->next;
        else
        {
            Insert(p1->element,p,resultList);
            
            p1=p1->next;
            p2=p2->next;
            p=p->next;
        }

    }
    return resultList;


}
/*3.4 L1 L2 两个有序链表的并集
    1. 建立新链表(结果列表)储存所得并集;
    2. p指针指向结果列表头,p1,p2分别指向L1 L2头节点下一节点;
       判断两个是否都为空,都为空返回NULL;
       建立循环,循环结束条件:两个链表中有一个遍历完毕。while(p1!=NULL&&p2!=NULL)
            比较p1 p2指针所指节点的值;1)所指的值小的指针向后移动,向结果列表新增节点存储这个值,结果的指针也向后移动,指向较大值的指针不变;
                                      2)相等时则向结果列表新增节点存储这个值,两个指针p1,p2都向后移动,存结果的指针也向后移动。继续进行循环比较  
    3. 判断两个链表是否都遍历完毕,没有遍历完毕的,将剩下的值放到结过链表中。

*/
List Union(List L1,List L2)
{
    Position p,p1,p2;
    List resultList;
    resultList=CreatList();
    p1=L1->next;
    
    p2=L2->next;
    
    p=resultList;
    if(p1==NULL&&p2==NULL)
        return NULL;
    while(p1!=NULL&&p2!=NULL)
    {
        if(p1->element<p2->element)
        {
            
            Insert(p1->element,p,resultList);    
            
            p1=p1->next;
            p=p->next;
        }
            
        else if(p1->element>p2->element)
        {
            
            Insert(p2->element,p,resultList);
            
            p2=p2->next;
            p=p->next;
            
        }
        else
        {
        
            Insert(p1->element,p,resultList);    
            
            p1=p1->next;
            p2=p2->next;
            p=p->next;

        }

    }
    if (p1!=NULL)
    {
        while(p1!=NULL)
        {
            
            Insert(p1->element,p,resultList);    
            p1=p1->next;
            p=p->next;
        }
    
    }
    else if(p2!=NULL)
    {
        while(p2!=NULL)
            
        {
            
            Insert(p2->element,p,resultList);    
            p2=p2->next;
            p=p->next;
        }
    
    }

    return resultList;



}

void main()
{
    List L1,L2,resultList;
    Position p1,p2;
    L1=CreatList();
    L2=CreatList();

    p1=L1;
    p2=L2;
    
    Insert(1,p1,L1);
    Insert(2,p1->next,L1);
    Insert(3,p1->next->next,L1);
    Insert(4,p1->next->next->next,L1);
    Insert(5,p1->next->next->next->next,L1);
    Insert(1,p2,L2);
    Insert(3,p2->next,L2);
    Insert(4,p2->next->next,L2);


    resultList=Union(L1,L2);

    Printlist(resultList);
    

}
原创粉丝点击