System and method for automatic stack growth processing
By using multiple arrays of equal length in the stack to achieve automatic growth, the shortcomings of linked stacks and sequential stacks in terms of storage efficiency are solved, realizing dynamic growth and efficient storage, which is suitable for quantum networks and computer fields.
Patent Information
- Application Number
- CN202111673479.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-31
- Publication Date
- 2026-02-13
- Estimated Expiration
- 2041-12-31
AI Technical Summary
In existing technologies, linked stacks and sequential stacks have shortcomings in storage efficiency. Linked stacks require additional pointer space and have high lookup complexity, while sequential stacks are prone to overflow when the capacity is fixed, and frequent expansion and contraction lead to low efficiency.
An automatic stack growth system and method are adopted. Through a structure class, an array creation module, and push and pop modules, multiple arrays of equal length are used to realize the dynamic growth of stack elements, avoiding frequent expansion and contraction, and reducing pointer space overhead and time complexity.
It implements automatic stack growth, improves storage efficiency, reduces time and space waste, and is suitable for quantum networks and computer fields.
Smart Images

Figure CN114595056B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of data structure in quantum network and computer, in particular to a system and method for automatic growth of stack. BACKGROUND
[0002] There are two storage methods for stack, which are linked stack and sequential stack. The linked stack is implemented by using single linked list. Since the linked list is dynamically allocated space, the storage capacity of the stack is variable, but an additional pointer space is needed for each stack element to store the pointer field, resulting in structural overhead. Moreover, the time complexity of querying the element at a specified position in the linked stack is O(n). The sequential stack is implemented by using array. Since the length of the array needs to be declared when it is initialized, the storage capacity of the stack is fixed, and stack overflow may occur. When using array to store stack elements, the array needs to be expanded or shrunk in capacity. Assuming that the length of the original array is len, when the memory is insufficient, a new array with a size of 2*len is created; when the memory usage is less than 1 / 4, a new array with a size of 1 / 2*len is created, and the original array is copied to the new array, and the space of the original array is released. This method needs to frequently apply for new arrays and destroy the original arrays, and it also needs to spend O(n) time to copy the original array to the new array, which not only wastes memory in space, but also spends a lot of time in time, resulting in low storage efficiency of the stack.
[0003] Therefore, it is necessary to improve the existing technology of quantum network and computer, and propose a method for solving the automatic growth of stack and improving the storage efficiency of stack. SUMMARY
[0004] The purpose of the present application is to overcome the defects of the prior art, to solve the problems of stack growth and storage efficiency, and to propose a system and method for automatic growth of stack.
[0005] The method of the present application is implemented by the following technical scheme:
[0006] A system for automatic growth of stack, comprising a structure class establishment module, an array establishment module, a stack-out module and a stack-in module, wherein:
[0007] The structure class establishment module is used to establish a structure class of a general stack, and set the member variables of the structure class;
[0008] The array establishment module is used to construct the constructor of the structure class and create a first array;
[0009] The push function is used to calculate the value index1 of the position of the last array of the current stack tail element, and when the value of index1 is not equal to 0, the element A is pushed into the stack,
[0010] When the value of index1 is equal to 0, the second array is established by the array establishment module;
[0011] The pop function is used to calculate the value index2 of the position of the last array of the current stack tail element B, and when the value of index2 is not equal to 1, the stack element B is popped out;
[0012] When the value of index2 is equal to 1, the stack element B is popped out, the array pointed by the last array address *lastArray is released, and *lastArray points to the second last array.
[0013] An automatic growth processing method of a stack, and specific steps of the method are as follows:
[0014] Step 1: establishing a structure class of a general stack and setting member variables of the structure class;
[0015] The member variables include a first array address pointer *firstArray for storing stack elements, a last array address pointer *lastArray for storing stack elements, the length of each array arrayLen, the number of current arrays arrayNum, and the size of the current stack element size;
[0016] Step 2: constructing a constructor of the structure class and newly building a first array, the first array is named as newArray1, and the address of the first array is stored into the first array address pointer *firstArray and the last array address pointer *lastArray respectively;
[0017] Step 3: constructing a push function and a pop function for the structure class;
[0018] Step 4: when an arbitrary element A is pushed into the stack, the push function is used to calculate the value index1 of the position of the last array of the current stack tail element, and the calculation formula is:
[0019] Index1 = size%(arrayLen-1),
[0020] When the value of index1 is not equal to 0, the element A is pushed into the stack, and the value of size is increased by 1;
[0021] When the value of index1 is equal to 0, a second array newArray2 is established and the process jumps to step 5;
[0022] Step 5: The address pointer *lastArray removes the array currently pointed to and points to the address of the second array, the value of the array number arrayNum is increased by 1, the value of size is increased by 1, and the first element of the second array is assigned to the value of the stack element A;
[0023] Step 6: When the stack tail element B is popped, the pop function is used to calculate the value of the position of the last array of the current stack tail element B, and the calculation formula is:
[0024] Index2 = size % (arrayLen - 1),
[0025] When the value of index2 is not equal to 1, the stack element B is popped, and the value of size is reduced by 1;
[0026] When the value of index2 is equal to 1, the stack element B is popped, the array pointed to by the address pointer *lastArray is released, and *lastArray points to the second last array.
[0027] Further, the position of the element in step 4 after being pushed into the stack is represented as: *item = newArray2[index].
[0028] Further, the structure further includes a query function findByRule, a sorting function sortByRule, and a display function showAll.
[0029] Further, the query step of the query function findByRule is:
[0030] Step A: Set a condition parameter *rule, which is a function pointer, and the function pointed to by *rule is the rule function rule;
[0031] Step B: Set a currentArray loop array, and assign the address of the first array to currentArray, i.e. *currentArray = *firstArray;
[0032] Step C: Set a loop variable i = 0, start the loop and judge whether i is less than size;
[0033] If the value of i is greater than or equal to the value of size, return an empty value and end the loop;
[0034] If the value of i is less than the value of size, then determine whether the current element meets the rule of the rule function rule, and if it does, return the current element;
[0035] If it does not, then determine whether the current element is the last stack element in the current array;
[0036] If the current element is the last stack element in the current array, then change the array of the current loop to the next array, and continue the loop;
[0037] If the element of the current loop is not the last stack element in the current array, continue the loop;
[0038] The basis for determining whether the element of the current loop is the last stack element in the current array is that if i%(arrayLen-1)==arrayLen-2, then the element of the current loop is the last stack element in the current array;
[0039] Step D: When the value of i is greater than the value of size, it indicates that no element meeting the rule of the rule function rule is found, and an empty value is returned, and the loop ends.
[0040] Further, the sorting step of the sorting function sortByRule is:
[0041] Step a: Establish a third array;
[0042] Step b: Traverse all elements of the structure and insert the stack elements into the third array in turn according to the sorting rule;
[0043] Step c: After the traversal ends, the sorting of the third array is completed.
[0044] Further, the length of the third array is the size of the current stack.
[0045] Further, the sorting rule includes ascending sorting and descending sorting.
[0046] Further, the display function showAll uses a for loop statement to traverse all stack elements of the structure class.
[0047] The present application has the following beneficial effects:
[0048] The application discloses a kind of automatic growth processing method of stack, utilize multiple equal-length arrays to store stack element.When the capacity of array is insufficient, a equal-length array is newly built, the address pointer of newly built array is stored in the last element of array, and the element newly stacked is stored in new array;When array (except the first array) has no stack element, the array is released, to prevent space memory occupation and waste.The method realizes the dynamic growth effect of array, i.e.the automatic growth function of stack, improves the storage efficiency of stack, and can be widely applied in quantum network and computer field. BRIEF DESCRIPTION OF DRAWINGS
[0049] Figure 1 It is the principle block diagram of the automatic growth processing system of stack of the application;
[0050] Figure 2 It is the step flow chart of the automatic growth processing method of stack of the application;
[0051] Figure 3 It is the in-stack flow chart of the automatic growth processing method of stack of the application;
[0052] Figure 4 It is the out-stack flow chart of the automatic growth processing method of stack of the application. DETAILED DESCRIPTION
[0053] In order to make the purpose, technical scheme and advantages of the application more clear and obvious, the application is further described in detail in combination with embodiments, but the scope of protection of the application is not limited to the following specific embodiments.
[0054] As shown in Figure 1 An automatic growth processing system of stack, comprising structure class establishing module, array establishing module, out-stack module and in-stack module, wherein:
[0055] The structure class establishing module is used to establish the structure class of a general stack, and set the member variable of structure class;
[0056] The array establishing module is used to construct the constructor of the structure class and newly build the first array;
[0057] The in-stack module is used for in-stack operation, and the in-stack function push is used to calculate the value index1 of the position of current stack tail element in the last array;When the value of index1 is not equal to 0, then the element A is stacked,
[0058] When the value of index1 is equal to 0, the second array is established through the array establishing module;
[0059] The pop-out module is used for pop-out operation, and a pop-out function pop is used to calculate the value Index2 of the position of the tail element B in the last array, and when the value of index2 is not equal to 1, the stack element B is popped out;
[0060] When the value of index2 is equal to 1, the stack element B is popped out, the array pointed by the last array address *lastArray is released, and *lastArray points to the second last array.
[0061] The specific implementation scheme of each module in the system is consistent with the method adopted by the corresponding part of the following automatic growth processing method of a stack, and will not be repeated here.
[0062] As shown in the following automatic growth processing method of a stack, the specific steps of the method are as follows: Figures 2-4
[0063] Step 1: Establish a structure class of a general stack, and set the member variables of the structure class;
[0064] The member variables include a first array address pointer *firstArray for storing stack elements, a last array address pointer *lastArray for storing stack elements, the length of each array arrayLen, the number of current arrays arrayNum, and the size of current stack elements size.
[0065] Wherein, *firstArray is an array address pointer, which points to an array for storing all stack elements, and each element in the firstArray array is a second-level pointer, which points to the address of a stack element. The actual type of the stack element can be any type, such as integer, character, boolean, pointer, etc. According to the demand, one of them is taken, and the type is declared by using template. The type of the stack element is consistent, that is, the stack cannot store both integers and booleans.
[0066] Step 2: Construct the constructor of the structure class and create a first array, and the first array is named as newArray1. The address of the first array is stored into the first array address pointer *firstArray and the last array address pointer *lastArray respectively; that is, *firstArray and *lastArray point to the same address, which represents that there is only one array at present.
[0067] In step 2, when the first array is created, the array length arrayLen is len, the initial number of arrays arrayNum is 1, and the size of the current stack element size is 0.
[0068] Step 3: build a push function and a pop function for the structure;
[0069] The push function includes a stack element parameter, named *item, and the type is pointer.
[0070] When an element is pushed, step 4 is as shown in the following figure: Figure 1 When any element A is pushed, the push function is used to calculate the value index1 of the position of the last element in the last array, and the calculation formula is:
[0071] Index1 = size%(arrayLen-1),
[0072] When the value of index1 is not equal to 0 or the size of the current stack element size is equal to 0, it proves that there is still space in the last array to store the pushed element, so the pushed element A is directly stored in the index+1 position of the last array, that is, [index]=*item, and the size of the stack size is increased by 1;
[0073] When the value of index1 is equal to 0, it proves that there is no space in the last array to store the pushed element, a new array needs to be created, and the element should be stored in the first position of the new array, so the second array newArray2 is established and step 5 is jumped to;
[0074] Step 5: the address pointer *lastArray removes the array currently pointed to and points to the address of the second array, the value of the number of arrays arrayNum is increased by 1, the size value is increased by 1, and the pushed element A is assigned to the first element of the second array, and the position of the pushed element A is represented as: *item=newArray2[index]; at this time, the size of the stack size is increased by 1;
[0075] When an element is popped, step 6 is as shown in the following figure: Figure 1 Step 6: when the tail element B is popped, the pop function is used to calculate the value Index2 of the position of the last element B in the last array, and the calculation formula is:
[0076] Index2 = size%(arrayLen-1),
[0077] When the value of index2 is not equal to 1, it proves that there is no need to release the space of the last array, so the stack element B is popped, and the size of the stack element size is changed, that is, the value of size is reduced by 1;
[0078] When the value of index2 is equal to 1, it proves that the element is in the first position of the last array, after the stack element B is out of stack, the array pointed by the lastArray is released and the lastArray points to the second last array;
[0079] The size of the stack element is changed, i.e., the value of-size is reduced by 1;
[0080] In the method, the structure further comprises a query function findByRule, a sorting function sortByRule and a display function showAll.
[0081] The query step of the structure class query function findByRule is as follows:
[0082] Step A: a condition parameter *rule is set, the *rule is a function pointer, and the function pointed by the *rule is a rule function rule;
[0083] Step B: a currentArray loop array is set, and the address of the first array is assigned to the currentArray, i.e., *currentArray = *firstArray;
[0084] Step C: a loop variable i = 0 is set, the loop starts and it is judged whether i is less than size;
[0085] If the value of i is greater than or equal to the value of size, an empty value is returned, and the loop ends;
[0086] If the value of i is less than the value of size, it is judged whether the current element meets the rule of the rule function rule, and if it meets, the current element is returned;
[0087] If it does not meet, it is judged whether the current element is the last stack element in the current array;
[0088] If the current element is the last stack element in the current array, the array of the current loop is changed to the next array, and the loop continues;
[0089] If the current loop element is not the last stack element in the current array, the loop continues;
[0090] The basis for judging whether the current loop element is the last stack element in the current array is that if i%(arrayLen-1)==arrayLen-2, the current loop element is the last stack element in the current array;
[0091] Step D: when the value of i is greater than the value of size, it means that no element of the rule that meets the rule function rule is found, and an empty value is returned, and the loop ends.
[0092] The array of the current loop is the next array, and the loop continues.
[0093] If the element of the current loop is not the last stack element of the current array, the loop continues.
[0094] The basis for determining whether the element of the current loop is the last stack element in the current array is: if i%(arrayLen-1)==arrayLen-2, the element of the current loop is the last stack element of the current array.
[0095] Step D: when the value of i is less than the value of size, an empty value is returned, and the loop ends.
[0096] The sorting step of the sorting function sortByRule is:
[0097] Step a: establish a third array; the length of the third array is the size of the current stack
[0098] Step b: traverse all elements of the structure and insert the stack elements into the third array according to the sorting rule; wherein the sorting rule includes ascending sorting and descending sorting.
[0099] Step c: after the traversal is completed, the sorting of the third array is completed.
[0100] The display function showAll of the structure traverses and searches for the stack elements, because there are multiple arrays in the stack structure, and the valid elements of each array need to be traversed, so the function showAll uses a for loop statement to traverse all stack elements of the structure.
[0101] The method of the application utilizes the establishment and destruction of arrays to realize the automatic growth of the stack, so that no additional pointer space needs to be allocated for each stack element, and no excessive structural overhead is generated. In terms of automatic growth, unlike the existing technology expansion / shrinking method, a new array does not need to be created, and the original array does not need to be copied into the new array one by one, and the time complexity changes from O(n) to O(1) without going through O(n) time; at the same time, the original array still exists and is utilized, and does not need to be frequently destroyed, so that the effect is significantly improved in terms of space and time, and the method can be widely applied in the field of quantum networks and computers.
[0102] Those skilled in the art can make various modifications and changes to the above embodiments according to the disclosure and teachings herein. Therefore, the application is not limited to the specific embodiments disclosed and described above, and some modifications and changes to the application shall fall within the protection scope of the claims of the application. In addition, although some specific terms are used in the specification, these terms are only for convenience of description and do not constitute any limitation on the application.
Claims
1. A system for automatic growth handling of a stack, characterized by The stack automatic growth processing system comprises a structure class establishing module, an array establishing module, a stack-out module and a stack-in module, wherein: The structure class establishing module is used for establishing a structure class of a general stack and setting member variables of the structure class; The member variables comprise a first array address pointer *firstArray for storing stack elements, a last array address pointer *lastArray for storing stack elements, a length arrayLen of each array, a number arrayNum of current arrays, and a size size of current stack elements; The array establishing module is used for constructing a constructor of the structure class and newly establishing a first array; The stack-in module is used for stack-in operation, and a stack-in function push is used to calculate a value index1 of a position of a current stack tail element in a last array, and the calculation formula is: Index1 = size % (arrayLen - 1), When the value of the index1 is not equal to 0, an arbitrary stack-in element A is stacked in; When the value of the index1 is equal to 0, a second array is established by the array establishing module, a currently pointed array of the last array address pointer *lastArray is removed and is pointed to the second array address, the value of the number arrayNum of the arrays is added by 1, the value of the size is added by 1, and the stack-in element A is assigned to a first element of the second array; The second array is equal in length to the first array; The stack-out module is used for stack-out operation, and a stack-out function pop is used to calculate a value Index2 of a position of a current stack tail element B in a last array, and the calculation formula is: Index2 = size % (arrayLen - 1), When the value of the index2 is not equal to 1, the current stack tail element B is stacked out; When the value of the index2 is equal to 1, the current stack tail element B is stacked out, an array pointed to by the last array address *lastArray is released, and *lastArray is pointed to a second last array.
2. A method of automatically growing a stack, characterized by, The application of the stack automatic growth processing system as claimed in claim 1 is as follows: Step 1: establishing a structure class of a general stack and setting member variables of the structure class; The member variables comprise a first array address pointer *firstArray for storing stack elements, a last array address pointer *lastArray for storing stack elements, a length arrayLen of each array, a number arrayNum of current arrays, and a size size of current stack elements; Step 2: constructing a constructor of the structure class and newly establishing a first array, the first array is named as newArray1, and the address of the first array is stored into the first array address pointer *firstArray and the last array address pointer *lastArray; Step 3: constructing a stack-in function push and a stack-out function pop for the structure class; Step 4: when an arbitrary element A is stacked in, the stack-in function push is used to calculate a value index1 of a position of a current stack tail element in a last array, and the calculation formula is: Index1 = size %(arrayLen - 1), When the value of index1 is not equal to 0, the element A is pushed into the stack, and the value of size is added by 1. When the value of index1 is equal to 0, a second array newArray2 is created, and the process jumps to step 5, and the second array has the same length as the first array. Step 5: The address pointer *lastArray removes the array currently pointed to and points to the address of the second array, the value of arrayNum is added by 1, the value of size is added by 1, and the pushed element A is assigned to the first element of the second array. Step 6: When the stack tail element B is popped, the pop function is used to calculate the value of index2 of the position of the last array of the current stack tail element B, and the calculation formula is: Index2 = size %(arrayLen - 1), When the value of index2 is not equal to 1, the stack tail element B is popped, and the value of size is reduced by 1. When the value of index2 is equal to 1, the stack tail element B is popped, the array pointed to by the address pointer *lastArray is released, and *lastArray points to the second last array.
3. The method of claim 2, wherein, In step 2, when the first array is created, the array length arrayLen is len, the initial array number arrayNum is 1, and the size of the current stack element size is 0.
4. The method of claim 3, wherein, The push function includes a stack element parameter, and the parameter is named *item and is a pointer type.
5. The method of claim 4, wherein, The position of the element pushed in step 4 is represented as: *item = newArray2[index].
6. The method of claim 2, wherein, The structure class also includes a query function findByRule, a sorting function sortByRule, and a display function showAll.
7. The method of claim 6, wherein, The query steps of the query function findByRule are as follows: Step A: Set a condition parameter *rule, which is a function pointer, and the function pointed to by *rule is the rule function rule. Step B: Set a currentArray loop array, and assign the address of the first array to currentArray, that is, *currentArray = *firstArray. Step C: Set a loop variable i = 0, start the loop, and judge whether i is less than size. If the value of i is greater than or equal to the value of size, return an empty value, and end the loop. If the value of i is less than the value of size, judge whether the current element meets the rules of the rule function rule. If it meets the rules, return the current element. If it does not meet the rules, judge whether the current element is the last stack element in the current array. If the current element is the last stack element in the current array, change the current loop array to the next array, and continue the loop. If the current loop element is not the last stack element in the current array, continue the loop. The basis for judging whether the element of the current loop is the last stack element in the current array is that if i%(arrayLen-1)==arrayLen-2, the element of the current loop is the last stack element in the current array; Step D: when the value of i is greater than the value of size, it indicates that no element meeting the rule function rule is found, and an empty value is returned, and the loop ends.
8. The method of claim 6, wherein the stack is automatically increased by a predetermined size. The sorting step of the sorting function sortByRule is: Step a: establishing a third array; Step b: traversing all elements of the structure and inserting the stack elements into the third array according to the sorting rule; Step c: after the traversal ends, the sorting of the third array is completed.
9. The method of claim 8, wherein, The length of the third array is the size of the current stack. The sorting rule includes ascending sorting and descending sorting.
10. The method of claim 6, wherein the stack is automatically increased by a predetermined size. The display function showAll adopts a for loop statement to traverse all stack elements of the structure class.
Citation Information
Patent Citations
Wait-free stack operation method based on array structure under multinuclear environment
CN106843806A
Demonstration method and system of VBA stack structure, storage medium and computer equipment
CN112541033A