9.8链表删除指定节点
# include <stdio.h>
# include <stdlib.h>
struct Test
{
int data;
struct Test * next;
} ;
void printLink ( struct Test * head)
{
struct Test * point;
point = head;
while ( point != NULL ) {
printf ( "%d " , point-> data) ;
point = point-> next;
}
putchar ( '\n' ) ;
}
int getLinkTotalNodeNum ( struct Test * head)
{
int cnt = 0 ;
struct Test * p = head;
while ( p != NULL ) {
cnt++ ;
p = p-> next;
}
return cnt;
}
int searchLink ( struct Test * head, int data)
{
while ( head != NULL ) {
if ( head-> data = data) {
return 0 ;
}
head = head-> next;
}
return 0 ;
}
struct Test * insertFromfor ( struct Test * head, int data, struct Test * new)
{
struct Test * p = head;
if ( p-> data == data) {
new-> next = head;
return new;
}
while ( p-> next != NULL ) {
if ( p-> next-> data == data) {
new-> next = p-> next;
p-> next = new;
printf ( "insert OK\n" ) ;
return head;
}
p = p-> next;
}
printf ( "no this data%d\n" , data) ;
return head;
}
int insertFromBehind ( struct Test * head, int data, struct Test * new)
{
struct Test * p = head;
while ( p != NULL ) {
if ( p-> data == data) {
new-> next = p-> next;
p-> next = new;
return 1 ;
}
p = p-> next;
}
return 0 ;
}
struct Test * deletNode ( struct Test * head, int data)
{
struct Test * p = head;
if ( p-> data == data) {
head = head-> next;
free ( p) ;
return head;
}
while ( p-> next != NULL ) {
if ( p-> next-> data == data) {
p-> next = p-> next-> next; \
return head;
}
p = p-> next;
}
return head;
}
int main ( )
{
struct Test * head = NULL ;
struct Test * p = ( struct Test * ) malloc ( sizeof ( struct Test ) ) ;
struct Test t2 = { 2 , NULL } ;
struct Test t3 = { 3 , NULL } ;
struct Test t4 = { 4 , NULL } ;
p-> data = 1 ;
p-> next = & t2;
t2. next = & t3;
t3. next = & t4;
head = p;
printLink ( head) ;
head = deletNode ( head, 4 ) ;
printLink ( head) ;
return 0 ;
}