📜  C语言中数组的优缺点

📅  最后修改于: 2021-05-17 05:46:49             🧑  作者: Mango

数组是相似类型元素的集合。例如,整数数组保存int类型的元素,而字符数组保存char类型的元素。下面是数组的表示形式:

虽然,数组有其自己的优点和缺点。

阵列的优势

下面是该数组的一些优点:

  • 在数组中,使用索引号可以很容易地访问元素。
  • 搜索过程可以轻松地应用于数组。
  • 2D数组用于表示矩阵。
  • 由于任何原因,用户希望存储相似类型的多个值,则可以有效地使用和利用Array。

阵列的缺点

现在让我们看一下数组的一些缺点以及如何克服它:

数组大小是固定的数组是静态的,这意味着其大小始终是固定的。分配给它的内存不能增加或减少。下面是相同的程序:

C
// C program to illustrate that the
// array size is fixed
#include 
  
// Driver Code
int main()
{
    int arr[10];
  
    // Assign values to array
    arr[0] = 5;
    arr[5] = 6;
    arr[7] = -9;
  
    // Print array element at index 0
    printf("Element at index 0"
           " is %d\n",
           arr[0]);
  
    // Print array element at index 11
    printf("Element at index 11"
           " is %d",
           arr[11]);
  
    return 0;
}


C
// C program to illustrate the use of
// array using Dynamic Memory Allocation
#include 
#include 
  
// Driver Code
int main()
{
    // Pointer will hold the base address
    int* ptr;
    int n = 10;
  
    // Dynamically allocates memory
    // using malloc() function
    ptr = (int*)malloc(n * sizeof(int));
  
    // Assign values to the array
    for (int i = 0; i < n; i++) {
        ptr[i] = i + 1;
    }
  
    // Print the array
    printf("The elements are: ");
  
    for (int i = 0; i < n; i++) {
        printf("%d ", ptr[i]);
    }
  
    // Free the dynamically
    // allocated memory
    free(ptr);
  
    return 0;
}


C
// C++ program to illustrate that
// the array is homogeneous
#include 
  
// Driver Code
int main()
{
    // Below declaration will give
    // Compilation Error
    int a[5] = { 0, 1, 2, "string", 9, 4.85 };
  
    return 0;
}


C
// C program to illustrate the use of
// structure to store heterogeneous
// variables
#include 
  
// Structure students
struct student {
  
    int student_id;
    float marks;
    char name[30];
};
  
// Driver Code
int main()
{
    // Structure variable s1
    struct student s1 = { 100, 547, "Ram" };
  
    // Accessing structure members
    // using structure pointer
    printf("%d\n", s1.student_id);
    printf("%f\n", s1.marks);
  
    for (int i = 0;
         s1.name[i] != '\0'; i++) {
        printf("%c", s1.name[i]);
    }
  
    return 0;
}


C
// C Program to insert an element at
// a specific position in an array
#include 
  
// Driver Code
int main()
{
    int arr[100] = { 0 };
    int i, x, pos, n = 10;
  
    // Initial array of size 10
    for (i = 0; i < 10; i++) {
        arr[i] = i + 1;
    }
  
    // Print the original array
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
  
    // Element to be inserted
    x = 50;
  
    // Position at which element
    // is to be inserted
    pos = 5;
  
    printf("Array after inserting %d"
           " at position %d\n",
           x, pos);
  
    // Increase the size by 1
    n++;
  
    // Shift elements forward
    for (i = n - 1; i >= pos; i--) {
  
        arr[i] = arr[i - 1];
    }
  
    // Insert x at pos
    arr[pos - 1] = x;
  
    // Print the updated array
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
  
    return 0;
}


C
// C program to insert an element at
// a position using linked list
#include 
#include 
  
// Structure for the linked list
struct node {
    int data;
    struct node* next;
};
  
// head Node
struct node* head;
  
