差不多就是这样了。
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
void
Del( Node **RootPP );
int
main( void )
{
Node *Root;
int ix;
Root = NULL;
for( ix = 0; 20 > ix; ++ix )
InsertList( &Root, ix % 3 );
PrintList( Root, ' ' );
printf( "\n" );
Del( &Root );
PrintList( Root, ' ' );
return 0;
}
void
Del( Node **RootPP )//删除链表中重复的值
{
Node *Current;
Node *This;
int t;
while( NULL != ( Current = *RootPP ) )
{
RootPP = &Current->Next;
t = Current->Value;
while( NULL != ( This = *RootPP ) )
{
if( t == This->Value )
{
*RootPP = This->Next;
free( This );
continue;
}
RootPP = &This->Next;
}
RootPP = &Current->Next;
}
}
程序代码:
//List.h
#ifndef List_H
#define List_H
typedef struct NODE {
int Value;
struct NODE *Next;
}Node;
void
PrintList( Node *RootP,char Spacech );
int
InsertList( Node **RootP, int value );
void
DeleteList( Node **RootP );
void
DeleteNode( Node **RootP, int value );
Node *
FindNode( Node *Root, int value );
Node *
ReverseList( Node *first );
void
MergeList( Node **L1, Node **L2 );
/* MergeList() 将L2链表合并到L1。L1,L2是两个递增有序的链表。
* 合并之后的链表同样是递增有序的链表。
*/
#endif
程序代码:
//List.c
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
int
InsertList( Node **RootP, int value )
{
Node *NewCell;
Node *Current;
while( NULL != ( Current = *RootP ) /*&& value > Current->Value */)
RootP = &Current->Next;
NewCell = (Node *)malloc( sizeof(Node) );
if( NULL == NewCell )
return 0;
NewCell->Value = value;
NewCell->Next = Current;
*RootP = NewCell;
return 1;
}
void
DeleteNode( Node **RootP , int value )
{
Node *Current;
while( NULL != ( Current = *RootP ) && value != Current->Value )
RootP = &Current->Next;
if( NULL == Current )
return;
*RootP = Current->Next;
free( Current );
}
void
PrintList( Node *RootP, char Spacech )
{
Node *Current;
while( NULL != ( Current = RootP ) )
{
printf("%d%c",Current->Value,Spacech);
RootP = Current->Next;
}
}
void
DeleteList( Node **RootP )
{
Node *Current;
Node *Temp;
Current = *RootP;
*RootP = NULL;
while( NULL != Current )
{
Temp = Current->Next;
free( Current );
Current = Temp;
}
}
Node *
FindNode( Node *Root, int value )
{
Node *Next;
while( NULL != ( Next = Root ) && Next->Value != value )
Root = Next->Next;
return Next;
}
Node *
ReverseList( Node *first )
{
Node *current;
Node *next;
for( current = NULL; NULL != first; first = next )
{
next = first->Next;
first->Next = current;
current = first;
}
return current;
}
void
MergeList( Node **L1, Node **L2 )
{
Node *First, *Secondary;
Node *Temp;
for( First = *L1, Secondary = *L2; NULL != First; First = *L1 )
{
for( ; NULL != Secondary && First->Value >= Secondary->Value; Secondary = Temp )
{
Temp = Secondary->Next;
*L1 = Secondary;
Secondary->Next = First;
L1 = &Secondary->Next;
}
L1 = &First->Next;
}
*L1 = Secondary;
}
[此贴子已经被作者于2017-4-23 19:16编辑过]