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 ...
随机推荐
- JavaScript基础入门教程(二)
说明 前一篇博客介绍了js以及一些关于js基本类型的简单知识,本篇博客将详细介绍js的基础类型,捎带介绍对象类型,更详细的对象类型的说明将后续再讲. js中类型的说明 js中的类型分为基本类型和对象类 ...
- 深入浅出RxJava就这一篇就够了
前言: 第一次接触RxJava是在前不久,一个新Android项目的启动,在评估时选择了RxJava.RxJava是一个基于事件订阅的异步执行的一个类库.听起来有点复杂,其实是要你使用过一次,就会大概 ...
- 《Go语言实战》笔记之第四章 ----数组、切片、映射
原文地址: http://www.niu12.com/article/11 ####数组 数组是一个长度固定的数据类型,用于存储一段具有相同的类型的元素的连续块. 数组存储的类型可以是内置类型,如整型 ...
- javascript 获取http头信息
Javascript中跟response header有关的就两个方法: getResponseHeader 从响应信息中获取指定的http头 语法 strValue = oXMLHttpReques ...
- c#中的数组、ArrayList、List区别
首先说明C#中的Array类:Array 类是 C# 中所有数组的基类,它是在 System 命名空间中定义.Array 类提供了各种用于数组的属性和方法.关于Array类的一些属性及方法详见博文:C ...
- 【云计算】Docker容器时间同步如何配置?
示例: # 3.8 配置时区.时钟同步 # configure timezone & ntp RUN echo ${TIME_ZONE} > /etc/timezone \ && ...
- errno , perror,strerror
1. 简介 很多系统函数在错误返回时将错误原因记录在libc定义的全局变量errno中,每种错误原因对应一个错误码. errno在头文件errno.h中声明,是一个整型变量,所有错误码都是正整数. 然 ...
- ubuntu16.04彻底卸载mysql并且重新安装mysql
首先删除mysql: sudo apt-get remove mysql-* dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 清理完毕: ...
- Spring Boot + Spring Data + Elasticsearch实例
Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...
- LeetCode-NQueensII
经典的N皇后问题, 这里学到了一个非常牛的新方法(http://www.matrix67.com/blog/archives/266),用位运算来求解N皇后问题: 思路其实也很容易懂,一点都不复杂, ...