4个回答
展开全部
#include<iostream>
using namespace std;
class Chain;
class ChainNode
{
friend Chain;
private:
int data;
ChainNode *link;
};
class Chain
{
public:
Chain();
~Chain();
bool IsEmpty()const{return first==0;}
void fun();//查找函数
private:
ChainNode *first;//指向第一个节点指针
};
Chain::Chain()
{
first=new ChainNode;
first->data=1;
first->link=NULL;
}
Chain::~Chain()
{
ChainNode *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
void Chain::fun(int key)
{
if(first==NULL)
return;
ChainNode *p=first->link;
while(p!=NULL)
{
if(p->data==key)
return 1;
else
p=p->link;
}
}
int main()
{
int n;
Chain mychain1;
cout<<"请输入需要查找的节点的关键值:";
cin>>n;
mychain1.fun(n);
cout<<endl;
return 0;
}
using namespace std;
class Chain;
class ChainNode
{
friend Chain;
private:
int data;
ChainNode *link;
};
class Chain
{
public:
Chain();
~Chain();
bool IsEmpty()const{return first==0;}
void fun();//查找函数
private:
ChainNode *first;//指向第一个节点指针
};
Chain::Chain()
{
first=new ChainNode;
first->data=1;
first->link=NULL;
}
Chain::~Chain()
{
ChainNode *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
void Chain::fun(int key)
{
if(first==NULL)
return;
ChainNode *p=first->link;
while(p!=NULL)
{
if(p->data==key)
return 1;
else
p=p->link;
}
}
int main()
{
int n;
Chain mychain1;
cout<<"请输入需要查找的节点的关键值:";
cin>>n;
mychain1.fun(n);
cout<<endl;
return 0;
}
展开全部
#include<stdio.h>
#include<Windows.h>
struct list{
int data;
struct list* ptr;
};
struct list* chuangjian(int i){
struct list* p;
p=(struct list*)malloc(sizeof(struct list));
p->data=i;
p->ptr=NULL;
return p;
}
struct list* charu(struct list* start,struct list* insert){
struct list* pre=NULL;
struct list* current=start;
if(start==NULL){
start=insert;
return start;
}
while(current->ptr!=NULL)
current=current->ptr;
current->ptr=insert;
return start;
}
int check (int i,struct list* start,int* J){
*J=*J+1;
if(start->data==i){
return 1;
}
else if(start->ptr==NULL)
return 0;
return check(i,start->ptr,J);
}
main()
{
int i,j=0;
int* J;
struct list* start=NULL;
J=&j;
printf("请输入一个链表,以-1结束:\n");
scanf("%d",&i);
for(;i!=-1;scanf("%d",&i))start=charu(start,chuangjian(i));
printf("请输入要查找的整数:");
scanf("%d",&i);
i=check (i,start,J);
if(i==1)
printf("在第%d个结点。",j);
else
printf("不存在。");
system("pause");
}
#include<Windows.h>
struct list{
int data;
struct list* ptr;
};
struct list* chuangjian(int i){
struct list* p;
p=(struct list*)malloc(sizeof(struct list));
p->data=i;
p->ptr=NULL;
return p;
}
struct list* charu(struct list* start,struct list* insert){
struct list* pre=NULL;
struct list* current=start;
if(start==NULL){
start=insert;
return start;
}
while(current->ptr!=NULL)
current=current->ptr;
current->ptr=insert;
return start;
}
int check (int i,struct list* start,int* J){
*J=*J+1;
if(start->data==i){
return 1;
}
else if(start->ptr==NULL)
return 0;
return check(i,start->ptr,J);
}
main()
{
int i,j=0;
int* J;
struct list* start=NULL;
J=&j;
printf("请输入一个链表,以-1结束:\n");
scanf("%d",&i);
for(;i!=-1;scanf("%d",&i))start=charu(start,chuangjian(i));
printf("请输入要查找的整数:");
scanf("%d",&i);
i=check (i,start,J);
if(i==1)
printf("在第%d个结点。",j);
else
printf("不存在。");
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
//---查找元素------------
linklist *mindata(linklist *head, datatype a)
{
linklist *p;
p = head->next;
while( p != NULL && p->data != a)
p = p->next;
}
return p ;
}
//------主函数---------------------
main()
{
linklist *head;
......
getchar();
} // main
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
//---查找元素------------
linklist *mindata(linklist *head, datatype a)
{
linklist *p;
p = head->next;
while( p != NULL && p->data != a)
p = p->next;
}
return p ;
}
//------主函数---------------------
main()
{
linklist *head;
......
getchar();
} // main
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用while循环从头节点开始查找知道搜索成功(返回节点指针)过到链表尾(搜索失败、返回NULL)、、
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询