Learning Stack data structure [Java]

所在平台: Udemy

课程主页: https://www.udemy.com/course/stack-data-structure/

课程评论:没有评论

第一个写评论        关注课程

课程简介

Coursera课程《Learning Stack data structure [Java]》课程总结: 本课程主要介绍了Java中栈(Stack)数据结构的基础知识和应用。 **核心概念:** * **定义:** 栈是一种遵循“后进先出”(LIFO)原则的线性数据结构,所有元素必须是相同的数据类型。 * **声明与创建:** 在Java中,可以使用 `Stack variable_name = new Stack();` 来声明和创建栈对象。 **常用方法:** * `push(element)`: 向栈顶添加一个元素。 * `peek()`: 查看栈顶元素,但不移除。 * `pop()`: 移除并返回栈顶元素。 * `size()`: 返回栈中元素的数量。 * `empty()`: 判断栈是否为空,返回true(空)或false(非空)。 * `clear()`: 清空栈中的所有元素。 * `contains(value)`: 检查栈中是否存在指定值,返回true或false。 * `remove(index)`: 移除指定索引位置的元素。 * `search(value)`: 搜索指定值在栈中的位置(从栈顶开始计数),返回索引(如果找到),否则返回-1。 **遍历栈:** * **逐个弹出:** 通过循环调用 `pop()` 方法,逐个取出栈顶元素进行处理。 * **For-each 循环:** 可以使用增强for循环遍历栈,但需要注意遍历方向是从栈底到栈顶。 **栈的适用场景:** * **表达式求值与匹配:** 如括号匹配、中缀表达式转后缀表达式等。 * **模拟回退操作:** 如键盘上的退格键、浏览器的后退功能。 * **有序删除:** 需要删除最后添加的元素时。 * **回溯算法:** 在解决需要“前进”和“后退”多步的问题时。 * **查找峰值元素:** 在某些类图结构中,当需要查找“峰值”时,栈可以起到辅助作用。

课程评论(0条)

课程详情

Stack:Stack stores elements of the same data type.We can insert, and remove elements in the stack in the Last In First Out policy (LIFO)DeclarationStack variable_name = new Stack();Stack stack = new Stack();Methods used in Stack:stack_name. push() - insert element into a stackstack_name. peek() - checks the topmost element in the stackstack_name. pop() - removes the topmost element in the stackstack_name. size() - returns the number of elements in stackstack_name. empty() - returns true / false. if stack is empty, it returns true. if stack is not empty it retuns falsestack_name. clear() - clears the stackstack_name. contains(value) - returns true if the value we check is there in stack or else it returns false stack_name remove(index) - removes the value in given indexstack_name. search(value) - searches the value in stack and returns us the index of the value if present. Iterating stack:We can go through the stack elements in two ways.1. Popping the elements We can pop the stack elements one by one and go through all the elements.2. For each loopWe can go through the stack by a for loop as belowfor(String s: stack_name){//Stack will be iterated from bottom to top direction in this way}How to identify if that problem can be solved using a stack1. when we want to evaluate brackets, expressions in certain order we can use stacks.2. When we wanted to use backspace character in keyboard or any similar sitautions, we can use stacks3. When we want to delete or remove the last elements we can use stack.4. When we want to backtrack to something and then again to move forward direction multiple times we can use stack.5. We can use stack to find some peak elements (assuming the value plotted something like graph) we can use stacks

课程标签

0人关注该课程

主题相关的课程