# 笔试题六：链表的顺序查找 并输出

425人阅读 评论(0)

struct ListNode

{

int data;

ListNode * nextNode;

}

int sequential_search(const List<int> &the_list,
const Key &target)
/*Post: If an entry in the_list is equal to target, then return the position
of this entry.
Otherwise return -1
*/
{
int position;
int s=the_list.size();
for(position=0;position<s;position++){
int data;
the_list.retrieve(position,data);
if(data==target){
return position;
}
}
return -1;
}

#include "stdafx.h"
#include<iostream>
#include<queue>
using namespace std;

typedef struct node{
int data;
struct node *next;
}Node,*List;

List createList(int N)
{
int count = 0;
while(count<N)
{	count++;
List s = (List)malloc(sizeof(Node));
s->data = count;
s->next = NULL;
p->next = s;
p = s;
}
}

{
{
return;
}
while(p)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
{
int count = 0;
List second = NULL;
while(first&&count<index)//first首先找到第index个元素
{
first = first->next;
count++;
}
//cout<<"count: "<<first->data<<endl;
if(first!=NULL)//如果没有到链表尾
{
while(first!=NULL)
{
first = first->next;
second = second->next;
}
}
return second;
}
int main()
{
int N = 20;
if(no!=NULL)
{
cout<<no->data<<endl;
}else{
cout<<"can't find"<<endl;
}
getchar();
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：5233次
• 积分：146
• 等级：
• 排名：千里之外
• 原创：10篇
• 转载：0篇
• 译文：0篇
• 评论：4条
文章分类
文章存档
评论排行
最新评论