chapter_stack_and_queue/stack/ #64
Replies: 66 comments 91 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.
-
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.
-
JS 版本: isEmpty() { push(value) {
} class ArrayStack{ |
Beta Was this translation helpful? Give feedback.
-
之前学data structure的时候只是单纯学习了stack的概念&代码,现在结合应用、array与linked list的对比就一下子变得更有趣了! |
Beta Was this translation helpful? Give feedback.
-
在linkedlist_stack.cpp好像没有定义freeMemoryLinkedList()这个函数,我写成(C++版本) |
Beta Was this translation helpful? Give feedback.
-
Java数组实现: public class ArrayStacks<E> {
//真实存储元素的数组
private Object[] array;
//栈大小
private int size = 0;
/* 一、初始化 */
//空参构造,初始默认长度为20
public ArrayStacks() {
this.array = new Object[20];
}
//有参构造,接收Collection接口的实现类初始化数组元素
public ArrayStacks(Collection<? extends E> c) {
Object[] a = c.toArray();
if ((size = a.length) != 0) {
if (c.getClass() == ArrayList.class) {
array = a;
} else {
array = Arrays.copyOf(a, size, Object[].class);
}
} else {
// replace with empty array.
array = new Object[]{};
}
}
/* 二、获取栈大小 */
public int size() {
return size;
}
/* 三、判断栈是否为空 */
public boolean isEmpty() {
return size==0;
}
/* 四、入栈 */
public void push(E e) {
if (size == array.length) {
//扩容
expansion();
}
array[size] = e;
size += 1;
}
/* 五、出栈 */
@SuppressWarnings("unchecked")
public E pop() {
if(isEmpty()) {
throw new IndexOutOfBoundsException();
}
size -= 1;
return (E) array[size];
}
/* 六、访问栈顶元素 */
@SuppressWarnings("unchecked")
public E peek() {
if(isEmpty()) {
throw new IndexOutOfBoundsException();
}
return (E) array[size-1];
}
/* 七、将 List 转化为 Array 并返回 */
public Object[] toArray() {
Object[] t = new Object[size];
for (int i = 0; i < t.length; i++) {
t[i] = array[i];
}
return t;
}
//数组扩容(如果扩容到大于int的取值范围后怎么办???)
private void expansion() {
Object[] t = new Object[(int) Math.ceil(size * 1.5)];
for (int i = 0; i < array.length; i++) {
t[i] = array[i];
}
array = t;
}
} |
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