链表的遍历
/* ------------------------------------ */ #include " stdio.h " #include " stdlib.h " struct llist{ int num; char name[ 10 ]; struct llist * next;};typedef struct llist node;typedef node * llink; /* -----------------链表的创建---------------- */ llink createllist(){ llink head; llink ptr,ptr1; int i; /* 创建第一个结点 */ head = (llink)malloc( sizeof (node)); if ( ! head) return NULL; printf( " 输入六项邮寄数据:\n " ); printf( " 请输入编号 ==> " ); scanf( " %s " ,head -> num); printf( " 请输入编号(%d)的姓名 ==> " ,head -> num); scanf( " %s " ,head -> name); head -> next = NULL; ptr = head; for ( i = 1 ; i < 6 ; i ++ ) { ptr1 = (llink)malloc( sizeof (node)); if ( ! ptr1) return NULL; printf( " 请输入编号 ==> " , ptr1 -> num); scanf( " %s " ,ptr1 -> name); ptr -> next = NULL; ptr -> next = ptr1; ptr = ptr -> next; } return head;} /* --------------链表的结点遍历---------------- */ llink findnode(llink head, int num){ llink ptr; ptr = head; while (ptr != NULL) { if (ptr -> num == num) return ptr; ptr = ptr -> next; } return ptr;} /* ----------------------输出查找邮寄姓名------------------- */ int main(){ llink head; llink ptr; int num; head = createllist(); if ( ! head) { printf( " 内存分配失败! \n " ); exit( 1 ); } while ( 1 ) { printf( " 请输入要寻找的邮寄编号 ==> " ); scanf( " %d " , & num); if (num != 0 ) { ptr = findnode(head,num); if ( ! ptr) printf( " 没有找到\n " ); else printf( " 姓名: %s\n " ,ptr -> name); } else exit( 1 ); }}