더블 체인 테이블 teacher-bouble-loop-nohead

37621 단어 each
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
" , girl -> name );
printf(
" girl bra: %d
" , girl -> bra );
printf(
" girl beautiful: %d

" , 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 .
" );
printf(
" (2) delete a node .
" );
printf(
" (3) sort list.
" );
printf(
" (4) print list .
" );
printf(
" (5) save file.
" );
printf(
" (6) load file.
" );
printf(
" (0) exit.
" );
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 4 :
print_list( head, print_girl );
break ;
default :
printf(
" choice error.
" );
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
* temp = malloc( sizeof (node_t) );
temp
-> data = malloc( data_len );
memcpy( temp
-> data, data, data_len );

if ( * head == NULL ) {
* head = temp;
(
* head) -> next = * head;
(
* head) -> pre = * head;
}
else {
(
* head) -> pre -> next = temp;
temp
-> pre = ( * head) -> pre;
temp
-> next = * head;
(
* head) -> pre = temp;
* head = temp;
}
}

int length( node_t * head )
{
node_t
* temp = head;

if ( head == NULL )
return 0 ;

int i = 0 ;
do {
i
++ ;
temp
= temp -> next;
}
while ( temp != head );

return i;
}

int del_node( node_t ** head, int ( * condition )( void * data ) )
{
if ( * head == NULL )
return 0 ;

int len = length( * head );

node_t
* temp = * head;
while ( len > 0 ) {
int ret = condition( temp -> data );
node_t
* temp_next = temp -> next;
if ( ret == 1 ) {
if ( temp -> next == temp ) {
free( temp
-> data );
free( temp );
* head = NULL;
return 0 ;
}
else {
temp
-> pre -> next = temp -> next;
temp
-> next -> pre = temp -> pre;
free( temp
-> data );
free( temp );
}
}
temp
= temp_next;
len
-- ;
}
* head = temp;
}

int print_list( node_t * head, void ( * print )( void * ) )
{
node_t
* temp = head;
if ( temp == NULL )
return 0 ;
do {
print( temp
-> data );
temp
= temp -> next;
}
while ( temp != head );
}

int destroy_list( node_t ** head )
{
node_t
* temp = * head;

if ( temp == NULL )
return 0 ;
do {
node_t
* temp_next = temp -> next;
free( temp
-> data );
free( temp );
temp
= temp_next;
}
while ( temp != * head );

* head = NULL;
}

int merge_list( node_t ** head1, node_t * head2 )
{
if ( * head1 == NULL )
* head1 = head2;
else if ( head2 == NULL )
return 0 ;
node_t
* head2_pre = head2 -> pre;

(
* head1) -> pre -> next = head2;
head2
-> pre = ( * head1) -> pre;
(
* head1) -> pre = head2_pre;
head2_pre
-> next = * head1;
}

Makefile

  
all: main

main: main.c list.c list.h
gcc
- o main main.c list.c

좋은 웹페이지 즐겨찾기