Data structure Basics (1)--array C language implementation--dynamic memory allocation

Source: Internet
Author: User

Data structure Basics (1)--array C language implementation--dynamic memory allocation

Basic idea: An array is the most commonly used data structure that is stored continuously in memory, can be statically initialized (int a[2]={1,2}), and can be dynamically initialized with malloc ().

The difficulty is that when the array is deleted or inserted, the coordinates of the element are not determined. Law:

1. If you want to insert an element in the array pos position (should be moved from the back)

for (i=cnu;i>=pos;i--)

PBASE[I]=PBASE[I-1];

2. Delete the element at POS position of the array

for (i=pos+1;i<=cnu;i--)

PBASE[I-2]=PBASE[I-1];

Dynamically allocating memory using malloc and assigning the return value to the shaping pointer

int *pbase= (int *) malloc (sizeof (int) *len);//Allocate 4*len byte-length memory

This is pbase can point to the first element in the array and can be used as an array variable name.

Advantages and disadvantages of arrays:

Advantages:

Fast access speed O (1) The memory location can be found directly according to the subscript

Disadvantages:

The length of the array must be known beforehand

Insert Delete element is slow

Space is usually limited.

Chunks of contiguous blocks of memory are required

Insertion of deleted elements is inefficient


[CPP]View Plain Copy
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdbool.h>
  4. struct arr{
  5. The maximum number of elements that an int len;//array can access
  6. int cnu;//The current number of elements in the array
  7. int * pbase;//stores pointers to arrays
  8. };
  9. /**
  10. * Initialize Array
  11. */
  12. void Init_array (struct ARR * Parray,int len) {
  13. parray->pbase= (int *) malloc (sizeof (int) *len);//Allocate 4*len byte-length memory
  14. if (null== parray->pbase)//Determine if memory allocation failed
  15. {
  16. printf ("Dynamically allocating memory failed \ n");
  17. Exit (-1);
  18. }else{
  19. parray->len=len;
  20. parray->cnu=0;
  21. }
  22. return;
  23. }
  24. /**
  25. * Determine if the array is empty, address the memory 4 bytes, transfer structure variables need to be copied, 12 bytes
  26. */
  27. BOOL IsEmpty (struct ARR * parray) {
  28. if (0==PARRAY->CNU)
  29. {
  30. return true;
  31. }else{
  32. return false;
  33. }
  34. }
  35. /**
  36. * * Determine if the array is full
  37. */
  38. BOOL Isfull (struct ARR * parray)
  39. {
  40. if (PARRAY->LEN==PARRAY->CNU)
  41. {
  42. return true;
  43. }else {
  44. return false;
  45. }
  46. }
  47. /**
  48. * Display array Contents
  49. */
  50. void Show_array (struct ARR * parray) {
  51. if (IsEmpty (Parray))
  52. printf ("The array is empty! \ n ");
  53. else{
  54. int i;
  55. for (i=0; i<parray->cnu;i++)
  56. {
  57. printf ("%d \ n", Parray->pbase[i]);
  58. }
  59. printf ("------------------------------------\ n");
  60. }
  61. }
  62. /**
  63. * * Append an element to an array
  64. */
  65. BOOL Append (struct ARR * Parray,int val) {
  66. if (Isfull (Parray))
  67. {
  68. printf ("The array is full!" \ n ");
  69. return false;
  70. }else{
  71. parray->pbase[parray->cnu]=val;
  72. parray->cnu++;
  73. }
  74. }
  75. /**
  76. * * Inserts an element into an array, POS is the number of positions in a group, pos=3 is inserting an element into the a[2]
  77. */
  78. BOOL Insert (struct ARR * parray,int pos,int val)
  79. {
  80. if (pos<1| | POS>PARRAY->LEN+1)//inserted position cannot be less than 1, and cannot be more than the last element sophomore
  81. {
  82. printf ("An illegal entry in the inserted position \ n");
  83. return false;
  84. }
  85. if (Isfull (Parray))
  86. {
  87. printf ("The array is full, the insertion failed!") \ n ");
  88. return false;
  89. }
  90. int i;
  91. Loop moves the array that starts the POS position back
  92. for (i=parray->cnu;i>=pos;i--)
  93. The moving range is from the first POS to the end of CNU
  94. {
  95. parray->pbase[i]=parray->pbase[i-1];
  96. /**
  97. If I represents the position of the element to move, from the beginning. Right is i-1, if left, left is i-2, right shift, left is I
  98. */
  99. }
  100. parray->pbase[pos-1]=val;
  101. parray->cnu++;
  102. parray->len++;
  103. return true;
  104. }
  105. /**
  106. * * Delete the first POS element in the array while returning the value of the deleted element
  107. */
  108. BOOL Delete (struct ARR * parray,int Pos,int * val)
  109. {
  110. if (pos<1| | POS>PARRAY->CNU)
  111. {
  112. printf ("Delete failed, location not valid \ n");
  113. return false;
  114. }
  115. int i;
  116. *val=parray->pbase[pos-1];
  117. for (i=pos+1;i<=parray->cnu;i++)
  118. {
  119. Moving units are from pos+1 to CNU
  120. parray->pbase[i-2]=parray->pbase[i-1];
  121. }
  122. parray->cnu--;
  123. return true;
  124. }
  125. /**
  126. * * Array Inversion
  127. */
  128. bool Inverse (struct ARR * parray)
  129. {
  130. if (IsEmpty (Parray))
  131. {
  132. printf ("Inversion fails, factor group is empty");
  133. return false;
  134. }
  135. int i=0;
  136. int j=parray->cnu-1;
  137. int temp;
  138. while (I<J)
  139. {
  140. temp=parray->pbase[i];
  141. parray->pbase[i]= parray->pbase[j];
  142. parray->pbase[j]=temp;
  143. i++;
  144. j--;
  145. }
  146. return true;
  147. }
  148. int main ()
  149. {
  150. struct ARR arr;
  151. Init_array (&arr,6);//The address of the struct is used as an argument in order to modify the value in the struct, and if a struct variable is passed, it will be copied without changing the value
  152. Append (&arr,1);
  153. Append (&arr,2);
  154. Append (&arr,3);
  155. Append (&arr,4);
  156. Show_array (&arr);
  157. Insert (&arr,2,88);
  158. Show_array (&arr);
  159. int Val;
  160. Delete (&arr,1,&val);
  161. Show_array (&arr);
  162. printf ("Removed%d\n", Val);
  163. Inverse (&arr);
  164. Show_array (&arr);
  165. return 0;
  166. }

Data structure Basics (1)--array C language implementation--dynamic memory allocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.