{"format":"brushup-mindmap-v1","id":"ds-03-stack-queue","title":"栈、队列和数组【王道】","structure":"logic-right","theme":"auto","root":{"id":"ecdafb8400292d7f8dc93a8881","topic":"第三章 栈、队列和数组","expanded":true,"children":[{"id":"4b2a6fa30d2a190f4378097f9a","topic":"栈","expanded":true,"children":[{"id":"8687e014-f446-486a-92a2-787d000f7464","topic":"栈的概念","expanded":true,"children":[{"id":"99850472-bd7a-489b-a5cd-879d70ed3ac4","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-95ea5cd461a1c2.png","width":280.99,"height":181},"children":[{"id":"bb3414e2-70db-4f7f-95e2-9f90ef43eb34","topic":"限定仅在一端进行插入和删除操作的线性表，又称后进先出（LIFO）的线性表；\n允许插入删除的那一端叫栈顶（Top），不允许插入和删除的一端叫栈底（Bottom）","expanded":true,"children":[{"id":"b79861ab-feb1-4442-8c26-170fc3578ec3","topic":"特殊的线性表，后进先出（LIFO）","expanded":true}]},{"id":"2c6d9edf-d05e-4aea-a1a2-c2b098678535","topic":"注意：n个不同元素进栈，出栈元素不同排序个数为（n个元素能构成多少种不同的二叉树）","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"0e67c0e4-6f3c-43f1-9a32-0d03945c5525","topic":"","expanded":true,"formula":{"tex":"\\frac{1}{n+1} C_{2n}^n","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mfrac><mn>1</mn><mrow><mi>n</mi><mo>+</mo><mn>1</mn></mrow></mfrac><msubsup><mi>C</mi><mrow><mn>2</mn><mi>n</mi></mrow><mi>n</mi></msubsup></mrow><annotation encoding=\"application/x-tex\">\\frac{1}{n+1} C_{2n}^n</annotation></semantics></math></span>"}}]}]}]},{"id":"12818ae6-ca96-4c64-9890-bf11bb7570a7","topic":"栈的实现","expanded":true,"children":[{"id":"cd09aa37-de5d-404e-b64f-054a886f0627","topic":"栈的顺序存储结构","expanded":true,"style":{"branchColor":"#EA8ABA"},"children":[{"id":"436ef243-497d-4dd7-8918-9247406e0fea","topic":"顺序栈的基本结构","expanded":true,"children":[{"id":"39f67576-db0c-4949-80c9-fe4e1f63de62","topic":"SqStack 定义","expanded":true,"children":[{"id":"a7e0dce5-a57a-4bbe-82de-d261f11cf760","topic":"利用一组地址连续的存储单元存放自栈底到栈顶的数据元素，同时设一个栈顶指针（Top）","expanded":true},{"id":"5a036a93-a1d7-4584-87cd-8647baa1d415","topic":"基础结构","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"e126afe0-7fdb-478d-a8a1-a605e8073e0b","topic":"栈顶指针，初始值：S.top = -1（若有元素，则指向栈顶元素S.data[S.top]）","expanded":true},{"id":"4d9022a5-30a9-4d19-886d-4ccb96fa3212","topic":"进栈：栈不满时，栈指针先+1，再送值到栈顶","expanded":true,"children":[{"id":"b0a4ee0c-fa8e-4b5c-8f87-ec20b1d7d528","topic":"出栈：栈非空时，先取栈顶元素值，再将栈顶指针-1","expanded":true}]},{"id":"1719a548-479d-4a84-a3b6-fab983d07133","topic":"栈空条件：S.top == -1","expanded":true,"children":[{"id":"59a7ce3e-7f15-4f17-a7e3-0ee71bae033c","topic":"栈满条件：S.top == MaxSize-1","expanded":true,"children":[{"id":"adc19c1b-54f8-4737-ad0a-3df4afbba2d2","topic":"栈长：S.top + 1","expanded":true}]}]}]}],"code":{"lang":"c","code":"#define MaxSize 10              //定义栈中元素的最大个数\ntypedef struct{\n    ElemType data[MaxSize];     //静态数组存放栈中元素\n    int top;                    //栈顶指针\n} SqStack;","title":"SqStack","tokens":[[{"text":"#define ","darkColor":"#C699E0"},{"text":"MaxSize","darkColor":"#6EC0DF"},{"text":" ","darkColor":"#C699E0"},{"text":"10","darkColor":"#E2B159"},{"text":"              //定义栈中元素的最大个数","darkColor":"#908B7E"}],[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#C699E0"},{"text":"struct","darkColor":"#8EA4E2"},{"text":"{","darkColor":"#BAB5A8"}],[{"text":"    ElemType ","darkColor":"#DEDBD2"},{"text":"data","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"MaxSize","darkColor":"#DEDBD2"},{"text":"];","darkColor":"#BAB5A8"},{"text":"     //静态数组存放栈中元素","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"int","darkColor":"#74CBC4"},{"text":" top","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"                    //栈顶指针","darkColor":"#908B7E"}],[{"text":"} SqStack","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}]]}}]},{"id":"60cfba60-ec55-47e4-a33e-004b46e32d2c","topic":"顺序栈的基本操作","expanded":true,"children":[{"id":"b4470dd5-c878-467f-8366-08286c0a53a4","topic":"初始化、判栈空、进栈、出栈、读栈顶元素（本质就是操作顺序表）","expanded":true}]},{"id":"3f10a6c7-a96f-417a-b871-b430b46f2dfe","topic":"共享栈","expanded":true,"style":{"borderColor":"#FF9F00"},"children":[{"id":"99772d5c-495c-4dda-a94e-a2f8181a97b9","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-4e76596381c6ac.png","width":211.92,"height":296.05},"children":[{"id":"e41f213e-f598-4d3c-ac9f-047b8725792d","topic":"概念","expanded":true,"children":[{"id":"c5f57316-7acf-4478-ba62-878018b4b71d","topic":"让两个顺序栈共享一个一维数组空间，将两个栈的栈底分别设置在共享空间的两端，两个栈顶向共享空间的中间延伸","expanded":true}]},{"id":"4e32a85c-8788-4cb4-a4ec-fc53b946372c","topic":"原则","expanded":true,"children":[{"id":"98c80e85-5d7e-4dc1-b9b5-83c15daa3da0","topic":"栈空判断：top0 = -1时0号栈为空，top1 = MaxSize时1号栈为空","expanded":true},{"id":"ddc53c29-db7f-407e-a7b2-ea220df10785","topic":"栈满判断：两个栈顶指针相邻（即top1-top0 == 1）时","expanded":true},{"id":"1f06221c-ce6c-4f69-9cbd-5d6d71a9d591","topic":"其他操作和顺序栈类似","expanded":true}]}]}]}]},{"id":"9cf5e320-3bb3-49e3-a832-30e43f7b310c","topic":"栈的链式存储结构","expanded":true,"children":[{"id":"5da30b57-1314-4fe5-810a-71d115fc79ba","topic":"LiStack 定义","expanded":true,"children":[{"id":"2ae539bd-068c-4e6e-b624-14e0191bc21c","topic":"采用单链表实现，并规定所有操作都是在单链表的表头进行","expanded":true},{"id":"4f71e779-1cc9-4564-ab60-af6030e06395","topic":"优点","expanded":true,"children":[{"id":"d3f06d7c-122a-4943-a24d-99e199a9f907","topic":"便于多个栈共享存储空间和提高效率，不存在栈满上溢的情况","expanded":true}]}],"code":{"lang":"c","code":"typedef struct Linknode{\n    ElemType data;              //数据域\n    struct Linknode *next;      //指针域\n} *LiStack;                     //栈类型定义","title":"Linknode / LiStack","tokens":[[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" Linknode","darkColor":"#DEDBD2"},{"text":"{","darkColor":"#BAB5A8"}],[{"text":"    ElemType data","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"              //数据域","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" Linknode ","darkColor":"#DEDBD2"},{"text":"*","darkColor":"#BAB5A8"},{"text":"next","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"      //指针域","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"},{"text":" ","darkColor":"#DEDBD2"},{"text":"*","darkColor":"#BAB5A8"},{"text":"LiStack","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"                     //栈类型定义","darkColor":"#908B7E"}]]}}]}]}]},{"id":"213b3751a789a97fff8ddbb537","topic":"队列","expanded":true,"children":[{"id":"42221ed4-7a64-44b7-baaa-5aeb8e5792d8","topic":"队列的概念","expanded":true,"children":[{"id":"e176d0a4-12d9-4bc4-a3e6-f61f37d60461","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-c3991ba7e84948.png","width":355.97,"height":114.1},"children":[{"id":"d47487e8-3ab5-4f94-a911-4daea05635d4","topic":"是一种操作受限的线性表，只允许在表的一端进行插入（队尾），另一端进行删除（队头）","expanded":true,"children":[{"id":"c9c63105-c959-4ea3-a4c4-1ef4510bf62e","topic":"特殊的线性表，先进先出（FIFO）","expanded":true}]},{"id":"eb7827d8-a2ef-462f-bd35-0b4e981f1b1e","topic":"队头：删除元素的一端","expanded":true,"children":[{"id":"3adc814e-d61c-4591-aabc-b790e6aa50ed","topic":"队尾：插入元素的一端","expanded":true}]}]}]},{"id":"11c65319-9c7b-4846-ac21-31a72d4ad5e7","topic":"队列的实现","expanded":true,"style":{"branchColor":"#007C74"},"children":[{"id":"569ee587-1d71-4122-9866-2efca85e0ccb","topic":"队列的顺序存储结构","expanded":true,"children":[{"id":"a915542d-d259-4f7e-aa75-fa9dda3faa83","topic":"SqQueue 定义","expanded":true,"children":[{"id":"f38b2f32-5a2f-4044-ac8c-178b4cca077c","topic":"分配一块连续的存储单元存放队列元素，设两个指针","expanded":true,"children":[{"id":"91c73954-5de3-40a8-b540-0ec63ea6980d","topic":"队头指针front：指向队头元素\n队尾指针rear：指向队尾元素的下一个元素","expanded":true}]},{"id":"ddc168a8-02b7-4c8c-b5f4-da2f0666deda","topic":"基础结构","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"d6f7402c-f8ac-4c84-bd93-f1631133170a","topic":"初始状态（队空条件）：Q.front == Q.rear == 0","expanded":true},{"id":"0308005e-ad1e-4018-848a-92b27aef120b","topic":"进队操作：队不满时，先送值到队尾元素，再将队尾指针+1","expanded":true},{"id":"bc4b29a7-f7f3-496d-808f-5e5bc8bb5fb6","topic":"出队操作：队不空时，先取队头元素值，再将队头指针+1","expanded":true}]},{"id":"c64d251e-ccfc-49de-b4b7-d81fa60888c0","topic":"假溢出：在data数组依然存在空位置时，却已经满足队列满的条件（出栈的元素位置空闲）","expanded":true}],"code":{"lang":"c","code":"#define MaxSize 10              //定义队列中元素的最大个数\ntypedef struct{\n    ElemType data[MaxSize];     //用静态数组存放队列元素\n    int front,rear;             //队头指针和队尾指针\n} SqQueue;","title":"SqQueue","tokens":[[{"text":"#define ","darkColor":"#C699E0"},{"text":"MaxSize","darkColor":"#6EC0DF"},{"text":" ","darkColor":"#C699E0"},{"text":"10","darkColor":"#E2B159"},{"text":"              //定义队列中元素的最大个数","darkColor":"#908B7E"}],[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#C699E0"},{"text":"struct","darkColor":"#8EA4E2"},{"text":"{","darkColor":"#BAB5A8"}],[{"text":"    ElemType ","darkColor":"#DEDBD2"},{"text":"data","darkColor":"#EEECE6"},{"text":"[","darkColor":"#BAB5A8"},{"text":"MaxSize","darkColor":"#DEDBD2"},{"text":"];","darkColor":"#BAB5A8"},{"text":"     //用静态数组存放队列元素","darkColor":"#908B7E"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"int","darkColor":"#74CBC4"},{"text":" front","darkColor":"#DEDBD2"},{"text":",","darkColor":"#BAB5A8"},{"text":"rear","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"             //队头指针和队尾指针","darkColor":"#908B7E"}],[{"text":"} SqQueue","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}]]}},{"id":"01839a19-4edb-46c7-bec0-bc59cf74396c","topic":"循环队列","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"03320d2c-1f51-420d-97c9-1c2b4a378902","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-a632949170d51c.png","width":411.99,"height":380.98},"children":[{"id":"b25c005e-376c-4427-9a87-5643898178f9","topic":"概念","expanded":true,"children":[{"id":"eec7b4ba-5d47-4dcf-80d2-20964966a16f","topic":"把存储队列元素的表逻辑上视为一个环","expanded":true}]},{"id":"74160130-6236-42eb-8181-830ad251beb3","topic":"基本操作","expanded":true,"children":[{"id":"a9a8c4a6-f0c3-4005-980b-3c422370dee4","topic":"初始时：Q.front = Q.rear = 0","expanded":true},{"id":"25b476db-bace-46ff-abf0-fdbcc30e60b2","topic":"队头指针+1：Q.front = (Q.front + 1) % MaxSize","expanded":true,"style":{"borderColor":"#E32C2D"}},{"id":"625d167b-991a-4aa4-b4d7-ba3cebdaf1ee","topic":"队尾指针+1：Q.rear = (Q.rear + 1) % MaxSize","expanded":true,"style":{"borderColor":"#E32C2D"}},{"id":"36998330-5142-45d6-84d2-c1c106b84710","topic":"队列长度：(Q.rear + MaxSize - Q.front) % MaxSize","expanded":true,"style":{"borderColor":"#E32C2D"}},{"id":"9bb4798e-9862-4eb6-82df-6206af0f68f9","topic":"队空：Q.front == Q.rear","expanded":true}]},{"id":"327cb8bc-92af-4f14-8f52-ceb4e3c6a169","topic":"队满判断","expanded":true,"children":[{"id":"8fc429d2-df99-4ac5-9551-736b800370fd","topic":"牺牲一个单元来区分队空和队满，即队头指针在队尾指针的下一位置作为队满标志","expanded":true,"children":[{"id":"4eabdb50-f64a-4ab4-a53c-28e74bca5544","topic":"队满条件：(Q.rear + 1) % MaxSize == Q.front","expanded":true}]},{"id":"d9dea8f4-d55b-44b9-a743-5467cd66f48b","topic":"类型中增设表示元素个数的数据成员（int size）","expanded":true,"children":[{"id":"54826078-ccb8-4fb1-8e25-a462685952e4","topic":"队空：Q.size == 0","expanded":true},{"id":"a9925892-5b10-48dd-8f7c-3998ad005e9e","topic":"队满：Q.size == MaxSize","expanded":true}]},{"id":"b05ca518-1da1-47b3-a1dd-37a63d46558c","topic":"类型中增设tag数据成员，以区分是队满还是队空","expanded":true,"children":[{"id":"64a2aada-0af8-4206-8400-dc5af384d1d7","topic":"tag == 0时，若因删除导致Q.front == Q.rear，则队空","expanded":true},{"id":"0f58024f-a584-471c-bd72-c228c9941c72","topic":"tag == 1时，若因插入导致Q.front == Q.rear，则队满","expanded":true}]}]}]}]}]},{"id":"2951c121-e861-4085-8dc4-bb3a82d94187","topic":"队列的链式存储结构","expanded":true,"children":[{"id":"d5c3b698-94c7-43ac-9ffc-44971ef31c0a","topic":"LinkQueue 定义","expanded":true,"children":[{"id":"06395733-7d08-41ba-80d0-335b3037e1a3","topic":"队列的链式表示时一个同时带有队头指针和队尾指针的单链表","expanded":true},{"id":"f57ff0e1-5f6d-4b10-8d44-1ec5c7e4e920","topic":"头指针指向队头结点，队尾指针指向队尾结点，当Q.front == NULL且Q.rear == NULL时，队列为空","expanded":true},{"id":"de6c9bb0-ceac-4833-9069-bea252a50d3d","topic":"不存在队列满且溢出问题，适合于数据元素变动较大的情况","expanded":true}],"code":{"lang":"c","code":"typedef struct LinkNode{        //链式队列结点\n    ElemType data;\n    struct LinkNode *next;\n}LinkNode;\n\ntypedef struct{                 //链式队列\n    LinkNode *front,*rear;      //队列的队头和队尾指针\n}LinkQueue;","title":"LinkNode / LinkQueue","tokens":[[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" LinkNode","darkColor":"#DEDBD2"},{"text":"{","darkColor":"#BAB5A8"},{"text":"        //链式队列结点","darkColor":"#908B7E"}],[{"text":"    ElemType data","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}],[{"text":"    ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":" LinkNode ","darkColor":"#DEDBD2"},{"text":"*","darkColor":"#BAB5A8"},{"text":"next","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}],[{"text":"}","darkColor":"#BAB5A8"},{"text":"LinkNode","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}],[{"text":" "}],[{"text":"typedef","darkColor":"#8EA4E2"},{"text":" ","darkColor":"#DEDBD2"},{"text":"struct","darkColor":"#8EA4E2"},{"text":"{","darkColor":"#BAB5A8"},{"text":"                 //链式队列","darkColor":"#908B7E"}],[{"text":"    LinkNode ","darkColor":"#DEDBD2"},{"text":"*","darkColor":"#BAB5A8"},{"text":"front","darkColor":"#DEDBD2"},{"text":",*","darkColor":"#BAB5A8"},{"text":"rear","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"},{"text":"      //队列的队头和队尾指针","darkColor":"#908B7E"}],[{"text":"}","darkColor":"#BAB5A8"},{"text":"LinkQueue","darkColor":"#DEDBD2"},{"text":";","darkColor":"#BAB5A8"}]]}}]}]},{"id":"d44bccb9-7418-4aae-b815-8028af0f414d","topic":"双端队列（挺喜欢考的考点）","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"5e8007e0-3e3b-443b-aef1-4e8ae8ebc14c","topic":"概念","expanded":true,"children":[{"id":"e1514f1d-666e-44c1-af96-ea28fbc87a69","topic":"双端队列是指允许两端都可以进行入队和出队操作的队列，其逻辑结构仍是线性表","expanded":true}]},{"id":"f40e87e3-2235-416b-8d71-d542de793993","topic":"分类","expanded":true,"children":[{"id":"72953222-ffa2-4133-8f6b-2704478544b0","topic":"输出受限的双端队列：允许在一端进行插入和删除，但在另一端只允许插入的双端队列","expanded":true},{"id":"864021d5-4d75-4643-8425-cb1463ff8603","topic":"输入受限的双端队列：允许在一端进行插入和删除，但在另一端只允许删除的双端队列","expanded":true}]}]},{"id":"fcdc153b-fca4-49e9-8225-9e552073b127","topic":"栈和队列有相同逻辑结构，线性结构","expanded":true,"style":{"borderColor":"#EB4D3D"}},{"id":"ba03fe59-3608-43b5-94d1-5e59fd0323bc","topic":"合适与不合适做链队的链表，关键看能不能获取到首尾指针（带首尾的非循环或带尾的循环）","expanded":true,"style":{"borderColor":"#EB4D3D"},"children":[{"id":"d83b5400-9fdf-4203-a8b3-fc6c2dc879b1","topic":"单链表实现队列，队头只能设置在链头（关注删除操作，需找到新队头）","expanded":true,"style":{"borderColor":"#EB4D3D"}}]}]},{"id":"fa7bc63b-4fd9-49ac-9253-cddea1da27c1","topic":"栈和队列的应用","expanded":true,"children":[{"id":"259a4b38-21c3-4fc7-bb46-0ddf70529335","topic":"栈的应用","expanded":true,"children":[{"id":"beecfb4b-89d7-4138-90b3-55a5ee51c9fc","topic":"括号匹配","expanded":true,"style":{"branchColor":"#BA69C8"},"children":[{"id":"ff660438-0932-47ff-99ab-86cdc9aa807e","topic":"步骤","expanded":true,"children":[{"id":"132a2b68-4891-4c23-b770-61e2be584067","topic":"初始设置一个空栈，顺序读入括号","expanded":true},{"id":"f11d9ea9-ae88-4682-970c-d86c63bada13","topic":"若右括号，则从栈中弹出一个符号，判断是否匹配","expanded":true},{"id":"8481f11b-2534-4e5a-83bd-460e6642c559","topic":"若左括号，则压入栈中","expanded":true}]},{"id":"eb88dc91-d821-4957-9bd2-8cfef766eceb","topic":"失败条件","expanded":true,"children":[{"id":"37bf0789-7c0c-4f29-a603-76d7fa07a9f3","topic":"不匹配、结束后栈仍有元素、遇右括号栈为空","expanded":true}]}]},{"id":"e1076121-fa0a-41fc-8368-f231a6a0eafe","topic":"表达式求值","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"e2b4d257-fbed-4ce0-b562-79594414f9f0","topic":"中缀表达式转后缀表达式","expanded":true,"children":[{"id":"581a720f-1624-46aa-bdbf-94a0ca8adacf","topic":"从左到右遍历各元素","expanded":true,"children":[{"id":"b2dd73f8-9361-4794-a779-a85546326e8f","topic":"若遇到操作数：直接加入后缀表达式","expanded":true},{"id":"1ceade4c-38f4-449a-b39e-6b2c4406def7","topic":"遇到界限符：“(”直接入栈；“)”则依次弹出栈内运算符并加入后缀表达式，直到弹出“(”为止","expanded":true},{"id":"2b2c5cdc-ac60-4033-977f-38974ad0bdd8","topic":"遇到运算符：依次弹出栈中优先级高于或等于当前运算符的所有运算符，并加入后缀表达式，若碰到“(”或栈空则停止，之后再把运算符入栈","expanded":true,"style":{"background":"rgba(215, 150, 248, 0.2)","borderColor":"#EB4D3D"}}]}]},{"id":"dd4ba6f6-ef82-4c91-b42a-6c19885e9157","topic":"中缀表达式转前缀表达式","expanded":true,"children":[{"id":"3eec1c21-1914-4caa-9b00-393e839bb126","topic":"同理但从右到左遍历，“(”和“)”与中缀转后缀的功能相反，最后结果依次翻转","expanded":true}]},{"id":"4f9da801-b2e3-4ebc-8d7f-4992c275fbf5","topic":"中缀表达式的计算","expanded":true,"children":[{"id":"7a13ffe5-880c-4c9a-ad39-6653b66af8b4","topic":"初始化操作数栈和运算符栈","expanded":true},{"id":"46e22d93-877c-4564-8629-9324f4658910","topic":"若扫描到操作数，压入操作数栈；若扫描到运算符或界限符，按“中缀转后缀”的逻辑压入运算符栈（可能弹出运算符，若弹出则将栈顶的两个操作数运算压回操作数栈）","expanded":true}]},{"id":"8a112cc9-1826-42ea-899a-4388a886f3bc","topic":"后缀表达式的计算","expanded":true,"children":[{"id":"94dc4507-66b3-45e7-a596-02e14776de4f","topic":"从左向右扫描元素，若扫描到操作数则压入栈，若扫描到运算符则弹出两个栈顶元素，执行运算将结构压回栈中","expanded":true}]},{"id":"188309c9-1749-4c32-91cd-aded67043b57","topic":"前缀表达式的计算","expanded":true,"children":[{"id":"b9d84fd3-c662-4958-a8b2-0e8f00063a1a","topic":"同上，但从右向左","expanded":true}]}]},{"id":"90d95902-1bf0-4a96-8de1-bcde69ff18d8","topic":"递归","expanded":true,"style":{"branchColor":"#BA69C8"},"children":[{"id":"c2ef6b06-5d4b-4ee9-a5a2-45b803787ad0","topic":"概念","expanded":true,"children":[{"id":"6d0a4101-7ab6-4991-9ef3-e890cd95be68","topic":"若对象部分地包含自己，或用自己给自己定义，则称这个对象是递归的","expanded":true},{"id":"09f8e9ff-30ab-4894-912b-c00827316bfa","topic":"若一个过程直接或间接的调用自己，则这个过程是递归的","expanded":true}]},{"id":"ab1ad4ab-d814-4a74-b194-f8616e6fab15","topic":"分治的思想：必备条件","expanded":true,"children":[{"id":"a3fb629b-af3e-40d2-a027-86f21081d7fa","topic":"能将一个问题转变成一个新问题，且两者的解法相同或类似，不同的仅是对象，且对象有变化规律","expanded":true},{"id":"26380f08-68eb-4884-b4be-7cb2993624a1","topic":"可以通过上述转化将问题化简","expanded":true},{"id":"774f7180-644e-49bd-8213-cf4fee6db8a7","topic":"必须有一个明确的递归出口（递归边界）","expanded":true}]},{"id":"019de01c-f928-434b-bc1f-f76f3c6c0113","topic":"形式","expanded":true,"children":[{"id":"2bca9ecc-cd5f-47bc-93a3-805883a5f670","topic":"递归函数形式","expanded":true,"code":{"lang":"plaintext","code":"void p (参数表){\n    if (递归结束条件) 可直接求解步骤; ------基本项\n    else p (较小的参数) ; ------归纳项\n}","title":"递归形式","tokens":[[{"text":"void p (参数表){"}],[{"text":"    if (递归结束条件) 可直接求解步骤; ------基本项"}],[{"text":"    else p (较小的参数) ; ------归纳项"}],[{"text":"}"}]]}}]}]},{"id":"22cbf4e2-96e7-4155-a504-2d4e78b98f4c","topic":"其他：数制转换、八皇后问题、行编辑程序、函数调用、迷宫求解等","expanded":true}]},{"id":"dcd1919d-bc76-4bae-b2b0-0429343b4b47","topic":"队列的应用","expanded":true,"children":[{"id":"885a230c-a41b-40ce-8bc7-14bdb72d771a","topic":"脱机打印输出、解决多用户引起的资源竞争问题、广度优先遍历、网络电文传输按时间先后顺序依次进行等等","expanded":true}]}]},{"id":"60c9ba3162680571a747abeccd","topic":"数组和特殊矩阵","expanded":true,"children":[{"id":"e3de2acc-b99a-4251-85c8-64f21c19ac33","topic":"本节的题目计算都可以不记公式，直接代入推导","expanded":true,"style":{"borderColor":"#FF9F00"}},{"id":"d270d854-8bbd-451e-930e-a6cd60b92735","topic":"数组","expanded":true,"children":[{"id":"3f36520b-018b-4a96-83d9-a0e105c3fa54","topic":"概念","expanded":true,"children":[{"id":"9725927a-2316-4ad0-bdcb-288eb87e9478","topic":"数组是由n个相同类型的数据元素构成的有限序列，每个元素在n个线性关系中的序号称为元素下标，下标取值范围称为数组的维界","expanded":true}]},{"id":"1e012ef9-416d-4600-834c-1c45b391269a","topic":"与线性表的关系","expanded":true,"children":[{"id":"ca493926-0687-4e17-b555-5bab3ddcef89","topic":"数组是线性表的推广，一维数组可视为一个线性表，二维数组可以视为其元素也是定长线性表的线性表","expanded":true}]},{"id":"3c16a6b1-0f0c-4279-9b96-3a7e23216fed","topic":"数组的存储结构","expanded":true,"children":[{"id":"79696847-15ee-4b3f-84be-bbc6112e1548","topic":"数组的所有元素在内存中占用一段连续的存储空间","expanded":true},{"id":"b9630d49-215f-4d20-b0a7-cbde4d5cfd67","topic":"一维数组","expanded":true,"children":[{"id":"d73293a2-ea57-4f30-aed5-531ea4b7a5c4","topic":"存储结构关系式：LOC(ai) = LOC(a0) + i × L    （0 <= i < n，L是每个元素所占存储单元）","expanded":true}]},{"id":"6d507240-8674-401d-a9ce-070b21120467","topic":"多维数组","expanded":true,"style":{"borderColor":"#E32C2D"},"children":[{"id":"f41afe7e-9670-4bdf-874c-4cb7a84db4ab","topic":"按行优先（一行行存储）","expanded":true,"children":[{"id":"45e56ff1-5edc-4fda-975e-a30e513386ce","topic":"存储结构关系式：LOC(ai,j) = LOC(a0,0) + [i × (h1 + l) + j] × L        （二维数组：h1 × h2）","expanded":true}]},{"id":"6d354876-27c0-4b9c-b513-6ca81107aff0","topic":"按列优先（一列列存储）","expanded":true,"children":[{"id":"c6a1fbd3-1144-4936-b51b-c35ae2865c36","topic":"存储结构关系式：LOC(ai,j) = LOC(a0,0) + [j × (h2 + l) + i] × L        （二维数组：h1 × h2）","expanded":true}]}]}]}]},{"id":"2c421617-5071-43c1-bda6-2903f2c31b80","topic":"特殊矩阵","expanded":true,"style":{"borderColor":"#E32C2D","branchColor":"#EA8ABA"},"children":[{"id":"8bd0d67a-fa0a-456f-b8e7-a1425f45f6bc","topic":"指具有许多相同矩阵元素或0元素，并且这些相同矩阵元素或0元素的分布有一定规律","expanded":true},{"id":"5ff8673f-cc3c-44bc-87a5-0ec18cdf3962","topic":"压缩矩阵","expanded":true,"children":[{"id":"421d9058-edaf-46b4-abe6-870cb5fbfe70","topic":"找出特殊矩阵中值相同的矩阵元素的分布规律，把那些呈现分布规律的、多个值相同的元素只分配一个存储空间，对0元素不分配空间","expanded":true}]},{"id":"dbdcafec-63d4-47a1-8f65-5bcadffdd53e","topic":"对称矩阵","expanded":true,"children":[{"id":"a044ba09-05c3-46af-aa30-6cdb7f63c3b3","topic":"概念","expanded":true,"children":[{"id":"a48ad801-beb2-4412-9b75-8e115c618a63","topic":"若对一个n阶方阵中的任意元素a(i, j) = a(j, i)，则称其为对称矩阵，可放在一维数组B[n(n+1)/2]中","expanded":true}]},{"id":"a967a8cd-31c5-45d4-bd20-9c1c3206d918","topic":"存储位置计算公式","expanded":true,"children":[{"id":"7dfacf03-1106-4b42-ab95-fdf5911d410d","topic":"","expanded":true,"formula":{"tex":"k = \\begin{cases} \\frac{i(i-1)}{2} + j - 1, & i \\geqslant j \\text{（下三角区和主对角线元素）} \\\\ \\frac{j(j-1)}{2} + i - 1, & i < j \\text{（上三角区元素 } a_{ij} = a_{ji}\\text{）} \\end{cases}","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>k</mi><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mi>i</mi><mo stretchy=\"false\">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo>+</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>⩾</mo><mi>j</mi><mtext>（下三角区和主对角线元素）</mtext></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mi>j</mi><mo stretchy=\"false\">(</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo>+</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>&lt;</mo><mi>j</mi><mtext>（上三角区元素 </mtext><msub><mi>a</mi><mrow><mi>i</mi><mi>j</mi></mrow></msub><mo>=</mo><msub><mi>a</mi><mrow><mi>j</mi><mi>i</mi></mrow></msub><mtext>）</mtext></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">k = \\begin{cases} \\frac{i(i-1)}{2} + j - 1, &amp; i \\geqslant j \\text{（下三角区和主对角线元素）} \\\\ \\frac{j(j-1)}{2} + i - 1, &amp; i &lt; j \\text{（上三角区元素 } a_{ij} = a_{ji}\\text{）} \\end{cases}</annotation></semantics></math></span>"}}]}]},{"id":"a39027de-12c2-43bb-895f-e85eee77494b","topic":"三角矩阵","expanded":true,"children":[{"id":"6ac8ce60-ec9d-45d1-9f53-3b141f29e906","topic":"概念","expanded":true,"children":[{"id":"ea267fbe-6d64-4643-8a94-04b4f3d0756f","topic":"（上/下）三角区的所有元素均为同一常量，其可压缩为存储完（下/上）三角区和主对角线上元素 + 存储常量一次","expanded":true}]},{"id":"3557768d-d7b4-4dcc-ba72-75b257348fbe","topic":"（下/上）三角矩阵元素下标对应关系","expanded":true,"children":[{"id":"e2e645c7-6f33-4703-b1be-d7aa71a04059","topic":"下三角矩阵","expanded":true,"children":[{"id":"dda401f3-d251-4f68-a384-131f9538ee84","topic":"上三角矩阵","expanded":true,"formula":{"tex":"k = \\begin{cases} \\frac{(i-1)(2n-i+2)}{2} + (j - i), & i \\leqslant j \\text{（上三角区和主对角线元素）} \\\\ \\frac{n(n+1)}{2}, & i > j \\text{（下三角区元素）} \\end{cases}","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>k</mi><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mo stretchy=\"false\">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy=\"false\">)</mo><mo stretchy=\"false\">(</mo><mn>2</mn><mi>n</mi><mo>−</mo><mi>i</mi><mo>+</mo><mn>2</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo>+</mo><mo stretchy=\"false\">(</mo><mi>j</mi><mo>−</mo><mi>i</mi><mo stretchy=\"false\">)</mo><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>⩽</mo><mi>j</mi><mtext>（上三角区和主对角线元素）</mtext></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mi>n</mi><mo stretchy=\"false\">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>&gt;</mo><mi>j</mi><mtext>（下三角区元素）</mtext></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">k = \\begin{cases} \\frac{(i-1)(2n-i+2)}{2} + (j - i), &amp; i \\leqslant j \\text{（上三角区和主对角线元素）} \\\\ \\frac{n(n+1)}{2}, &amp; i &gt; j \\text{（下三角区元素）} \\end{cases}</annotation></semantics></math></span>"}}],"formula":{"tex":"k = \\begin{cases} \\frac{i(i-1)}{2} + j - 1, & i \\geqslant j \\text{（下三角区和主对角线元素）} \\\\ \\frac{n(n+1)}{2}, & i < j \\text{（上三角区元素）} \\end{cases}","displayMode":true,"mathml":"<span class=\"katex\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>k</mi><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mi>i</mi><mo stretchy=\"false\">(</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo>+</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>⩾</mo><mi>j</mi><mtext>（下三角区和主对角线元素）</mtext></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mfrac><mrow><mi>n</mi><mo stretchy=\"false\">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><mn>2</mn></mfrac><mo separator=\"true\">,</mo></mrow></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><mi>i</mi><mo>&lt;</mo><mi>j</mi><mtext>（上三角区元素）</mtext></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">k = \\begin{cases} \\frac{i(i-1)}{2} + j - 1, &amp; i \\geqslant j \\text{（下三角区和主对角线元素）} \\\\ \\frac{n(n+1)}{2}, &amp; i &lt; j \\text{（上三角区元素）} \\end{cases}</annotation></semantics></math></span>"}}]},{"id":"6fc7bb15-4c8f-4381-96c2-a91ebac3e072","topic":"（下/上）三角矩阵在内存中的压缩存储形式","expanded":true,"children":[{"id":"e7451d59-3433-47d8-a3ad-b2818dc93365","topic":"下三角矩阵","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-afcf138de61158.png","width":398.13,"height":67.82},"children":[{"id":"ea64c518-16d9-4602-930b-14bd5b3fbbb6","topic":"上三角矩阵","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-d971f00e753cf6.png","width":373.33,"height":62.11}}]}]}]},{"id":"a8a4f012-18c1-4bc0-9cd4-9e980bbab6e5","topic":"三对角矩阵","expanded":true,"children":[{"id":"4940c834-b236-4c17-bed6-2d3ced69a2f2","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-2ebf75a6cee539.png","width":742,"height":644},"children":[{"id":"f4c1a15b-089b-48a3-8347-485fbabd3d62","topic":"概念","expanded":true,"children":[{"id":"dd9a59be-fcdf-4c94-8947-2973b2817bd6","topic":"又称带状矩阵，除以对角线为中心的3条对角线区域外都为0","expanded":true}]},{"id":"ee96e493-1666-4a9f-96bd-7e40a4157151","topic":"压缩存储：将元素按行优先方式存放在一维数组B中，且a(i, j)与B[n]对应关系为：k = 2i + j - 3","expanded":true}]}]},{"id":"6ef9ee9b-89fb-48ef-8f60-067ac689e2b5","topic":"稀疏矩阵","expanded":true,"children":[{"id":"897fd321-d29f-4d74-b150-08aca4ed81c2","topic":"概念","expanded":true,"children":[{"id":"fadba11f-0c23-4efd-b640-592338bd33ca","topic":"矩阵中非0元素的个数t，相对矩阵元素的个数s来说非常少，即s>>t","expanded":true}]},{"id":"1031e69a-bf96-4bc2-a730-e1b5356bf0be","topic":"存储方式","expanded":true,"children":[{"id":"14949992-fa7c-401e-9838-6e788f231095","topic":"三元组（行标、列标、值）","expanded":true,"children":[{"id":"352a35a7-b3e6-43ab-8265-4cf30878322d","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-fb6e63e3e5bcf4.png","width":420,"height":416},"children":[{"id":"d05f0a65-dccc-4061-9136-7544fd7511ce","topic":"十字链表法","expanded":true,"children":[{"id":"68bdcb25-e374-4706-878f-769d27f0b4c3","topic":"图示","expanded":true,"image":{"src":"https://gastigado.cnies.org/d/BrushUP/mm-11c139a08a6b5e.png","width":684.63,"height":295.04}}]}]}]}]},{"id":"acf9fbd1-e794-441e-8ae8-20e8976ace88","topic":"稀疏矩阵压缩存储后失去了随机存取的特性","expanded":true}]}]}]},{"id":"e2260b84-0e84-4f6f-9be8-0ef962a47bce","topic":"补充代码","expanded":true,"children":[{"id":"3f3e9784-452e-435d-81ee-05f5a4ee63fa","topic":"栈（数组模拟）","expanded":true,"children":[{"id":"59386015-02c5-4f56-8cac-ba8bc4cb4a08","topic":"对数组简单操作，略","expanded":true}]},{"id":"37aec7ef-9364-4b89-881b-d56d409e1892","topic":"（循环）队列（数组模拟）","expanded":true,"style":{"branchColor":"#15831C"},"children":[{"id":"81c476fc-017f-4edf-a933-6ecc6cc25387","topic":"循环队列注意点：队满与队空条件需要有区别，即需要一个额外的元素空间判断队空与队满","expanded":true},{"id":"0b96d1bc-2a3e-422c-8fd5-677348ed55e1","topic":"void push_q(int x){\n    if((tail + N + 1) % N != head){  //判断队满\n        q[tail] = x;\n        tail = (N + tail + 1) % N;  //队尾插入一个数据注意指针移动需要%N，对头类似：head=(N+head+1)%N;\n    }\n}\nbool empty(){  //判断是否为空\n    return head == tail;\n}\nvoid pop_q(){  //对头删去一个元素\n    if(!empty())\n        head = (N + head + 1) % N;\n}\nvoid query(){  //查询对头元素\n    if(!empty())\n        printf(\"%d\\n\",q[head]);\n}","expanded":true}]},{"id":"f5f217c6-70e0-47d7-9e0d-02dbf15ea546","topic":"单调栈","expanded":true,"children":[{"id":"cca195c7-4332-440c-9189-c293c313b9af","topic":"应用场景：求某个数左边第一个小于他的数；\n思路：在每次暴力从for循环的当前值，向左遍历找第一个小于数的O(n2)情况下进行优化；\n            在向左遍历过程中删去无用的数（左边小于，但值大于），利用栈形成单调增大的序列，所求数即为栈顶；","expanded":true},{"id":"f451e00c-1c5d-42e2-8fe4-91a917e00cb8","topic":"for(int i=0;i<n;i++){\n    scanf(\"%d\",&v);\n    while(top&&s[top-1]>=v) top--;           //去除比当前数大但在左边的数\n    if(!top) printf(\"-1 \");\n    else printf(\"%d \",s[top-1]);                    //输出栈顶元素，即第一个小于当前数的数\n    s[top++]=v;                                          //当前数压入栈中\n}","expanded":true}]},{"id":"79a886db-947a-4f73-9e97-e0c89bd063da","topic":"单调队列","expanded":true,"style":{"branchColor":"#15831C"},"children":[{"id":"6186e095-24bb-431f-bbd4-4d000f31c1bd","topic":"应用场景：滑动窗口中最小值，也可优化背包问题；\n思路：同理通过删去无用的数进行优化；\n（求滑动窗口中最小值）队列头保留最小的数，遍历数组，若队尾数大于遍历的数则不断删去队尾（同理无用的数），使得队列单调增，在遍历过程中，不断更新队头使得不超过滑动窗口。","expanded":true},{"id":"221803fd-0379-4d38-a497-732523065fc3","topic":"for(int i=0;i<n;i++){\n   if(l<r&&i-q[l]>len-1) l++;                       //移动对头使不超过滑动窗口，为了实现，q数组存下标\n   while(l<r&&v[q[r-1]]>=v[i]) r--;              //弹出队尾无用数，因为会在队尾弹出不满足先进先出原则，事实上也不能叫单调队列\n   q[r++]=i;                                                  //每次都入队一个\n   if(i>=len-1) printf(\"%d \",v[q[l]]);\n}","expanded":true}]}]}]},"relations":[{"id":"5006e0da-265e-43b8-b5c0-8460806f6bf7","from":"c64d251e-ccfc-49de-b4b7-d81fa60888c0","to":"01839a19-4edb-46c7-bec0-bc59cf74396c","bidirectional":false,"label":"解决假溢出问题"}]}