数据结构请任意输入n个整数,直至-1结束,给定1个待查数据e,请给出该数是第几个数据;代码
时间: 2023-04-07 17:03:46 浏览: 49
可以使用数组来存储输入的n个整数,然后遍历数组查找待查数据e的位置,最后输出该位置即可。以下是示例代码:
```
#include <stdio.h>
int main() {
int arr[1000], n = 0, e, i;
printf("请输入整数,以-1结束:\n");
while (1) {
scanf("%d", &arr[n]);
if (arr[n] == -1) break;
n++;
}
printf("请输入待查数据:\n");
scanf("%d", &e);
for (i = 0; i < n; i++) {
if (arr[i] == e) {
printf("%d 是第 %d 个数据\n", e, i + 1);
return 0;
}
}
printf("%d 不在输入的数据中\n", e);
return 0;
}
```
相关问题
数据结构请任意输入n个整数,直至-1结束,使用单链表存储输入的数据;
好的,这是一个关于数据结构的问题,我可以回答。以下是实现单链表存储输入数据的代码:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_node(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
# 输入数据
n = int(input("请输入整数个数:"))
linked_list = LinkedList()
while n != -1:
linked_list.add_node(n)
n = int(input("请输入整数:"))
```
希望这个代码能够帮到你。
请任意输入n个整数,直至-1结束,使用单链表存储输入的数据;
好的,这个问题我可以回答。以下是代码示例:
```python
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
head = Node()
cur = head
while True:
num = int(input())
if num == -1:
break
new_node = Node(num)
cur.next = new_node
cur = new_node
cur = head.next
while cur:
print(cur.val)
cur = cur.next
```
这段代码可以让用户输入任意个整数,以-1作为结束标志,然后使用单链表存储输入的数据,并输出链表中的所有元素。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)