zoukankan      html  css  js  c++  java
  • midStack

    public class MidStack<T> {
    	static class Node<T>{
    		T val;
    		Node<T> next = null;
    		Node<T> pre = null;
    		public Node(T val){
    			this.val = val;
    		}
    	}
    	private int size = 0;
    	private Node<T> head = null;
    	private Node<T> tail = null;
    	private Node<T> mid = null;
    	
    	public T pop() {
    		if (tail == null) {
    			return null;
    		}
    		Node<T> temp = tail;
    		if (tail == head) {
    			tail = null;
    			head = null;
    			mid = null;
    		} else {
    			tail.pre.next = null;
    			if (size % 2 != 0) {
    				mid = mid.pre;
    			}
    			tail = tail.pre;
    		}
    		size--;
    		return temp.val;
    	}
    	
    	public void push(T val) {
    		Node<T> newNode = new Node<>(val);
    		if (head == null) {
    			head = newNode;
    			tail = newNode;
    			mid = newNode;
    		} else {
    			tail.next = newNode;
    			newNode.pre = tail;
    			tail = newNode;
    			if (size % 2 == 0) {
    				mid = mid.next;
    			}
    		}
    		size++;
    	}
    	
    	public T peekMid() {
    		if (mid == null) {
    			return null;
    		}
    		return mid.val;
    	}
    	
    	public T popMid() {
         if(size == 0) {
           return null;
         }
         if (size == 2) {
           T temp = mid.val;
           head = tail;
           mid = tail;
           size--;
           return temp;
         }
    
    		if (size == 1) {
    		    T temp = mid.val;
    		    mid = head = tail = null;
           size--;
        		    return temp;
    		}
    		mid.pre.next = mid.next;
    		mid.next.pre = mid.pre;
    		if (size % 2 == 0) {
    			mid = mid.next;
    		} else {
    			mid = mid.pre;
    		}
    		size--;
    		return mid.val;
    	}
    	
    	public String toString() {
    		if (head == null) {
    			return "";
    		}
    		Node<T> cur = head;
    		StringBuilder sb = new StringBuilder();
    		while (cur != null) {
    			sb.append(cur.val).append(',');
    			cur = cur.next;
    		}
    		sb.deleteCharAt(sb.length() - 1);
    		return sb.toString();
    	}
    	
    	public static void main(String[] args) {
    		MidStack<Integer> ms = new MidStack<>();
    		ms.push(1);
    		ms.push(2);
    		ms.push(3);
    		ms.pop();
    		ms.pop();
    		System.out.println("stack: " + ms);
    		System.out.println("mid: " + ms.peekMid());
    		ms.popMid();
    		System.out.println("stack: " + ms);
    		System.out.println("mid: " + ms.peekMid());
    	}
    }
    

      

      

  • 相关阅读:
    Java使用POI插件将数据以excel形式备份
    bzoj1611[Usaco2008 Feb]Meteor Shower流星雨*
    bzoj1603[Usaco2008 Oct]打谷机*
    bzoj1599[Usaco2008 Oct]笨重的石子*
    bzoj1230[Usaco2008 Nov]lites 开关灯*
    bzoj4002[JLOI2015]有意义的字符串
    bzoj1613[Usaco2007 Jan]Running贝茜的晨练计划*
    bzoj1602[Usaco2008 Oct]牧场行走*
    bzoj1715[Usaco2006 Dec]Wormholes 虫洞*
    bzoj2442[Usaco2011 Open]修剪草坪*
  • 原文地址:https://www.cnblogs.com/apanda009/p/7958860.html
Copyright © 2011-2022 走看看