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 ...
随机推荐
- API Monitor---------------Using API Monitor to crack copy protected software
For this tutorial we will be using Mirial Softphone which is a HD video conferencing application. Th ...
- ES6 import 引用文件夹/目录及其处理过程
1.现象 看redux的时候发现官网的教程里直接import了一个文件夹,我再三确定没有看错, 是一个 文件夹 (Directory), 它直接 import了一个目录!这个 文件夹/目录 底下还有一 ...
- 30分钟Git命令“从入门到放弃”
git 现在的火爆程度非同一般,它被广泛地用在大型开源项目中,但是初学者非常容易“从入门到放弃”,各种命令各种参数,天哪,宝宝要吓哭了.实际上新手并不需要了解所有命令的用途,学习是需要一个循序渐进的过 ...
- ios中Core Location跟Map Kit的基本使用
地图类开发应用中,离不开地理位置跟MKMapView的使用,下面就记录下自己在使用这两个东西中学到的. 不过并不是所有苹果的设备都支持地理位置,我们在使用前应该做个判断,代码如下: BOOL loca ...
- java 注解(自身理解)
声明注解 使用注解 解析注解 产生的结果 注解利用的是反射机制 ============================================================= 使用注解修饰 ...
- 基于jQuery的TreeGrid组件
/** * @author 陈举民 * @version 1.0 * @link http://chenjumin.iteye.com/blog/419522 */ TreeGrid = functi ...
- java中的Annotation
java中包含5个基本的Annotation: @Override @Deprecated @SuppressWarnings @SafeVarargs @FunctionalInterface …… ...
- JMeter 五:监控服务器
参考:http://jmeter.apache.org/usermanual/build-monitor-test-plan.html 添加Thread Group 添加方法:Test Plan上右键 ...
- vue - static(.gitkeep)
描述:git上传忽略的文件,与.gitnoignore一样.
- Activity生命周期以及启动模式对生命周期的影响
前天用户体验反馈的一个需求,要求每次进入应用都定位到首页;这个操作很明显不适合放在首页Activity(启动模式为SingleTask)的onResume中,如果对Activity的启动模式和生命周期 ...