[C and pointers] ch17. Classical abstract data types

Chapter 17: Classic Abstract Data Types

GithubLink: ch17. Classic abstract data types

Abstract data type (ADT)is very common, the most common is the array, sequence tables, linked lists, stacks and queues, and so on. Such as OStask scheduling queue inside there, doubly linked list, red-black trees are widely used. Proficiency in various data structures is very important and necessary.

Summary of this chapter and points to note

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-AoGGlVQb-1610330697919)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210109165515.png)]
[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-UkaNHWrT-1610330697922)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210109170119.png)]

Insert picture description here


Answers to some after-school exercises

17.9 Problem

  1. Stack.

  2. queue.

  3. of course can. The programmer can encapsulate it. top()Take the top element of the stack but do not pop the top element of the stack, the pop()function pops the top element of the stack.

  4. I don't feel how powerful it is. For static array simulation in terms of the stack, just to top_element() = -1be assigned to empty stack so that can achieve results. Of course, C++ STLthe container has a separate clearfunction used to empty the container.

  5. It all depends on whether its initialization is -1 or 0.

  6. First assert()mainly used in this sentence stack empty, full sentence, in fact, it is to keep out of bounds array access occurs. If you delete all the assertions, it is equivalent to the array may have out-of-bounds access, the two are equivalent.

  7. The nodes in the chain stack are all applied for separately, so they have to be released separately. And the pop()function has been implemented in the release of memory, so a direct stack sentenced empty + pop()can be.

  8. Certainly not! This is a common problem when malloc()space is freethe corresponding pointer no longer access a memory space has been released. So take a temporary pointer variable to save the address of the space it points to, and finally release the space pointed to by this pointer variable.

  9. The answer gives a very good explanation:

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-J4kgpC26-1610330697925)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210110082123.png)]

  10. Mentioned in the book. It is really simple to use a counter, and vacating an array element without using it will cause a waste of space. When the size of the element is larger, the waste of space will be greater.

  11. This is indeed a troublesome problem. There are four situations in total: the queue is empty, full, frontfirst, and second. Note that at the end, the calculated element needs to be modulo. When the queue is empty, the modulo operation will also give the correct answer. See demo01.c.

  12. Queue and do not need a reverse traversal, this does not apply doubly linked list, STLthe dequedouble-ended queue, slow the ghost ...

  13. BST Insertion will only insert at the position of its leaf node and will not modify the structure of the original tree. Just pay attention to this point.

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-WVUyTy03-1610330697926)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210110084352.png)]

  14. It is equivalent to array and linked list, O (n) O(n)O ( n )

  15. Simple question.

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-RhVdRLXN-1610330697926)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210110084519.png)]

  16. Middle order: left-root-right. Post sequence: left-right-root. Preface: Root-Left-Right. In fact, it can be found: Simply modify the preorder traversal to: root-right-left, then the reverse order of the preorder is the postorder traversal. Of course, I'm talking about iterative writing, as for recursion, there is nothing to write about. You can see the blog post, the four kinds of traversal are summarized in detail: [M Binary Tree] lc145. Post-order traversal of binary tree (stack+dfs) .

  17. Same as above. Simply look at the reference answer:

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-cFLaiB3l-1610330697927)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210110085002.png)]

  18. In-order traversal. None of the four traversals can directly get the descending sequence, but simply modify the middle order traversal, and change it to right-root-left to get the descending sequence.

  19. Of course, it is post-order traversal, which will process the child nodes before processing the root node.

