单链表创建、排序、合并

5 篇文章 0 订阅
1 篇文章 0 订阅

单链表操作代码:

list.h

#ifndef _List_H
#define  ElementType int
struct node;
typedef struct node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

List MakeEmpty( List L );
void CreatList( List L );
int IsEmpty( List L );
int IsLast( Position P, List L );
Position Find( ElementType X, List L );
void Delete( ElementType X, List L );
Position FindPrevious( ElementType X, List L );
void Insert( ElementType X, List L, Position P);
void DeleteList( List L );
void Print( List L );
int Length( List L );
Position Header( List L );
Position First( List L );
Position Advance( Position P );
void SortList( List L );
Position CombineList( List L1, List L2 );

#endif

list.c

#include "link.h"
#include <malloc.h>
#include <stdio.h>

struct node
{
    ElementType Element;
    Position Next;
};

void
CreatList( List L )
{
    Position TmpCell, P;
    P = L;
    ElementType X;
    scanf("%d", &X);
    while( X != 0 )
    {
        TmpCell = malloc( sizeof(struct node) );
        TmpCell->Element = X;
        TmpCell->Next = P->Next;
        P->Next = TmpCell;
        scanf("%d", &X);
    }
}

int 
IsEmpty( List L )
{
    return ( L->Next == NULL );
}

int 
IsLast( Position P, List L )
{
    return P->Next == NULL;
}

Position
Find( ElementType X, List L )
{
    Position P;
    P = L->Next;

    while( P != NULL && P->Element != X )
        P = P->Next;
    return P;
}

void 
Delete( ElementType X, List L )
{
    Position Pre, Cur;
    Pre = L;
    Cur = L->Next;
    
    while( Cur != NULL && Cur->Element != X )
    {
        Pre = Cur;
        Cur = Cur->Next;
    }
    Pre->Next = Cur->Next;
    free(Cur);
}

Position 
FindPrevious( ElementType X, List L )
{
    Position Pre, Cur;
    Pre = L;
    Cur = L->Next;
    while( Cur != NULL && Cur->Element != X )
    {
        Pre = Cur;
        Cur = Cur->Next;
    }
    return Pre;
}

//insert into the position after P
void 
Insert( ElementType X, List L, Position P )
{
    Position TmpCell;
    TmpCell = malloc( sizeof(struct node) );

    if( TmpCell == NULL )
      //  FataError( "Out of space !!" );
        return ;

    TmpCell->Element = X;
    TmpCell->Next = P->Next;
    P->Next = TmpCell;
}

void 
Print( List L )
{
    Position P;
    P = L;

    while( P->Next != NULL )
    {
        printf("%d\t", P->Next->Element);
        P = P->Next;
    }
    printf("\n\n");
}

void 
DeleteList( List L )
{
    Position Nex;
    Position Cur;
    Cur = L->Next;
    L->Next = NULL;
    while( Cur != NULL )
    {
        Nex = Cur->Next;
        free(Cur);
        Cur = Nex;
    }
}

int Length( List L )
{
    Position Header;
    Header = L;
    int num = 0;
    while( Header->Next != NULL )
    { 
        num++;
        Header = Header->Next;
    }
    return num;
}

void
SortList( List L )
{
    int length = Length(L);
    Position Header, p, v, r, r_pre;
    Header = L;
    p = Header->Next->Next;
    if( IsEmpty(L) || p == NULL ) return;

    int i;
    Header->Next->Next = NULL;
    for( i = 1; i < length; i++ )
    {
        v = p;
        p = p->Next;
        r = Header->Next;
        r_pre = Header;
        while( ( r != NULL ) && ( v->Element > r->Element ) )
        {
            r_pre = r;
            r = r->Next;
        }
        v->Next = r;
        r_pre->Next = v;
    }
    
}

Position 
CombineList( List L1, List L2 )
{
    Position P1, P1_Pre;
    Position P2, TmpCell;

    int i, length;

    length = Length(L2);
    if( IsEmpty( L1 ) || IsEmpty( L2 ) )
        return;
    P2 = L2->Next;
    P1 = L1->Next;
    P1_Pre = L1;
    for( i = 0; i < length; i++ )
    {
        while( ( P1 != NULL ) && ( P1->Element < P2->Element ) )
        {
            P1_Pre = P1;
            P1 = P1->Next;
        }

        TmpCell = P2;
        P2 = P2->Next;
        
        TmpCell->Next = P1;
        P1_Pre->Next = TmpCell;
        P1_Pre = P1_Pre->Next;
    }       
    L2->Next = NULL;
  //  free(L2);
}


int main()
{
    struct node Header1,Header2;
    Position TmpCell;
    Header1.Next = NULL;
    Header2.Next = NULL;
    List L1, L2;

    L1 = &Header1;
    L2 = &Header2;

    CreatList( L1 );
    CreatList( L2 );

    Print( L1 );
    printf("Length L1: %d\n", Length(L1));
    Print( L2 );
    printf("Length L2: %d\n", Length(L2));

    printf("After sort: \n");
    SortList( L1 );
    SortList( L2 );
    Print( L1 );
    Print( L2 );
  
    printf("After combine:\n");
    TmpCell = CombineList(L1, L2);

    Print( L1 );

    DeleteList(L1);
    DeleteList(L2);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值