使用c语言写入一组数据到栈并输出最大的数
时间: 2023-04-04 20:04:59 浏览: 54
可以使用以下代码实现:
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int stack[MAX_SIZE];
int top = -1;
int i, max;
// 将数据写入栈中
for (i = 0; i < MAX_SIZE; i++) {
printf("请输入第 %d 个数:", i + 1);
scanf("%d", &stack[++top]);
}
// 找出最大的数
max = stack[0];
for (i = 1; i <= top; i++) {
if (stack[i] > max) {
max = stack[i];
}
}
// 输出最大的数
printf("最大的数是:%d\n", max);
return 0;
}
相关问题
使用c语言写入一组数据到队列并输出最大的数
您好,我可以回答这个问题。以下是使用 C 语言写入一组数据到队列并输出最大的数的代码示例:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int queue[MAX_SIZE];
int front = 0, rear = 0;
int n, i, max;
printf("请输入数据个数:");
scanf("%d", &n);
printf("请输入数据:");
for (i = 0; i < n; i++) {
scanf("%d", &queue[rear]);
rear++;
}
max = queue[front];
for (i = front + 1; i < rear; i++) {
if (queue[i] > max) {
max = queue[i];
}
}
printf("最大的数是:%d\n", max);
return 0;
}
以上代码使用数组模拟队列,先输入数据个数和数据,再遍历数组找到最大的数并输出。希望能对您有所帮助。
使用c语言将一组数据写入单链表并输出最大的数
您好,我可以回答这个问题。以下是使用 C 语言将一组数据写入单链表并输出最大的数的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表结构体
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
// 将数组写入单链表
ListNode* createList(int* nums, int size) {
ListNode* head = NULL;
ListNode* tail = NULL;
for (int i = 0; i < size; i++) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->val = nums[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
// 输出单链表中的最大值
int getMax(ListNode* head) {
int max = head->val;
ListNode* p = head->next;
while (p != NULL) {
if (p->val > max) {
max = p->val;
}
p = p->next;
}
return max;
}
int main() {
int nums[] = {1, 3, 5, 7, 9};
int size = sizeof(nums) / sizeof(int);
ListNode* head = createList(nums, size);
int max = getMax(head);
printf("最大值为:%d\n", max);
return 0;
}
```
希望能帮到您!