数据结构-单链表查找按序号查找

#include
#include
#define ERROR -40000
using namespace std;

typedef struct LNODE
{
	int data;
	struct LNODE *next;
}Lnode;

Lnode *Input(int n)
{
	Lnode *head,*p,*q;
	head=p=(Lnode*)malloc(sizeof(Lnode));
	p->next=NULL;
	for(int i=0;i>data;
		q=(Lnode*)malloc(sizeof(Lnode));
		q->data=data;
		q->next=p->next;
		p->next=q;
		p=q;
	}
	return head;
}

int FindData(Lnode *L,int id)
{
	Lnode *p;
	p=L->next;
	int j=1;
	while(p!=NULL&&jnext;
		j++;
	}
	if(j!=id||p==NULL) return ERROR;//书上没有此处判断n+1位置输入后bug,指针没有分配内存 
	else return p->data;
}

int main()
{
	int n;
	while(cin>>n)
	{
		Lnode *p=Input(n);
		cout<<"数据查询:"<>num;
			int find_data=FindData(p,num);
			if(find_data!=ERROR)
			{
				cout<<"查找结果为:"<

你可能感兴趣的:(数据结构)