寻找链表最大值C++代码实现
时间: 2023-01-29 19:06:00 浏览: 86
好的,这是一个在链表中寻找最大值的 C 代码实现。这里假设你已经有了一个结构体定义来表示链表中的每个结点,其中有一个整型值 val 来表示结点保存的数据,以及一个指向下一个结点的指针 next:
```
struct ListNode {
int val;
struct ListNode *next;
};
```
你可以使用如下的代码来找到链表中的最大值:
```
int findMax(struct ListNode *head) {
int max = INT_MIN; // 初始化为最小值
while (head != NULL) {
if (head->val > max) {
max = head->val;
}
head = head->next;
}
return max;
}
```
在这段代码中,我们遍历了整个链表,并且对于每个结点,都检查其值是否大于当前的最大值。如果是,就将最大值更新为该结点的值。最后,我们返回找到的最大值。
希望这对你有帮助!
相关问题
c++实现寻找单链表中最大值
可以使用以下代码实现单链表中最大值的查找:
```c++
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct ListNode {
int val;
ListNode* next;
ListNode(int x): val(x), next(nullptr) {}
};
int getMax(ListNode* head) {
if (head == nullptr) {
return INT_MIN; // 如果链表为空,则返回最小整数值
}
int maxVal = head->val;
ListNode* cur = head->next;
while (cur != nullptr) {
if (cur->val > maxVal) {
maxVal = cur->val;
}
cur = cur->next;
}
return maxVal;
}
int main() {
// 创建链表 1 -> 3 -> 5 -> 2 -> 4
ListNode* head = new ListNode(1);
head->next = new ListNode(3);
head->next->next = new ListNode(5);
head->next->next->next = new ListNode(2);
head->next->next->next->next = new ListNode(4);
// 查找链表中最大值
int maxVal = getMax(head);
cout << "The maximum value in the linked list is: " << maxVal << endl;
return 0;
}
```
输出结果为:
```
The maximum value in the linked list is: 5
```
c++哈希表不用类和结构体实现怎么代码实现
哈希表可以用数组和链表实现,具体实现方式如下:
1. 定义一个数组,数组的长度为哈希表中键值对的最大数量(一般为质数),数组中每个元素的初始值为 NULL。
2. 定义一个哈希函数,将键值通过该函数转换成数组下标。
3. 实现插入函数。首先使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否已经存在,如果存在,则更新其对应的值;如果不存在,则在链表头插入新节点,节点的值为键值对。
4. 实现查找函数。同样使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否存在,如果存在,则返回其对应的值;如果不存在,则返回空值。
5. 实现删除函数。同样使用哈希函数获取键对应的数组下标,然后在该下标对应的链表中查找键是否存在,如果存在,则删除该节点,否则不做任何处理。
下面是一个实现哈希表的示例代码(使用 C 语言实现):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 10007
#define KEY_SIZE 128
struct Node {
char key[KEY_SIZE];
char value[KEY_SIZE];
struct Node* next;
};
struct Node* hashtable[MAX_SIZE] = { NULL };
int hash(char* key) {
int sum = 0;
for (int i = 0; i < strlen(key); i++) {
sum += key[i];
}
return sum % MAX_SIZE;
}
void insert(char* key, char* value) {
int index = hash(key);
struct Node* p = hashtable[index];
while (p != NULL) {
if (strcmp(p->key, key) == 0) {
strcpy(p->value, value);
return;
}
p = p->next;
}
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
strcpy(node->key, key);
strcpy(node->value, value);
node->next = hashtable[index];
hashtable[index] = node;
}
char* find(char* key) {
int index = hash(key);
struct Node* p = hashtable[index];
while (p != NULL) {
if (strcmp(p->key, key) == 0) {
return p->value;
}
p = p->next;
}
return NULL;
}
void remove_key(char* key) {
int index = hash(key);
struct Node* p = hashtable[index];
if (p == NULL) {
return;
}
if (strcmp(p->key, key) == 0) {
hashtable[index] = p->next;
free(p);
return;
}
while (p->next != NULL) {
if (strcmp(p->next->key, key) == 0) {
struct Node* node = p->next;
p->next = node->next;
free(node);
return;
}
p = p->next;
}
}
int main() {
insert("hello", "world");
insert("foo", "bar");
printf("%s\n", find("hello")); // output: world
printf("%s\n", find("foo")); // output: bar
remove_key("foo");
printf("%s\n", find("foo")); // output: (null)
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)