#define FALSE 0
#define TRUE 1
typedef struct NODE{
struct NODE *link;
int value;
}NODE;
int length( NODE *head )
{
NODE *current;
int length = 0;
current = head;
while( current != NULL )
{
current = current->link;
length++;
}
return length;
}
NODE *create()
{
NODE *head, *p, *s;
int value, cycle = 1;
head = (NODE *)malloc( sizeof(NODE) );
p = head;
while( cycle )
{
printf( "/nPlease input the data:" );
scanf( "%d", &value );
if( value != 0 )
{
s = (NODE *)malloc( sizeof( NODE ) );
s->value = value;
printf("/n s->value = %d", s->value);
p->link = s;
p = s;
}
else
cycle = 0;
}
head = head->link;
p->link = NULL;
return head;
}
void print( NODE *head )
{
NODE *p = NULL;
int n = 0;
n = length( head );
printf( "/n Now, These %d records are:/n", n );
p = head;
while( p != NULL )
{
printf("/n p->value %d", p->value);
p = p->link;
}
printf("/n");
}
NODE *reserve( NODE *head )
{
NODE *p1, *p2, *p3;
printf("/nEntry reserve() ======= begin !!!!");
if( head == NULL || head->link == NULL )
return head;
p1 = head;
p2 = p1->link;
while( p2 )
{
p3 = p2->link;
p2->link = p1;
p1 = p2;
p2 = p3;
}
head->link = NULL;
head = p1;
printf("/nEntry reserve() ======= end !!!!");
return head;
}
int sll_insert( NODE **rootp, int new_value )
{
register NODE *current;
register NODE *_new;
printf("/nEntry sll_insert() ======= begin !!!!");
while( (current = *rootp) != NULL && current->value < new_value )
{
rootp = ¤t->link;
}
_new = (NODE *)malloc( sizeof(NODE) );
if( _new == NULL )
return FALSE;
_new->value = new_value;
_new->link = current;
*rootp = _new;
printf("/nEntry sll_insert() ======= end !!!!");
return TRUE;
}
NODE *del( NODE *head, int value )
{
NODE *p1, *p2;
p1 = head;
printf("/nEntry del() ======= begin !!!!");
while( p1->link != NULL && value != p1->value )
{
p2 = p1;
p1 = p1->link;
}
if( value == p1->value )
{
if( p1 == head )
{
head = p1->link;
free( p1 );
}
else
{
p2->link = p1->link;
free( p1 );
}
}
else
{
printf( "/n %d is could not been found", value );
return NULL;
}
printf("/nEntry del() ======= end !!!!");
return head;
}
NODE *sort( NODE *head )
{
NODE *p;
int temp;
int n;
int i, j;
printf("/nEntry sort() ======= begin !!!!");
n = length(head);
if( head == NULL || head->link == NULL )
return head;
for( j = 1; j < n; j++ )
{
p = head;
for( i = 0; i < n-j; i++ )
{
if( p->value > p->link->value )
{
temp = p->value;
p->value = p->link->value;
p->link->value = temp;
}
p = p->link;
}
}
printf("/nEntry sort() ======= end !!!!");
return head;
}
int main(void)
{
NODE *head = NULL;
NODE *_new = NULL;
NODE *returnp = NULL;
NODE *delp = NULL;
NODE *sortp = NULL;
int length = 0;
head = create();
print( head );
_new = reserve( head );
print(_new);
sll_insert( &_new, 100);
print( _new );
delp = del( _new, 4 );
print( delp );
sortp = sort(delp);
print( sortp );
return 0;
}