C语言如何输入长度未知的数组
C语言如何输入长度未知的数组
C语言输入长度未知的数组的方法主要有:动态内存分配、链表结构、使用定长数组再扩展。其中,动态内存分配是最常见的方法,接下来将详细描述这种方法。
一、动态内存分配
动态内存分配是处理长度未知数组的常用方法。通过使用C语言的malloc
和realloc
函数,可以在运行时根据需要分配和调整内存大小。以下是具体步骤和实现代码。
1.1 动态内存分配的基本概念
动态内存分配允许程序在运行时分配和释放内存。这对于处理不确定长度的数据非常有用。malloc
函数用于分配指定大小的内存,realloc
函数用于调整已分配内存的大小,而free
函数用于释放内存。
1.2 示例代码
以下是一个简单的示例,演示如何使用动态内存分配来输入长度未知的数组:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array = NULL;
int size = 0, capacity = 10, input;
// 初始分配内存
array = (int *)malloc(capacity * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter integers (enter -1 to end):\n");
while (1) {
scanf("%d", &input);
if (input == -1) {
break;
}
// 检查是否需要扩展数组
if (size == capacity) {
capacity *= 2;
array = (int *)realloc(array, capacity * sizeof(int));
if (array == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
}
array[size++] = input;
}
printf("You entered:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
// 释放内存
free(array);
return 0;
}
在这个示例中,我们首先分配了一个初始大小的数组,然后在用户输入数据时动态调整数组大小。如果数组已满,我们将其容量加倍,并使用realloc
函数重新分配内存。
二、链表结构
链表是一种灵活的动态数据结构,适合处理长度未知的数组。链表中的每个节点包含数据和指向下一个节点的指针。以下是链表的基本概念和实现方法。
2.1 链表的基本概念
链表由一系列节点组成,每个节点包含两部分:数据部分和指针部分。指针部分指向链表中的下一个节点。链表可以方便地插入和删除元素,但访问速度较慢,因为需要逐个遍历节点。
2.2 示例代码
以下是一个简单的链表实现,演示如何使用链表来输入长度未知的数组:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node* createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void append(Node **head, int data) {
Node *newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(Node *head) {
Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void freeList(Node *head) {
Node *temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
Node *head = NULL;
int input;
printf("Enter integers (enter -1 to end):\n");
while (1) {
scanf("%d", &input);
if (input == -1) {
break;
}
append(&head, input);
}
printf("You entered:\n");
printList(head);
freeList(head);
return 0;
}
在这个示例中,我们首先定义了一个链表节点结构,然后实现了创建节点、追加节点和打印链表的函数。用户输入数据时,我们将数据追加到链表的末尾,最终打印链表中的所有数据。
三、使用定长数组再扩展
这种方法是先使用一个定长数组存储数据,当数组满时,再创建一个更大的数组,将旧数组的数据复制到新数组中。这种方法相对简单,但效率较低。
3.1 定长数组再扩展的基本概念
定长数组再扩展的方法是先分配一个固定大小的数组,当数组满时,分配一个更大的数组,将旧数组的数据复制到新数组中。这种方法适合数据量不大的情况。
3.2 示例代码
以下是一个示例,演示如何使用定长数组再扩展的方法来输入长度未知的数组:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array = NULL;
int size = 0, capacity = 10, input;
// 初始分配内存
array = (int *)malloc(capacity * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter integers (enter -1 to end):\n");
while (1) {
scanf("%d", &input);
if (input == -1) {
break;
}
// 检查是否需要扩展数组
if (size == capacity) {
capacity *= 2;
int *newArray = (int *)malloc(capacity * sizeof(int));
if (newArray == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < size; i++) {
newArray[i] = array[i];
}
free(array);
array = newArray;
}
array[size++] = input;
}
printf("You entered:\n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
// 释放内存
free(array);
return 0;
}
在这个示例中,我们首先分配了一个初始大小的数组,然后在用户输入数据时动态调整数组大小。如果数组已满,我们分配一个更大的数组,并将旧数组的数据复制到新数组中。
结论
处理长度未知的数组在C语言中有多种方法,其中动态内存分配是最常见的方法,适用于大多数情况。链表结构和定长数组再扩展的方法也各有优点,可以根据具体需求选择合适的方法。