http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2054&cid=1156
1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef struct node 4 { 5 int data; 6 struct node *rb; 7 struct node *zb; 8 }lb; 9 lb *creat(int n) 10 { 11 lb *head,*p,*tail; 12 int i; 13 head=(lb *)malloc(sizeof(lb)); 14 head->rb=NULL; 15 head->zb=NULL; 16 tail=head; 17 for(i=0;i<n;i++) 18 { 19 p=(lb *)malloc(sizeof(lb)); 20 scanf("%d",&p->data); 21 p->rb=NULL; 22 tail->rb=p; 23 p->zb=tail; 24 tail=p; 25 } 26 return head; 27 } 28 void search(lb *head,int m) 29 { 30 int i,a; 31 lb *q,*p; 32 for(i=0;i<m;i++) 33 { 34 scanf("%d",&a); 35 p=head->rb; 36 while(p!=NULL) 37 { 38 if(p->data==a) 39 { 40 q=p; 41 if(q==head->rb) 42 { 43 printf("%d\n",q->rb->data); 44 break; 45 } 46 else if(q->rb==NULL) 47 { 48 printf("%d\n",q->zb->data); 49 break; 50 } 51 else 52 {printf("%d %d\n",q->zb->data,q->rb->data);break;} 53 } 54 else 55 p=p->rb; 56 } 57 } 58 } 59 int main() 60 { 61 int n,m; 62 lb *head; 63 scanf("%d %d",&n,&m); 64 head=creat(n); 65 search(head,m); 66 return 0; 67 }