3.c

来自「数据结构练习共18道。」· C语言 代码 · 共 46 行

C
46
字号
#include <stdio.h>
#include<malloc.h>
typedef struct node{
	int Elem;
	struct node *next;
}Lnode;

void main()
{
	int i,num,times=0;
	Lnode *head,*p,*tail;
	head =(Lnode*) malloc(sizeof (Lnode));
	head->Elem=0;
	head->next=NULL;
	for (i=0;i<=9 ;i++ )
	{
		p =(Lnode*) malloc(sizeof (Lnode));
		printf("Input the Elem:");
		scanf("%d",&p->Elem);
		p->next=NULL;
		//printf("%d",p->Elem);
		if (head->next==NULL)
		{
			head->next = p;
			tail=p;
		}
		else
		{tail->next=p;tail=p;}
	}
	printf("Input the searching integer:");
	scanf("%d",&num);
	p=head->next;
	//while(p!=NULL){printf("%d ",p->Elem);p=p->next;}
	while(p!=NULL)
	{if (p->Elem==num)
		{
		printf("\nThe compare times is %d:",times);
		return;
		}
	else {p=p->next;times++;}
	}
	if (p==NULL)
	{
		printf("\nThe Elem is not in!");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?