495. Implement Stack【easy】
Implement a stack. You can use any data structure inside a stack except stack itself to implement it.
push(1)
pop()
push(2)
top() // return 2
pop()
isEmpty() // return true
push(3)
isEmpty() // return false
解法一:
class Stack {
public:
struct Node {
int val;
Node *prev, *next;
Node(int v) {
val = v;
prev = NULL;
next = NULL;
}
}; Node *dummy;
Node *tail;
Stack() {
dummy = new Node();
tail = dummy;
} ~Stack() { } void push(int x) {
tail->next = new Node(x);
tail->next->prev = tail;
tail = tail->next;
} // Pop the top of the stack
void pop() {
if (dummy->next == NULL) {
return;
}
Node *prev = tail->prev;
prev->next = NULL;
tail = prev;
} // Return the top of the stack
int top() {
if (dummy->next == NULL) {
return -;
}
return tail->val;
} // Check the stack is empty or not.
bool isEmpty() {
if (dummy->next == NULL) {
return true;
}
return false;
}
};
用链表实现栈,因为栈pop出的是最后一个,如果用尾插法的单向链表明显不太方便,所以用双向链表。
参考@YI 的代码
https://yixuanwangblog.wordpress.com/2016/09/04/lintcode-495-implement-stack/
解法二:
class ListNode{
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}; public class Stack {
ListNode head;
public Stack(){
head = new ListNode(0);
} /*
* @param x: An integer
* @return: nothing
*/
public void push(int x) {
ListNode node = new ListNode(x);
node.next = head.next;
head.next = node;
} /*
* @return: nothing
*/
public int pop() {
ListNode top = head.next;
head.next = head.next.next;
return top.val;
} /*
* @return: An integer
*/
public int top() {
return head.next.val;
} /*
* @return: True if the stack is empty
*/
public boolean isEmpty() {
if (head.next == null) {
return true;
} else{
return false;
}
}
}
用链表实现栈,使用头插法。
参考@zhengyang2015 的代码
https://zhengyang2015.gitbooks.io/lintcode/implement_stack_495.html
495. Implement Stack【easy】的更多相关文章
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 141. Sqrt(x) 【easy】
141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
随机推荐
- \r和\n的区别
/n 是换行/r 是回车 这是两码事,换行是指移动到下一行,回车是指移动到行首,我们通常所说的“回车”其实是这两个动作的结合.
- php根据word模板生成新的word文件
原文地址:http://www.niu12.com/article/16 php使用phpword将word内容变量替换 a.安装phpword composer require phpoffice/ ...
- [转]Sql Server 主从数据库配置
本文转自:http://www.cnblogs.com/yukaizhao/archive/2010/06/02/sql-server-master-slave-mode.html 网站规模到了一定程 ...
- 探讨android更新UI的几种方法(转)
作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...
- Linux环境下搭建MYSQL数据库指令详情
一.mysql数据库的安装 确保安装gcc(开发工具) #groupadd mysql #useradd -g mysql mysql #cd /usr/local # tar -zxvf mysql ...
- 安装red5 1.0.1版本Java_home不能用Java7
安装red5 1.0.1一直出现问题,安装顺利可以过,但是一访问老是报错. 用1.0之前的版本则没有问题.好一顿折腾,查看log发现问题出在tomcat 的nio上,查询这个问题有回复说是jr ...
- (转)NIO 内存映射文件
内存映射文件 I/O 是一种读和写文件数据的方法,它可以比常规的基于流或者基于通道的 I/O 快得多. 内存映射文件 I/O 是通过使文件中的数据神奇般地出现为内存数组的内容来完成的.这其初听起来似乎 ...
- 轻量级的前端UI开发框架 - UIkit
来源:GBin1.com UIkit是YOOtheme团队开发的开源的前端UI界面框架,可以帮助你快速的开发和创建前端UI界面. 基于下列开源项目: LESS jQuery normalize.css ...
- C#运行原理——我的柔情你永远不懂
记得歌手陈琳曾经在1993年发行了第一张专辑<你的柔情我永远不懂>,创造了150万张的销售纪录,里边的主打歌——我的柔情你永远不懂,多年以后才发现是写给C#运行原理的,因为原理总是伤不起~ ...
- 如何设置outlook实现自动秘密抄送邮件的功能?
很多朋友会发现虽然在家里同步了公司的邮箱可以正常收发邮件,可是每当使用家里的outlook发送相关邮件的时候,在公司的邮箱里找不到相关的发件记录,只能同步收件箱,而不能同步发件箱应该是比较让人困扰的问 ...