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. php概率算法

    这是一个很经典的概率算法函数: function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr) ...

  2. oracle 查看执行最慢 sql

    查询执行最慢的sql select * from (select sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS "执行次数", round ...

  3. golang解析json格式 -- 全

    项目中客户端和服务端的交互数据部分为json,因此在服务端就得解析,复杂的json解析起来其实还是挺费劲的. 交互的数据类似如下格式: {"sn":1,"ls" ...

  4. What is required for a successful backup of all files during hoi backup?

    There is a typo in the body of this question. It should be "Hot" instead of "hoi" ...

  5. apache无法启动:The request operation has failed

    apache无法启动提示the requested operation has failed 的错误信息,有以下几种解决方法:1.80端口占用 apache默认使用的端口是80,而IIS和迅雷用的也是 ...

  6. 云计算之路-试用Azure-飞流直下三千尺:实测虚拟机磁盘IO

    Azure的Temporary Storage(临时存储)磁盘的IO速度曾经是个传说,只知道它很快,但不知道究竟有多快.而Azure中国的情况怎么样,我们来实测一下. 测试环境:Azure上海机房,1 ...

  7. python __slot__

    class Player(object): def __init__(self,name,age,life): self.name=name self.age=age self.life=life c ...

  8. vue - utils.js

    exports:导出功能函数或变量 module.exports:默认导出{} ------------------------------------------------------------ ...

  9. UVA 10168 Summation of Four Primes(数论)

    Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...

  10. Android中Word转Html

    一.POI方式 1.先看word效果图 2.再看下在android上使用WebView显示的效果   3. 生成的html的代码,如下: <html> <head> <M ...