The LISTARRAY function in VC ++ is implemented in C Language (1)

Source: Internet
Author: User

1. # ifndef _ LISTARRAY_H __
2. # define _ LISTARRAY_H __
3. # include "rtthread. h"
4. # include "finsh. h"
5. // LIST Array
6. typedef struct _ ListArray;
7. struct _ ListArray
8 .{
9. void ** pListPointArray; // LIST array pointer
10. int Total; // number of elements
11. void (* Add) (ListArray * pList, void * pListPoint); // Add
12. void (* Remove) (ListArray * pList, void * pListPoint); // Remove
13. void (* Delete) (void * pList); // destructor
14 .};
15. // List class destructor
16. static void ListDelete (void * pList)
17 .{
18. if (ListArray *) pList)-> pListPointArray! = RT_NULL) // release the pointer array first
19 .{
20. rt_free (ListArray *) pList)-> pListPointArray );
21 .}
22. rt_free (pList); // release the entire List class.
23 .}
24. // Add a function to the element
25. static void ListAdd (ListArray * pList, void * pListPoint)
26 .{
27. void ** tListPointArray = rt_malloc (sizeof (int *) * (pList-> Total + 1); // apply for memory larger than the original storage unit
28. int pListIndex;
29. for (pListIndex = 0; pListIndex <pList-> Total; pListIndex ++) // copy
30 .{
31. if (pList-> pListPointArray [pListIndex] = pListPoint) // determines if the same element exists.
32 .{
33. rt_free (tListPointArray); // release the applied memory
34. return; // return
35 .}
36. tListPointArray [pListIndex] = pList-> pListPointArray [pListIndex]; // copy
37 .}
38. tListPointArray [pList-> Total] = pListPoint; // place the added elements in the last storage unit.
39. pList-> Total + = 1; // Add 1 to the Total number
40. if (pList-> pListPointArray! = RT_NULL) rt_free (pList-> pListPointArray); // release the original memory.
41. pList-> pListPointArray = tListPointArray; // replace the original handle with the new handle.
42 .}
43. // element Removal Function
44. static void ListRemove (ListArray * pList, void * pListPoint)
45 .{
46. int pListIndex, tListIndex;
47. void ** tListPointArray;
48. void ** FreePointArray;
49. void ** SavePointArray;
50. if (pList-> Total = 0) return; // exit when the Total number is 0.
51. tListPointArray = rt_malloc (sizeof (int) * (pList-> Total-1); // apply for memory smaller than the original storage unit
52. FreePointArray = tListPointArray; // use the newly applied memory space as the default release space.
53. SavePointArray = pList-> pListPointArray; // use the existing memory space as the default storage space
54. for (pListIndex = 0, tListIndex = 0; pListIndex <pList-> Total; pListIndex ++) // find the remove Point
55 .{
56. if (pList-> pListPointArray [pListIndex] = pListPoint) // The current vertex is the remove vertex.
57 .{
58. FreePointArray = pList-> pListPointArray; // change the memory release pointer.
59. SavePointArray = tListPointArray; // Changes the reserved memory pointer.
60. continue; // end this loop
61 .}
62. if (tListIndex <(pList-> Total-1) // if the current vertex is not a removal vertex, the copy sequence number is less than or equal to 1
63 .{
64. tListPointArray [tListIndex] = pList-> pListPointArray [pListIndex]; // copy
65. tListIndex ++; // Copy Sequence Number plus 1
66 .}
67 .}
68. pList-> Total = (SavePointArray = tListPointArray )? PList-> Total-1: pList-> Total; // change the Total number of reserved memory blocks.
69. if (FreePointArray! = RT_NULL) rt_free (FreePointArray); // release unused memory blocks.
70. pList-> pListPointArray = SavePointArray; // Reserved
71 .}
72. // List constructor
73. static ListArray * ListCreate (void)
74 .{
75. ListArray * pList = (ListArray *) rt_malloc (sizeof (ListArray ));
76. pList-> Total = 0;
77. pList-> pListPointArray = RT_NULL;
78. pList-> Add = ListAdd;
79. pList-> Remove = ListRemove;
80. pList-> Delete = ListDelete;
81. return pList;
82 .}
83. # endif

This method re-applies for a memory of 1 or 1 when adding or deleting elements in the array, and then copies the original array to the newly applied memory, replace the original array pointer!
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.