Implement a stack. You can use any data structure inside a stack except stack itself to implement it.

Have you met this question in a real interview?

 
 
Example

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】的更多相关文章

  1. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  2. 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 ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  5. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  6. 141. Sqrt(x) 【easy】

    141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...

  7. 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 ...

  8. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  9. 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 ...

随机推荐

  1. Express重定向

    var express = require('express'); var app = express(); app.get('/',function(req,res){ res.redirect(' ...

  2. entity framework 去缓存

    MSDN上对MergeOption枚举的定义为: 成员名称 说明 AppendOnly 不会从数据源加载对象上下文中已存在的对象.这是查询或调用 EntityCollection<(Of < ...

  3. Linux内核开发者峰会照的全家福

    刚才看到一张Linux内核开发者峰会照的全家福,有历史价值,给大家分享一下.上面有Torvalds(大致在中间).Andrew Morton(目前的内核主要维护者,第二排右数第二个).Alan Cox ...

  4. Android Killer

    首先,我们先看一Android界有名的大神写关于Android反编译的博客: 郭 大 侠:http://blog.csdn.net/guolin_blog/article/details/497380 ...

  5. LINUX提权笔记

    没啥技术含量,一种思路,让某些得到root权限webshell的童鞋可以快点拿到权限. 某日拿到某站webshell,RedHat 系统,Tomcat程序是root身份启动的,webshell当然也拥 ...

  6. trilinear filter

    之前对三线型过滤理解有些问题更新一下 三线性采样点 一次sample采样点是8 (u,v,d) d是个分数 作为权重 miplevel floor(d)向上取整 在这级mipmap上用uv 采样4个点 ...

  7. spinner自定义,效果如腾讯QQ账号选择时候的下拉列表

         下拉列表在android中自带spinner的有时候不太适合我们的界面,我们希望有自己的一种显示方法,那怎么办?自定义Spinner.效果如QQ账号选择一样.如图所以. 这种效果,如果你喜欢 ...

  8. java 过滤器(自己的理解)

    filter继承javax.servlet.* 必须实现doFilter方法 chain.doFilter(request, response);这句话必须写在doFilter方法内部(以便调用其他的 ...

  9. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  10. 【原创】Android自定义适配器的使用方法

    比如说我们已经得到了数据,想在一个listview或者在其他的控件中显示的,并且我们显示想要自己设计样式来显示的话就要用到自定义适配器了,下面让我们结合代码讲一下具体的使用方法: 代码会有注释的哦: ...