chapter_stack_and_queue/stack/ #64
Replies: 62 comments 89 replies
-
想问一下,用链表实现栈的pop操作时,删除栈顶还需要把原来的栈顶的内存delete掉吗,或者说进行或不进行delete都行呢? |
Beta Was this translation helpful? Give feedback.
-
仅针对Java,给大家补充一点知识:Java中是有专门的栈实现类的,就是java.util.Vector,Stack在Java中是继承于Vector,这里说的是1.8版本,使用了Vector底层的数据结构,底层都是使用数组实现的。
以后大家在LeetCode中刷题时,会经常用到这个类,可以提前先了解一下。 |
Beta Was this translation helpful? Give feedback.
-
请问出栈的这个方法中返回被pop节点的值的意义是什么?
|
Beta Was this translation helpful? Give feedback.
-
c++的版本,pop接口应该增加int返回值并在实现最后return num,以保持设计逻辑的同步(栈 pop() 接口的设计习惯,一般都会返回出栈元素),要不然int num = top();就多余了。 没找到修改的办法,只好评论麻烦作者自己修改了 |
Beta Was this translation helpful? Give feedback.
-
Deque stack = new LinkedList<>();这样是否可以呢,如果是当作队列直接换个名称 |
Beta Was this translation helpful? Give feedback.
-
初学者很多只学C,想求个C语言的代码~非常感谢 |
Beta Was this translation helpful? Give feedback.
-
栈也可以用作括号检查 |
Beta Was this translation helpful? Give feedback.
-
好的,谢谢你 超级开心😁帮我解答了我得困惑
…---Original---
From: "Yudong ***@***.***>
Date: Fri, Apr 7, 2023 16:59 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [krahets/hello-algo] chapter_stack_and_queue/stack/ (Discussion#64)
Hi 也可以显式地将 next 设置为空,不设置也行,stackPeek = stackPeek.next; ,这一步后原来的 stackPeek 已经无法被访问到了,可以视为已经从链表中删除了。
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
典型应用中 |
Beta Was this translation helpful? Give feedback.
-
您好k哥,链表出栈的c语言代码, |
Beta Was this translation helpful? Give feedback.
-
手敲了一遍链表实现的栈,很有成就感 |
Beta Was this translation helpful? Give feedback.
-
在Java中,Stack是一种继承自Vector的类,用于实现后进先出(LIFO)的数据结构。然而并不推荐使用Stack,主要有以下几个原因:
综上所述,Java更倾向于使用其他数据结构来实现栈的功能,如ArrayList、LinkedList或ArrayDeque。这些数据结构提供了更好的性能、更多功能和更高的灵活性。 |
Beta Was this translation helpful? Give feedback.
-
作者您好! |
Beta Was this translation helpful? Give feedback.
-
作者你好, 在基于链表实现栈的rust 代码中 pub struct LinkedListStack<T> {
stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // 将头节点作为栈顶
stk_size: usize, // 栈的长度
} 根据上面的定义, 入栈操作应该是这样吧 /* 入栈 */
pub fn push(&mut self, num: T) {
let node = ListNode::new(num);
node.borrow_mut().next = self.stack_peek.take();
self.stack_peek = Some(Rc::new(RefCell::new(node))); <-----
self.stk_size += 1;
} |
Beta Was this translation helpful? Give feedback.
-
go代码里面,基于链表实现栈的操作,入栈使用PushBack()进行插入,那么这里就应该是 “将链表的尾节点视为栈顶,头节点视为栈底” |
Beta Was this translation helpful? Give feedback.
-
基于数组的实现中JS和TS的peek方法為何是叫top而不是peek呢?有什麼考慮嗎? |
Beta Was this translation helpful? Give feedback.
-
public class ArrayStack {
} |
Beta Was this translation helpful? Give feedback.
-
public class LinkStack {
} |
Beta Was this translation helpful? Give feedback.
-
做了一个可视化演示,欢迎来玩~ |
Beta Was this translation helpful? Give feedback.
-
入栈操作需要初始化节点对象并修改指针,因此效率相对较低。不过,如果入栈元素本身就是节点对象,那么可以省去初始化步骤,从而提高效率。 |
Beta Was this translation helpful? Give feedback.
-
你好,我想请问一下,在printStack函数中使用模板的意义所在,以及不使用模板的区别 |
Beta Was this translation helpful? Give feedback.
-
各位道友好: typedef struct ListNode { |
Beta Was this translation helpful? Give feedback.
-
为什么在python实现中,类的属性中stack前面要加个下划线? def init(self): |
Beta Was this translation helpful? Give feedback.
-
我使用的是C版本,对于链表和数组实现出栈的代码中,出栈的时候加上对栈判断是否为空是否更规范一些? |
Beta Was this translation helpful? Give feedback.
-
自己写的时候每次push都把新节点挂到top后面,结果写pop的时候卡住了,怎么想也想不到如何找到pop的前驱,甚至想到了双向链表。。。结果看作者的答案,每次push把top挂到新节点后面,怎么就没想到头插法呢,还是菜啊。。。。 |
Beta Was this translation helpful? Give feedback.
-
c++代码中,/* 初始化栈 */ |
Beta Was this translation helpful? Give feedback.
-
#ifndef __CUSTOMSTACK_HPP__
#define __CUSTOMSTACK_HPP__
/**
* @file customstack.hpp
* @brief 基于数组和链表的栈
* @example
* stackByList<int> stack{1, 2, 3, 4, 5, 6, 7};
* stackByVector<int> stack{1, 2, 3, 4, 5, 6, 7};
*/
#include <cstddef>
#include <vector>
#include <memory>
#include <chrono>
template<typename T>
class linklist;
template<typename T, template<typename> typename ContainerType = std::vector>
class customStack
{
public:
template<typename... Args>
customStack(Args&&... args)
{
(push(std::forward<Args>(args)),...);
};
~customStack(){};
void push(const T& v){ m_elements.push_back(v);}
void pop() { if (!m_elements.empty()) m_elements.pop_back();}
const T& top() const {return m_elements.back();}
T& top() {return const_cast<T&>(const_cast<const customStack*>(this)->top());}
bool empty() const {return m_elements.empty();}
std::size_t size() const {return m_elements.size();}
private:
ContainerType<T> m_elements;
};
template<typename T>
class linklist
{
struct linknode
{
T value;
std::shared_ptr<linknode> next;
linknode(const T& v, std::shared_ptr<linknode> node):value(v), next(node){};
};
public:
void push_back(const T& v)
{
m_top = std::make_shared<linknode>(v, m_top);
++m_size;
}
void pop_back()
{
m_top = m_top->next;
--m_size;
}
const T& back() const {return m_top->value;}
bool empty() const {return m_size == 0;}
std::size_t size() const {return m_size;}
private:
std::size_t m_size{0};
std::shared_ptr<linknode> m_top{nullptr};
};
template<typename T>
using stackByVector = customStack<T>;
template<typename T>
using stackByList = customStack<T, linklist>;
#endif //__CUSTOMSTACK_HPP__ |
Beta Was this translation helpful? Give feedback.
-
pop函数中 |
Beta Was this translation helpful? Give feedback.
-
chapter_stack_and_queue/stack/
Your first book to learn Data Structure And Algorithm.
https://www.hello-algo.com/chapter_stack_and_queue/stack/
Beta Was this translation helpful? Give feedback.
All reactions