17.10 Programming Exercises

  1. This function must allocate space for the new stack and copy the value of the old stack to the new one. Then it must release the old array. Assertions are used to ensure that the new array is large enough to hold all the data currently on the stack. See demo02.c.

  2. This module is converted into stackthe module. resizeFunction is more interesting: not every position in the array need to be copied, and when the data around to the end of the array, frontand rearit is easy to become incorrect. See demo03.c.

  3. I'm too lazy to write, just finish writing by yourself and look at the answers after class. It is a simple basic operation of inserting and deleting the end of the linked list.

  4. This is simple, the stack data is simply declared as an array, and the stack number passed as a parameter selects the element to be operated on. See demo04.h、demo05.c.

  5. Simply dfs(), count the number of nodes in the binary tree.

  6. Common bfs()template problem, which is realized in the form of an array BST, can also refer to my blog [M Binary] lc102. Binary tree traversal sequence (queue bfs +) .

    [External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-rLktzPo1-1610330697928)(https://raw.githubusercontent.com/Y-puyu/picture/main/images /20210110091933.png)]

  7. It doesn't mean anything, whether the mid-order sequence is strictly ascending. Recursion can also be used to judge directly. It is definitely possible to solve this problem. Just go up and practice your hands. Here you have to build and write test cases, but the test is not complete.

  8. My blog post: [C++ series] 76. Explain the BST binary search tree in detail . The array form is similar to each other. The four cases of deletion require extra attention. If the value is not found, the leaf node is found, the node with a single child is found, and the node with two children is found. Also find the maximum value in the left subtree for value replacement, this should be the predecessor node, and finally delete the predecessor node.

  9. This is best to use post-order traversal. Unfortunately, the interface design of the traversal function passes a value pointing to the node, not a value pointing to the node contained, so we have to write our own.

  10. Same as 8.

  11. The book uses a macro to make the stack capable of multiple declarations, resolve naming conflicts, generics and other issues. But there are still flaws, for example, the naming of functions of the same type will be consistent. It is a good idea to decompose the stack function into three macros, decoupling. And in daily learning, there is still too little to write for macros, especially the use of macros to write function functions, it is very rare, and you have to accumulate one when you see one. seedemo08.c

Essay

  1. Three ways to implement the stack. Realization of static array, dynamic array, and chain stack. Generally, static arrays are selected for simulation in algorithmic problems. The implementation of dynamic arrays involves the use of memory allocation functions and memory leaks that must be prevented. The space utilization of the chain stack is quite high, but it involves a lot of related operations that change the pointer pointing to. I personally think that the efficiency is definitely not as high as that of the static array!

  2. The three implementations of queues are the same as stacks. However, due to the large amount of space wasted when the static array implements the queue, the loop array is used for optimization, and we generally call it a circular queue here. Use two pointers plus a spare array element to judge empty and full. In fact, it is quite fragrant to use a counting variable. Two circular queue pointers, wherein rearthe pointer is initialized to 0, is added to the first element and time can be frontpointers to the same location. Therefore, each pushtime the element is first rearpointer Xianxiang again after moving the position of an assignment. Full of empty sentenced sentenced two formulas is quite clever: (rear+1)%SIZE==frontthat is, the queue is empty, this can be seen from the initialization out, start a queue is empty frontis 1, rearit is 0. When rear == front, the entire queue is only one element, and then after a team front++is in rearfront of a position, so the formula sentenced empty establishment. And since rear, and frontin the interval of at least one empty element, then when the (read+2)%SIZE == fronttime to explain this queue is full. The dynamic queue is not implemented in the book, and the chain queue is relatively simple.

  3. Tree, in this book that focused on the BSTtree, you can refer to my blog, take C++to achieve: [C ++ Series] 76. Detailed BST binary search tree , with explained in this column AVLtrees and red-black tree. Chain BSTpretty good, eliminating an array of space-utilization problem. Among them, P377the insertion function of the chain binary tree uses two pointers, one of which is a first-level pointer and a second-level pointer. The first-level pointer stores the node in the tree currently traversed, and the second-level pointer points to the left and right child pointers of the current node. space. All insertions under the binary search tree will only be inserted in the leaf nodes.

  4. Improved implementation raised ADTthree issues: Users declare more than one stack, support for generics, to resolve naming conflicts. In the C++ STLmiddle, with the template, these problems will be ironed. In the Clanguage macros can be used to solve this problem. In ch14the already mentioned: the type of macro is irrelevant. And implement a support any type of mallocfunction. The book achieved using macro parameters regardless of the type, the code written in the stack of a macro, the user identifier and add the name customizable by ##way added to the end of the function name. Use Clanguage to implement generics is very difficult, however, object-oriented language for generics have a good support.

doubt

  1. The chained implementation of stacks, queues, BSTetc. are actually quite rusty, and they have not been written in the past. Static arrays are the most written.

  2. On the use of macros to implement Cgenerics in the language of study it is worth considering learning things!

  3. The data structure is much more problematic.

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/112462359