使用递归对栈进行排序2024 年 8 月 28 日 | 阅读 17 分钟 栈是一种线性数据结构,遵循后进先出 (LIFO) 原则。这意味着最后添加到栈中的元素会首先被删除。栈的另一个词是 LIFO,它指的是元素从栈中移除的顺序(后进先出)。这种结构被称为“栈”,因为它类似于一堆相互堆叠的真实物品。这种结构使得从栈顶移除一个元素很简单,但从栈底移除一个元素可能需要先移除许多其他元素。窥视操作也可以在不改变栈的情况下访问栈顶元素。 对栈进行排序对于各种任务都很有用,包括内存管理、在发生中断时维护进程的上下文以及其他高优先级任务。尽管我们在此将看到递归方法,但排序也可以迭代地完成。 栈中的操作在栈中,我们可以执行以下操作,例如:
栈是一种非常有用且关键的数据结构,它用于内存管理和进程流调度。程序计数器是栈最重要的应用之一,因为它保存了处理器代码的上下文,以防在需要转换到新进程时将其堆叠起来,以便在新进程完成时可以返回旧进程并完成它。 递归是最重要的算法之一,因为如果我们能解决一个较小的任务,我们几乎肯定可以利用较小的任务来解决整个项目。递归是一个术语,指的是自我调用的行为。它有一个基本情况,这是主要情况,它处理较小的问题场景,然后为较小的部分调用自身。它利用了这样一个事实:当我们处于某种状态时,我们假设我们的递归函数已经处理了较小的响应,现在我们可以将这些响应组合起来解决我们当前的状态。 Java 代码让我们编写一个 Java 代码,使用递归对栈进行排序。 输出 上面的 Java 代码给出以下输出。 Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 32 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 98 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 22 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 49 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 13 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 36 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 54 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 102 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 1 Enter integer element to insert 506 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 2 Stack:: [45, 32, 98, 22, 49, 13, 36, 54, 102, 506] Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 3 Sort done. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new value in the Stack Data Structure. 2. To display the data inside of the Stack Data Structure. 3. To perform sorting (via Recursion) on the Stack Data Structure. 2 Stack:: [13, 22, 32, 36, 45, 49, 54, 98, 102, 506] Do you want to continue (Type y or n) n 通过这种方式,我们编写了 Java 代码来对栈数据结构执行排序操作。为栈数据结构编写了三个函数,一个是向栈数据结构中添加新节点,第二个是显示栈数据结构中存在的所有内容,第三个也是最后一个函数是对栈数据结构执行排序操作。 用户首先在栈数据结构中添加足够的数据。一旦节点成功添加,通过从每次操作后显示菜单中选择第三个选项,对栈数据结构执行排序操作,这将调用代码中编写的 sort() 函数,并将栈数据结构的对象作为参数传递。 排序操作成功完成后,用户可以通过从用户处选择第二个选项来显示栈数据结构中节点的值,以确认排序操作的结果。一旦所有操作完成,用户可以通过输入“n”或“N”字符退出代码。 C++ 代码现在让我们编写一个 C++ 代码,使用递归对栈进行排序。 输出 上面的 C++ 代码产生以下输出。 Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 32 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 97 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 40 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 21 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 67 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 30 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 505 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 672 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 341 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 1 Enter the value to be inserted 231 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 2 Contents of the stack are:: 231 341 672 505 30 67 21 40 97 32 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 3 Sort did successfully. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform sorting on the Stack Data Structure. 2 Contents of the stack are:: 672 505 341 231 97 67 45 40 32 30 21 Do you want to continue (Type y or n) n 通过这种方式,我们编写了 C++ 代码来对栈数据结构执行排序操作。为栈数据结构编写了三个函数,一个是向栈数据结构中添加新节点,第二个是显示栈数据结构中存在的所有内容,第三个也是最后一个函数是对栈数据结构执行排序操作。 用户首先在栈数据结构中添加足够的数据。一旦节点成功添加,通过从每次操作后显示菜单中选择第三个选项,对栈数据结构执行排序操作,这将调用代码中编写的 sort() 函数,并将栈数据结构的对象作为参数传递。 排序操作成功完成后,用户可以通过从用户处选择第二个选项来显示栈数据结构中节点的值,以确认排序操作的结果。一旦所有操作完成,用户可以通过输入“n”或“N”字符退出代码。 C 代码现在让我们编写一个简单的 C 代码,使用递归对栈进行排序。 输出 上面的 C 代码给出此输出。 Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 45 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 9 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 32 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 87 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 20 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 18 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 1 Enter the value to be inserted 56 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 2 Stack elements: 56 18 20 87 32 9 45 12 Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 3 Sort applied successfully. Do you want to continue (Type y or n) y Select one of the operations:: 1. To insert a new node in the Stack Data Structure. 2. To display the nodes of the Stack Data Structure. 3. To perform a sort on the Stack Data Structure. 2 Stack elements: 87 56 45 32 20 18 12 9 Do you want to continue (Type y or n) N 通过这种方式,我们编写了 C 代码来对栈数据结构执行排序操作。为栈数据结构编写了三个函数,一个是向栈数据结构中添加新节点,第二个是显示栈数据结构中存在的所有内容,第三个也是最后一个函数是对栈数据结构执行排序操作。 用户首先在栈数据结构中添加足够的数据。一旦节点成功添加,通过从每次操作后显示菜单中选择第三个选项,对栈数据结构执行排序操作,这将调用代码中编写的 sort() 函数,并将栈数据结构的对象作为参数传递。 排序操作成功完成后,用户可以通过从用户处选择第二个选项来显示栈数据结构中节点的值,以确认排序操作的结果。一旦所有操作完成,用户可以通过输入“n”或“N”字符退出代码。 下一主题数据结构中的 LIFO 方法 |
我们请求您订阅我们的新闻通讯以获取最新更新。