单链表数值查找 -- C语言

代码实现

st_dataNode* searchListNode(st_dataNode* head, int num){
	if(NULL == head){
		return NULL;
	}

	st_dataNode* p = NULL;
	st_dataNode* q = NULL;

	p = head;
	while(NULL != p){
		if(p->data == num){
			q = p;
			break;
		}

		p = p->next;
	}

	return q;
}



void testSearchList(void){
	st_dataNode * rst = NULL;
	rst = searchListNode(ghead, 6);
	if(NULL != rst){
		printf("node: %p, data = %d\n", rst, rst->data);
	} else {
		printf("Can not found num 6 \n");
	}

	rst = searchListNode(ghead, 119);
	if(NULL != rst){
		printf("node: %p, data = %d\n", rst, rst->data);
	} else {
		printf("Can not found num 119 \n");
	}	
}

代码编译

gcc listMain.c list.c -o a.exe -DDEBUG

调试输出

========= Dump List 0x1de6010 ===========
         22  32  19  53  0  47  29  116  4  6
===================================
List length = 10
node: 0x1de6130, data = 6
Can not found num 119
发布了191 篇原创文章 · 获赞 43 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/leoufung/article/details/104334265