chapter_stack_and_queue/stack/ #64
Replies: 48 comments 73 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.
-
//利用双端栈实现类似网页浏览前进后退的功能
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct page_manager
{
char **page;
int lefttop;
int righttop;
};
struct page_manager* init(){
struct page_manager* p=(struct page_manager*)malloc(sizeof(struct page_manager));
p->page=(char**)malloc(sizeof(char*)*MAX);
p->lefttop =-1;
p->righttop = MAX;
}
void add(struct page_manager *p,char *page){//增加新的网页
if(p->lefttop == p->righttop-1){
printf("stack is full!");
return;
}
p->page[++p->lefttop] = page;
}
char *forward(struct page_manager* p){ //按前进键
if(p->righttop == MAX) return NULL;
char *page = p->page[p->righttop++];
p->page[++p->lefttop] = page;
return page;
}
char *backward(struct page_manager* p){ //按后退键
if(p->lefttop == -1) return NULL;
char *page=p->page[p->lefttop--];
p->page[--p->righttop] = page;
return page;
}
int main(){
struct page_manager* p =init();
add(p,"page1");add(p,"page2");add(p,"page3");
add(p,"page4");add(p,"page5");add(p,"page6");
int oper = 1;
while(oper!=0){
printf("input:");
scanf("%d",&oper);
if(oper == 1) printf("%s\n",forward(p));
else if(oper == -1) printf("%s\n",backward(p));
}
} |
Beta Was this translation helpful? Give feedback.
-
好好好,感谢作者 |
Beta Was this translation helpful? Give feedback.
-
链表实现栈的代码中,C语言实现ListNode并未定义; |
Beta Was this translation helpful? Give feedback.
-
动态数组实现栈的操作过程中,MAX_SIZE未定义,出栈时并未做判空处理;另外数组动态扩容功能并没有实现; |
Beta Was this translation helpful? Give feedback.
-
析构函数是删除该数据结构吗,为什么要叫析构啊 |
Beta Was this translation helpful? Give feedback.
-
链表实现的栈,List 转 Array 用 迭代器会比递归调用好吧?
|
Beta Was this translation helpful? Give feedback.
-
浏览器的前进后退,不用出栈操作,用数组形式来实现,单独维护当前页的数组索引就可以实现前进后退了吧,一个数组就行 |
Beta Was this translation helpful? Give feedback.
-
js的pop实现是不是将出栈的节点的next置为null,更合理一些? |
Beta Was this translation helpful? Give feedback.
-
这里的入栈,新建一个节点,然后指向头节点(栈顶),然后这个新建节点的地址赋给了栈顶,不会形成环形链表吗,是哪里理解错了吗? |
Beta Was this translation helpful? Give feedback.
-
def is_empty(self) -> bool:
"""判断栈是否为空"""
return self._stack == [] python这里是不可以直接return self._stack,判断是否相等是不有点多余? |
Beta Was this translation helpful? Give feedback.
-
在初始化列表时,系统会为列表分配“初始容量”,该容量可能超出实际需求。 |
Beta Was this translation helpful? Give feedback.
-
找个半天才发现是一个语法糖,展开运算符(集合表达式中的 ..)可将其参数替换为该集合中的元素。 |
Beta Was this translation helpful? Give feedback.
-
基于链表的实现代码中出现一个报错:未定义标识符freeMemoryLinkedList,请问该函数定义在哪里呢? |
Beta Was this translation helpful? Give feedback.
-
5.1.2.1提到对于入栈操作,我们只需将元素插入链表头部,这种节点插入方法被称为“头插法”。意思尾插法也可以实现栈吗,我查资料说,头插法适用于栈FILO,因为它存储链表是逆序的,尾插法适用于队列这种先进先出FIFO的,因为它是顺序的,还是说头插法和尾插法都能用来实现栈,也都能用来实现队列呢? |
Beta Was this translation helpful? Give feedback.
-
打卡打卡,25一定上岸呀 |
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