# include <stdio.h>
# include <stdlib.h>
# define TRUE 1
# define FASLE 0
typedef struct Node {
int data;
struct Node * next;
} Node;
Node* InitList ( ) {
Node* list = ( Node* ) malloc ( sizeof ( Node) ) ;
list-> data = 0 ;
list-> next = list;
return list;
}
void headInsert ( Node* list, int data) {
Node* node = ( Node* ) malloc ( sizeof ( Node) ) ;
node-> data = data;
node-> next = list-> next;
list-> next = node;
list-> data++ ;
}
void tailInsert ( Node* list, int data) {
Node* n = list;
Node* node = ( Node* ) malloc ( sizeof ( Node) ) ;
node-> data = data;
while ( n-> next != list) {
n = n-> next;
}
node-> next = list;
n-> next = node;
list-> data++ ;
}
void printList ( Node* list) {
Node* node = list-> next;
while ( node != list) {
printf ( "%d->" , node-> data) ;
node = node-> next;
}
printf ( "NULL\n" ) ;
}
int num ( Node* list) {
return list-> data;
}
int DeleteList ( Node* list, int data) {
Node* prenode = list;
Node* current = list-> next;
while ( current!= list) {
if ( current-> data == data) {
prenode-> next = current-> next;
free ( current) ;
list-> data-- ;
return TRUE;
}
else {
prenode = current;
current = current -> next;
}
}
return FASLE;
}
int main ( ) {
Node* list= InitList ( ) ;
headInsert ( list, 1 ) ;
headInsert ( list, 2 ) ;
headInsert ( list, 3 ) ;
headInsert ( list, 4 ) ;
tailInsert ( list, 5 ) ;
tailInsert ( list, 6 ) ;
tailInsert ( list, 43 ) ;
tailInsert ( list, 21 ) ;
tailInsert ( list, 31 ) ;
printList ( list) ;
printf ( "%d\n" , num ( list) ) ;
DeleteList ( list, 4 ) ;
printList ( list) ;
printf ( "%d\n" , num ( list) ) ;
DeleteList ( list, 31 ) ;
printList ( list) ;
printf ( "%d\n" , num ( list) ) ;
return 0 ;
}