栈实现

链表实现
头插插入链表
每次删除头结点
python

class Node(object):
    def __init__(self, value):
        self.val = value
        self.next = None

class Stack(object):
    def __init__(self):
        self.head = Node(None)

    def isempty(self):
        return self.head.val == None

    def push(self,value):
        temp = Node(value)
        temp.next = self.head
        self.head = temp
    def pop(self):
        if(self.isempty()): raise SyntaxError('Stack is Empty!')
        res = self.head.val
        self.head = self.head.next
        return res

s = Stack()
s.push(1)
s.push(2)
s.push(3)
print(s.pop())
s.push(4)
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())

java

/**
 * Created by qywtg on 2019/5/13.
 */
public class Stack {
    private Node first = null;
    private class Node{
        String item;
        Node next;
    }

    public boolean isEmpty(){return first == null;}
    public void push(String item){
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }
    public String pop(){
        String item = first.item;
        first = first.next;
        return item;
    }

    public static void main(String[] args) {
        Stack s = new Stack();
        s.push("1");
        s.push("2");
        s.push("3");
        s.push("4");
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
    }
}

java数组实现

/**
 * Created by qywtg on 2019/5/13.
 */
public class Stack {
    private String[] arr;
    private int N;

    public Stack(int capacity) {
        arr = new String[capacity];
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public void push(String item) {
        arr[N++] = item;
    }

    public String pop() {
        String item = arr[--N];
        arr[N] = null;
        return item;
    }


    public static void main(String[] args) {
        Stack s = new Stack(10);
        s.push("1");
        s.push("2");
        s.push("3");
        s.push("4");
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
    }
}

动态增加数组长度和动态缩小数组长度

/**
 * Created by qywtg on 2019/5/13.
 */
public class Stack {
    private String[] arr;
    private int N;

    public Stack() {
        arr = new String[1];
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public void push(String item) {
        if (N == arr.length) resize(2 * arr.length);
        arr[N++] = item;
    }

    public String pop() {
        String item = arr[--N];
        arr[N] = null;
        if (N > 0 && N == arr.length / 4) resize(arr.length / 2);
        return item;
    }

    private void resize(int capacity) {
        String[] copy = new String[capacity];
        for (int i = 0; i < N; i++) {
            copy[i] = arr[i];
        }
        arr = copy;
    }


    public static void main(String[] args) {
        Stack s = new Stack();
        s.push("1");
        s.push("2");
        s.push("3");
        s.push("4");
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。