问题:最后指针C是否有指向节点?如果有,那是一个怎样的节点?
# include <stdio.h>
struct LinkNode {
int data;
struct LinkNode * next;
} ;
void initLinkList ( struct LinkNode * & L) {
struct LinkNode * p, * s;
int num;
L = new LinkNode;
p = L;
printf ( "初始化链表->每输入一个数需要用回车来结束该数输入,最后以Ctrl+Z结束输入。\n" ) ;
while ( scanf ( "%d" , & num) != EOF ) {
s = new LinkNode;
s-> data = num;
p-> next = s;
p = s;
}
p-> next = NULL ;
}
void printLinkList ( struct LinkNode * L) {
struct LinkNode * p = L-> next;
while ( p) {
printf ( "%d\t" , p-> data) ;
p = p-> next;
}
printf ( "\n" ) ;
}
void destroyLinkList ( struct LinkNode * & L) {
struct LinkNode * p;
while ( L) {
p = L-> next;
delete ( L) ;
L = p;
}
}
void mergeLinkList ( struct LinkNode * L1, struct LinkNode * L2, struct LinkNode * & L3) {
struct LinkNode * p = L1-> next, * q = L2-> next, * r;
L3 = L1;
r = L3;
while ( p && q) {
if ( p-> data < q-> data) {
r-> next = p;
r = p;
p = p-> next;
}
else {
r-> next = q;
r = q;
q = q-> next;
}
}
if ( p == NULL ) {
p = q;
}
r-> next = p;
}
void reverseLinkList ( struct LinkNode * L) {
struct LinkNode * r = NULL , * s = L-> next, * p;
L-> next = NULL ;
while ( s) {
p = s-> next;
s-> next = r;
r = s;
s = p;
}
L-> next = r;
}
struct LinkNode * reverseLinkList2 ( struct LinkNode * L) {
struct LinkNode * p = L-> next, * r;
if ( L-> next == NULL ) {
return L;
}
r = reverseLinkList2 ( p) ;
p-> next = L;
L-> next = NULL ;
return r;
}
void printLinkListByDiGui ( struct LinkNode * L) {
struct LinkNode * p = L;
while ( p && p-> next) {
printf ( "%d\t" , p-> data) ;
p = p-> next;
}
}
int main ( ) {
struct LinkNode * A, * B, * C;
struct LinkNode * D;
initLinkList ( A) ;
printf ( "A链表>>>\n" ) ;
printLinkList ( A) ;
initLinkList ( B) ;
printf ( "B链表>>>\n" ) ;
printLinkList ( B) ;
mergeLinkList ( A, B, C) ;
printf ( "合并>>>\n" ) ;
printLinkList ( C) ;
printf ( "逆置合并后的链表>>>\n" ) ;
reverseLinkList ( C) ;
printLinkList ( C) ;
printf ( "再次逆置:\n" ) ;
D = reverseLinkList2 ( C) ;
printf ( "打印第二次逆置的链表:\n" ) ;
printLinkListByDiGui ( D) ;
destroyLinkList ( D) ;
return 0 ;
}