单链表操作代码:
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;
}