双链表 c teacher-double-nohead-noloop
双链表 c teacher-double-nohead-noloop
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
typedef struct girl {
char name[30];
int bra;
int beautiful;
} girl_t ;
void input_girl( girl_t * girl )
{
printf("Input girl name: ");
gets( girl->name );
printf("Input gril bra: ");
scanf("%d", &girl->bra );
printf("Input girl beautiful: ");
scanf("%d", &girl->beautiful );
getchar();
}
void print_girl( void * data )
{
girl_t * girl = ( girl_t * ) data;
printf("girl name: %s\n", girl->name );
printf("girl bra: %d\n", girl->bra );
printf("girl beautiful: %d\n\n", girl->beautiful );
}
int del_condition( void * data )
{
girl_t * girl = ( girl_t * ) data;
if( girl->bra > 80 || girl->bra < 40 || girl->beautiful < 6 )
return 1;
return 0;
}
int compare( void * data1, void * data2 )
{
girl_t * girl1 = ( girl_t * )data1;
girl_t * girl2 = ( girl_t * )data2;
if( girl1->beautiful > girl2->beautiful )
return -1;
return 1;
}
void menu( void )
{
printf("(1) insert from end .\n");
printf("(2) delete a node .\n");
printf("(3) sort list.\n" );
printf("(4) print list .\n");
printf("(5) save file.\n");
printf("(6) load file.\n");
printf("(0) exit.\n");
printf("Choice: ");
}
int main( int argc, char ** argv )
{
node_t * head = NULL;
char filename[30];
while( 1 ) {
menu();
int choice;
girl_t girl;
scanf( "%d", &choice );
getchar();
switch( choice ){
case 0:
destroy_list( &head );
exit( 0 );
case 1:
input_girl( &girl );
insert_node( &head, &girl, sizeof(girl_t) );
break;
case 2:
del_node( &head, del_condition );
break;
case 3:
sort_node( head, compare );
break;
case 4:
print_list( head, print_girl );
break;
case 5:
printf("Save as: ");
gets( filename );
save_list( head, sizeof(girl_t), filename );
break;
case 6:
printf("Input the filename: ");
gets( filename );
load_list( &head, sizeof(girl_t), filename );
break;
default:
printf("choice error.\n");
break;
}
}
return 0;
}
list.h
#ifndef _GEN_NOLOOP_SINGLE_
#define _GEN_NOLOOP_SINGLE_
typedef struct node node_t;
struct node {
void * data;
node_t * pre;
node_t * next;
};
#define TYPE_LEN 4
#define TYPE_NAME "LIST"
int length( node_t * head );
node_t * get_num( node_t * head, int num );
int insert_node( node_t ** head, void * data, int data_len );
int del_node( node_t ** head, int ( * condition )( void * ) );
int sort_node( node_t * head, int ( * compare )( void *, void * ) );
int print_list( node_t * head, void ( * print )( void * data ) );
int destroy_list( node_t ** head );
int save_list( node_t * head, int data_len, char * filename );
int load_list( node_t ** head, int data_len, char * filename );
#endif /* _GEN_NOLOOP_SINGLE_ */
list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
int insert_node( node_t ** head, void * data, int data_len )
{
node_t * node = ( node_t * )malloc( sizeof(node_t) );
node->data = malloc( data_len );
memcpy( node->data, data, data_len );
node->pre = NULL;
if( *head == NULL ) {
*head = node;
(*head)->next = NULL;
} else {
(*head)->pre = node;
node->next = *head;
*head = node;
}
}
int del_node( node_t ** head, int ( * condition )( void * ) )
{
node_t * temp = *head;
while( temp != NULL ) {
int ret = condition( temp->data );
if( ret == 1 ) {
if( temp == *head ) {
*head = temp->next;
free( temp->data );
free( temp );
if( *head != NULL )
(*head)->pre = NULL;
temp = *head;
} else {
node_t * temp_next = temp->next;
if( temp->next != NULL )
temp->next->pre = temp->pre;
temp->pre->next = temp->next;
free( temp->data );
free( temp );
temp = temp_next;
}
} else {
temp = temp->next;
}
}
}
node_t * get_num( node_t * head, int num )
{
while( head != NULL ) {
if( num -- == 0 )
return head;
head = head->next;
}
return NULL;
}
int length( node_t * head )
{
int i = 0;
while( head != NULL ) {
++ i;
head = head->next;
}
return i;
}
int sort_node( node_t * head, int ( * compare )( void *, void * ) )
{
node_t * temp = head;
int len = length( head );
int i, j;
for( i=0; i<len-1; ++i )
for( j=0; j<len-1-i; ++j ){
node_t * n1 = get_num( head, j );
node_t * n2 = get_num( head, j+1 );
int ret = compare( n1->data, n2->data );
if( ret > 0 ) {
void * temp_data;
temp_data = n1->data;
n1->data = n2->data;
n2->data = temp_data;
}
}
return 0;
}
int merge_list( node_t ** head1, node_t * head2 )
{
if( *head1 == NULL ) {
*head1 = head2;
} else {
if( head2 != NULL ) {
node_t * temp = *head1;
while( temp->next != NULL )
temp = temp->next;
temp->next = head2;
head2->pre = temp;
}
}
}
int print_list( node_t * head, void ( * print )( void * ) )
{
while( head != NULL ) {
print( head->data );
head = head->next;
}
}
int destroy_list( node_t ** head )
{
node_t * temp = *head;
while( temp != NULL ) {
temp = temp->next;
free( (*head)->data );
free( (*head) );
*head = temp;
}
return 0;
}
int save_list( node_t * head, int data_len , char * filename )
{
FILE * fp = fopen( filename, "wb" );
if( fp == NULL ) {
perror( "fopen" );
exit( 1 );
}
char magic[ TYPE_LEN + 1 ] = TYPE_NAME;
fwrite( magic, 4, 1, fp );
int ret;
while( head != NULL ) {
ret = fwrite( head->data, data_len, 1, fp );
if( ret != 1 )
continue;
head = head->next;
}
fclose( fp );
}
int load_list( node_t ** head, int data_len, char * filename )
{
FILE * fp = fopen( filename, "rb" );
if( fp == NULL ) {
perror( "fopen" );
return -1;
}
char * buf = ( char * )malloc( TYPE_LEN );
fread( buf, TYPE_LEN, 1, fp );
if( strncmp( buf, TYPE_NAME, 4 ) != 0 ) {
printf("Can't parse the file type.\n");
return -1;
}
destroy_list( head );
int ret;
void * data_buf = malloc( data_len );
while( feof( fp ) == 0 ) {
ret = fread( data_buf, data_len, 1, fp );
if( ret != 1 )
continue;
insert_node( head, data_buf, data_len );
}
fclose( fp );
}
Makefile
all: main
main: main.c list.c list.h Makefile
gcc -o main main.c list.c -g