小言_互联网的博客

链表:编写一个函数int fun(LNode *h,int x)。函数功能:统计链表中数值为x的结点的个数。

258人阅读  评论(0)
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef  struct list
{  
	int  data;
   	struct list  *next;
}LNode;
LNode *creatlist(int  *a)
{  
	LNode  *h,*p,*q;      
	int  i;
   	h=p=(LNode *)malloc(sizeof(LNode));
   	for(i=0; i<N; i++)
   	{  
	   q=(LNode *)malloc(sizeof(LNode));
       q->data=a[i];  
	   p->next=q;  
	   p=q;
   	}
   p->next=0;
   return h;
}
void outlist(LNode  *h)
{  
   LNode  *p;
   p=h->next;
   if (p==NULL)  
   		printf("\nThe list is NULL!\n");
   else
   {  
   	  printf("\nHead");
      do { 
	  	printf("->%d",p->data);  
	  	p=p->next;    
	  } while(p!=NULL);
      printf("->End\n");
  }
}
int fun(LNode *h,int x)//功能函数
{
	LNode *p;
	int count=0;
	p=h->next;
	while(p)
	{
		if(p->data==x)
		{
			count++;
		}
		p=p->next;
	}
	return count;	
} 
int main( )
{  
	LNode *A;    
	int a[N]={4,10,7,5,9};
   	A=creatlist(a);
	printf("结点值为5的总个数为:%d",fun(A,5));
	return 0;
}

转载:https://blog.csdn.net/weixin_38174000/article/details/102391914
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场