线性表----访问链表中的数据及查询链表

    科技2022-08-19  102

    #include <stdio.h> #include <stdlib.h> struct Test { int data; struct Test *next; }; void printLink(struct Test *head) { struct Test *p; p = head; while(p != NULL){ printf("data = %d\n",p->data); p = p->next; } printf("--------\n"); } int printLinkNum(struct Test *head) { int cnt = 0; struct Test *p; p = head; while(p !=NULL){ cnt++; p = p->next; } return cnt; } int searchLink(struct Test *head,int data2) { struct Test *p; p = head; while(p !=NULL){ if(p->data == data2){ return 1; } p = p->next; } return -1; } int main() { int num; int ret; struct Test t1 = {1,NULL}; struct Test t2 = {2,NULL}; struct Test t3 = {3,NULL}; struct Test t4 = {4,NULL}; t1.next = &t2; t2.next = &t3; t3.next = &t4; printLink(&t1); num = printLinkNum(&t1); printf("-------------------"); printf("num = %d\n",num); if( searchLink(&t1,20) == 1){ printf("find ok\n"); }else{ printf("not find\n"); } return 0; }
    Processed: 0.009, SQL: 9