// Function to insert any element
// at the end of the linked list
int insert_last(int k)
{
    struct node *ptr, *s;
    ptr = (struct node*)
        malloc(sizeof(struct node));
    ptr->data = k;
    ptr->next = NULL;
  
    // If head is NULL
    if (head == NULL) {
        head = ptr;
    }
  
    // Else
    else {
  
        s = head;
  
        // Traverse the LL to last
        while (s->next != NULL) {
            s = s->next;
        }
        s->next = ptr;
    }
}
  
// Function to display linked list
void display()
{
    struct node* s;
  
    // Store the head
    s = head;
  
    // Traverse till s is NULL
    while (s != NULL) {
  
        // Print the data
        printf("%d ", s->data);
        s = s->next;
    }
    printf("\n");
}
  
// Function to insert any element at
// the given position of linked list
int insert_position(int a, int b)
{
    int f = 0, i;
    struct node *ptr, *s;
  
    // Allocate new Node
    ptr = (struct node*)
        malloc(sizeof(struct node));
    ptr->data = a;
  
    // If first position
    if (b == 1) {
        ptr->next = head;
        head = ptr;
    }
  
    // Otherwise
    else {
        s = head;
  
        // Move to (b - 1) position
        for (i = 0; i < b - 2; i++) {
            s = s->next;
        }
  
        // Assign node
        ptr->next = s->next;
        s->next = ptr;
    }
  
    return 0;
}
  
// Driver Code
int main()
{
    // Given Linked List
    insert_last(3);
    insert_last(1);
    insert_last(5);
    insert_last(7);
  
    printf("Current Linked List is: ");
  
    // Display the LL
    display();
  
    // Insert 6 at position 4
    insert_position(6, 4);
    printf("\n Linked List after insert"
           " 6 in 4th position: ");
  
    // Display the LL
    display();
  
    return 0;
}


输出:
Element at index 0 is 5
Element at index 11 is -1176897384

说明在上述程序中,声明了大小为10的数组,并在特定索引处分配了该值。但是,当打印索引11处的值时,它会打印垃圾值,因为该数组是从绑定索引之外访问的。在某些编译器中,它给出的错误为“数组索引越界”。

如何克服要克服该问题,请使用诸如malloc()calloc()之类的动态内存分配。它还可以帮助我们使用free()释放内存 通过释放内存来帮助减少内存浪费的方法。以下是相同的程序:

C

// C program to illustrate the use of
// array using Dynamic Memory Allocation
#include 
#include 
  
// Driver Code
int main()
{
    // Pointer will hold the base address
    int* ptr;
    int n = 10;
  
    // Dynamically allocates memory
    // using malloc() function
    ptr = (int*)malloc(n * sizeof(int));
  
    // Assign values to the array
    for (int i = 0; i < n; i++) {
        ptr[i] = i + 1;
    }
  
    // Print the array
    printf("The elements are: ");
  
    for (int i = 0; i < n; i++) {
        printf("%d ", ptr[i]);
    }
  
    // Free the dynamically
    // allocated memory
    free(ptr);
  
    return 0;
}
输出:
The elements are: 1 2 3 4 5 6 7 8 9 10

数组是同类的数组是同类的,即,数组中只能存储一种类型的值。例如,如果数组类型为“ int ”,则只能存储整数元素,而不能允许其他类型的元素,例如double,float,char等。下面是相同的程序:

C

// C++ program to illustrate that
// the array is homogeneous
#include 
  
// Driver Code
int main()
{
    // Below declaration will give
    // Compilation Error
    int a[5] = { 0, 1, 2, "string", 9, 4.85 };
  
    return 0;
}
输出:

100
547.000000
Ram

输出:

说明由于将整数类型数组的值分配给字符串浮点类型,因此上述代码给出了“编译错误”。

如何克服要克服该问题,我们的想法是进行结构化,以在其中存储非均匀(异构)值。以下是相同的程序:

C

// C program to illustrate the use of
// structure to store heterogeneous
// variables
#include 
  
// Structure students
struct student {
  
    int student_id;
    float marks;
    char name[30];
};
  
