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 ...
随机推荐
- 翻译:Spring-Framework-Reference Document:15.2-DispatcherServlet
写在前面的话: 最近被项目的代码折腾的死去活来的,其实框架也没有那么难理解,只是自己的Web基础太差,被Request和Response这一对神雕侠侣坑到泪流满面!今天捣腾了一下Spring We ...
- hive删除列
hive中删除列时没有与mysql语句alter table <table> drop column <col>对应的语句. 然而依然可以完成此功能:使用ALTER TABLE ...
- redux状态管理和react-redux的结合使用
一:调试 注意:Redux调试工具.谷歌中搜redux同理react 新建store的时候判断window.devToolsExtension使用compose(组合函数)结合thunk插件和wind ...
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- 各种软核处理器二进制文件FPGA初始化文件生成程序
不管是MIPS, Nios II, MicroBlaze, MSP430, 8051, OpenRISC, OpenSPARC, LEON2/LEON3等等软核处理器,在FPGA上实现的时候我们通常需 ...
- http://blog.sina.com.cn/s/blog_4a5dbd380101f031.html
http://blog.sina.com.cn/s/blog_4a5dbd380101f031.html mvn clean install
- PhantomJS + Selenium webdriver 总结-元素定位
webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要高些,使用id,name属性定位元素是最可靠,效率最高的一 ...
- JNI_Android项目中调用.so动态库
JNI_Android项目中调用.so动态库 2014年6月3日 JNI学习 參考:http://blog.sina.com.cn/s/blog_4298002e01013zk8.html 上一篇笔者 ...
- MapReduce报错:Error: java.io.IOException: Initialization of all the collectors failed. Error in last collector was :interface javax.xml.soap.Text
这个问题折腾了我一天了,最主要的是真的不懂在哪里看log,所以虽然折腾地很没有道理但还是一直在折腾. 这个问题很好解决,在main函数处错误import了 javax.xml.soap.Text,应该 ...
- 页面嵌入QQ功能(点QQ建立一个临时会话,显示在页面的固定位置)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...