数组是相似类型元素的集合。例如,整数数组保存 int 类型的元素,而字符数组保存 char 类型的元素。下面是数组的表示:
但是,数组有其自身的优点和缺点。
数组的优点
以下是阵列的一些优点:
- 在数组中,通过使用索引号访问元素非常容易。
- 搜索过程可以很容易地应用于数组。
- 二维数组用于表示矩阵。
- 出于任何原因,用户希望存储多个相似类型的值,然后可以有效地使用和利用数组。
数组的缺点
现在让我们看看数组的一些缺点以及如何克服它:
数组大小是固定的:数组是静态的,这意味着它的大小总是固定的。分配给它的内存不能增加或减少。以下是相同的程序:
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
Array is homogeneous :数组是同构的,即只能在数组中存储一种类型的值。例如,如果数组类型为“ 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 。在一个 链表,元素不存储在连续的内存位置。下面是相同的表示:
Array中的插入和删除并不容易:对数组的插入和删除操作存在问题,因为在数组中的任何位置插入或删除,需要遍历数组,然后根据操作移动剩余元素。这个操作成本比较高。
示例:为了在数组的第三个位置插入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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。