// Driver Code
int main()
{
    // Structure variable s1
    struct student s1 = { 100, 547, "Ram" };
  
    // Accessing structure members
    // using structure pointer
    printf("%d\n", s1.student_id);
    printf("%f\n", s1.marks);
  
    for (int i = 0;
         s1.name[i] != '\0'; i++) {
        printf("%c", s1.name[i]);
    }
  
    return 0;
}
输出:
100
547.000000
Ram

数组是连续的内存块数组将数据存储在连续的内存位置中(一个接一个)。下面是相同的表示形式:

如何克服:为了克服对数组的顺序访问,其想法是使用Linked list 。在一个 链表元素不存储在连续的内存位置中。下面是相同的表示形式:

在数组中插入和删除并不容易:在数组中进行操作的插入和删除在插入或删除数组中的任何地方时都是有问题的,有必要遍历数组,然后根据操作移动其余元素。该操作成本更高。

示例:要在数组的第3个位置插入22 ,请执行以下步骤:

下面是说明相同内容的程序:

C

// C Program to insert an element at
// a specific position in an array
#include 
  
// Driver Code
int main()
{
    int arr[100] = { 0 };
    int i, x, pos, n = 10;
  
    // Initial array of size 10
    for (i = 0; i < 10; i++) {
        arr[i] = i + 1;
    }
  
    // Print the original array
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
  
    // Element to be inserted
    x = 50;
  
    // Position at which element
    // is to be inserted
    pos = 5;
  
    printf("Array after inserting %d"
           " at position %d\n",
           x, pos);
  
    // Increase the size by 1
    n++;
  
    // Shift elements forward
    for (i = n - 1; i >= pos; i--) {
  
        arr[i] = arr[i - 1];
    }
  
    // Insert x at pos
    arr[pos - 1] = x;
  
    // Print the updated array
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
  
    return 0;
}
输出:
1 2 3 4 5 6 7 8 9 10 
Array after inserting 50 at position 5
1 2 3 4 50 5 6 7 8 9 10

克服方法:使用链表克服上述问题下面是相同的表示形式:

下面是实现相同程序的程序:

C

// C program to insert an element at
// a position using linked list
#include 
#include 
  
// Structure for the linked list
struct node {
    int data;
    struct node* next;
};
  
// head Node
struct node* head;
  
// Function to insert any element
// at the end of the linked list
int insert_last(int k)
{
    struct node *ptr, *s;
    ptr = (struct node*)
        malloc(sizeof(struct node));
    ptr->data = k;
    ptr->next = NULL;
  
    // If head is NULL
    if (head == NULL) {
        head = ptr;
    }
  
    // Else
    else {
  
        s = head;
  
        // Traverse the LL to last
        while (s->next != NULL) {
            s = s->next;
        }
        s->next = ptr;
    }
}
  
// Function to display linked list
void display()
{
    struct node* s;
  
    // Store the head
    s = head;
  
    // Traverse till s is NULL
    while (s != NULL) {
  
        // Print the data
        printf("%d ", s->data);
        s = s->next;
    }
    printf("\n");
}
  
// Function to insert any element at
// the given position of linked list
int insert_position(int a, int b)
{
    int f = 0, i;
    struct node *ptr, *s;
  
    // Allocate new Node
    ptr = (struct node*)
        malloc(sizeof(struct node));
    ptr->data = a;
  
    // If first position
    if (b == 1) {
        ptr->next = head;
        head = ptr;
    }
  
    // Otherwise
    else {
        s = head;
  
        // Move to (b - 1) position
        for (i = 0; i < b - 2; i++) {
            s = s->next;
        }
  
        // Assign node
        ptr->next = s->next;
        s->next = ptr;
    }
  
    return 0;
}
  
// Driver Code
int main()
{
    // Given Linked List
    insert_last(3);
    insert_last(1);
    insert_last(5);
    insert_last(7);
  
    printf("Current Linked List is: ");
  
    // Display the LL
    display();
  
    // Insert 6 at position 4
    insert_position(6, 4);
    printf("\n Linked List after insert"
           " 6 in 4th position: ");
  
    // Display the LL
    display();
  
    return 0;
}
输出:
Current Linked List is: 3 1 5 7 

 Linked List after insert 6 in 4th position: 3 1 5 6 7