当前位置:网站首页>DHU programming exercise
DHU programming exercise
2022-06-30 02:09:00 【qq_ forty-three million four hundred and three thousand six hun】
1 Find sequence elements ( Linked list )
#include<iostream>
using namespace std;
/* to define a linklist node*/
struct node {
int val;
struct node *next;
};
/*create a list based on data input*/
node *createList() {
node *head = NULL, *p, *tail = NULL;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
p = (node*)malloc(sizeof(node));//generate a new node
if (head == NULL)
head = p;
else
tail->next = p;
p->val = num;
p->next = NULL;
tail = p;
}
return head;
}
/*traverse the linklist and find the target's location*/
int Locate(node *head, int num)
{
node *p;
p = head;
int location = 1;
while (p)
{
if (p->val == num)
break;
p = p->next;
location++;
}
if (p == NULL)
return 0;
else
return location;
}
int main() {
struct node *head;
head = createList();
int num;
while (cin>>num)
{
int loc = Locate(head, num);
if (loc != 0)
cout << loc << endl;
else
cout << "no" << endl;
}
return 0;
}
边栏推荐
- Looking for thesaurus data [closed]
- AI落地制造业:智能机器人应具备这4种能力
- Matlab 2012a 绘制带箭头的线段
- 如何制作CSR(Certificate Signing Request)文件?
- Oppo mobile phone search
- Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
- 018_ rate
- The national industrial information security development research center issued the report on industrial information security situation in 2021
- Fake divorce turns into real divorce. What about property
- DMX configuration
猜你喜欢

魔百盒CM201-2-CH-Hi3798MV300-300H-EMMC和NAND_红外蓝牙语音_通刷固件包

(4) Blender source code analysis flash window display process

Using grpcui to test asp Net core grpc service

当大学毕业感到迷茫怎么办?

What problems can cloud storage architecture solve for Devops?

Want to change careers, but don't know what you want to do?

DTW学习(dynamic time warping)——思想、代码实现

Knowledge payment cannot escape the essence of "anxiety"
![[MySQL 06] backup and restore MySQL database in Linux + docker container environment](/img/4e/8662d15ff84b2436d02948019540d3.png)
[MySQL 06] backup and restore MySQL database in Linux + docker container environment

What should I do when I feel confused after graduation from university?
随机推荐
Using grpcui to test asp Net core grpc service
Tencent released the first Office Photo 23 years ago. It's so chronological
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
What should I do when I feel confused after graduation from university?
js Array. Five convenient applications of from()
[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework
【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态
【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
Write this number in C
The (3n+1) conjecture that C language kills people without paying for their lives
DTW学习(dynamic time warping)——思想、代码实现
012_ switch
Unity2d-- add keys to animation and bind events
What are the payment and distribution systems?
记录生产的一次OOM异常
8 — router
图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
CTF入门学习(Web方向)
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
7 — filter