How to Implement the LISTARRAY function in VC ++ in C Language (2)

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201206/136582.html


1. # ifndef _ LISTARRAY_H __
2. # define _ LISTARRAY_H __
3. # include "rtthread. h"
4. # include "finsh. h"
5.
6. # define LISTDEFAULTSIZE 20
7. # define LISTCHANGESIZE 10
8.
9. // LIST Array
10. typedef struct _ List;
11. struct _ List
12 .{
13. void ** pListPointArray; // LIST array pointer
14. int Total; // number of elements
15. int ChangeSize; // The capacity size when the capacity is changed each time
16. int Size; // current capacity
17. void (* Add) (List * pList, void * pListPoint); // Add
18. void (* Remove) (List * pList, void * pListPoint); // Remove
19. void (* Delete) (void * pList); // destructor
20 .};
21. // List class destructor
22. static void ListDelete (void * pList)
23 .{
24. rt_free (List *) pList)-> pListPointArray );
25. rt_free (pList); // release the entire List class.
26 .}
27. // Add a function to the element
28. static void ListAdd (List * pList, void * pListPoint)
29 .{
30. void ** tListPointArray;
31. if (pList-> Size = pList-> Total) // if the space is full
32 .{
33. pList-> Size = pList-> Size + pList-> ChangeSize; // you can change the Size of a space.
34. tListPointArray = rt_malloc (sizeof (int *) * pList-> Size); // re-apply for memory
35. memcpy (tListPointArray, pList-> pListPointArray, sizeof (int *) * pList-> Total); // copy data from the original memory to the new memory
36. rt_free (pList-> pListPointArray); // release the original space
37. pList-> pListPointArray = tListPointArray; // Replace the new space pointer with the original space pointer.
38 .}
39. pList-> pListPointArray [pList-> Total] = pListPoint; // place the added elements in the last storage unit.
40. pList-> Total ++; // Add 1 to the number
41 .}
42. // element Removal Function
43. static void ListRemove (List * pList, void * pListPoint)
44 .{
45. int pListIndex, tListIndex;
46. void ** tListPointArray;
47. if (pList-> Total = 0) return; // exit when the Total number is 0.
48. for (pListIndex = 0, tListIndex = 0; pListIndex <pList-> Total; pListIndex ++) // find the remove Point
49 .{
50. if (pList-> pListPointArray [pListIndex]! = PListPoint) // The current vertex is not a remove Vertex
51 .{
52. pList-> pListPointArray [tListIndex] = pList-> pListPointArray [pListIndex]; // copy
53. tListIndex ++; // Copy Sequence Number plus 1
54 .}
55 .}
56. pList-> Total = tListIndex;
57.
58. if (pList-> Total <= (pList-> Size-pList-> ChangeSize ))
59 .{
60. pList-> Size = pList-> Size-pList-> ChangeSize; // change the memory Size.
61. tListPointArray = rt_malloc (sizeof (int *) * pList-> Size); // apply for new memory
62. memcpy (tListPointArray, pList-> pListPointArray, sizeof (int *) * pList-> Total); // copy data
63. rt_free (pList-> pListPointArray); // release the original space
64. pList-> pListPointArray = tListPointArray; // Replace the new space pointer with the original space pointer.
65 .}
66 .}
67. // List constructor
68. static List * ListCreate (void)
69 .{
70. List * pList = (List *) rt_malloc (sizeof (List ));
71. pList-> Total = 0;
72. pList-> Size = LISTDEFAULTSIZE;
73. pList-> ChangeSize = LISTCHANGESIZE;
74. pList-> pListPointArray = rt_malloc (LISTDEFAULTSIZE );
75. pList-> Add = ListAdd;
76. pList-> Remove = ListRemove;
77. pList-> Delete = ListDelete;
78. return pList;
79 .}
80. # endif

This method is developed from method 1. Two parameters are added to method 1, one is the SIZE parameter, the other is the CHANGESIZE parameter, and the SIZE parameter is the array capacity during initialization, CHANGESIZE is the SIZE of the array that changes the array capacity when the data SIZE is reached. For example, the initial SIZE is SIZE, and when the array is increased to SIZE, you need to store the SIZE + 1 data, and then re-apply for a memory larger than the size changesize, then, transfer the data, release the original memory, and save the pointer of the new memory. Reduction is a truth. When it is reduced to SIZE, the original SIZE is reduced by CHANGESIZE, and then the memory is applied for, the data is transferred, the old memory is released, and the new pointer is saved.
The advantage of this method is that you do not need to apply for, release the memory, or transfer data each time. When LIST operations are frequent, this method is faster than the 1st methods!

 

Author: sx_wpc

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.