-->
当前位置:首页 > 题库 > 正文内容

函数题:查找链表中值为x的结点

Luz2年前 (2022-11-12)题库401
要求实现函数,查找带头结点的单链表中数据域值为x的结点,若找到则返回该结点在链表中的序号,否则返回0。

### 函数接口定义:
c++
int Search(Node* head, int x);

其中参数head是指向单链表头结点的指针(头指针),结点的指针域为next;参数x为待查找的某个值。

### 裁判测试程序样例:
c++
#include<iostream>
using namespace std;

struct Node {
int data; // 数据域
Node* next; // 指针域
};

Node* Create(int n);
int Search(Node* head, int x);

int main() {
int n;
while(cin>>n) {
Node *h=Create(n);
int x;
cin>>x;
cout<<Search(h, x)<<endl;
}
return 0;
}


### 输入样例:

in
5
5 4 3 2 1
9
4
11 21 33 45
33


### 输出样例:

out
0
3






答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。