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());
    	}
    }
    

      

      

  • 相关阅读:
    June 1. 2018 Week 22nd Friday
    【Android开发】【布局】几个常用布局构成的简单demo
    【Android开发】【布局】各种TabLayout样式
    【Android开发】【数据库】Realm For Android
    【Android开发】jarsigner重新打包apk
    【Android开发】Coding + git命令行基本使用
    【MarkDown】使用
    【Android Studio】Gradle统一管理版本号引用配置
    【Android开发】EasyPermissions 请求权限
    【Android开发】Android6.0请求权限方式
  • 原文地址:https://www.cnblogs.com/apanda009/p/7958860.html
Copyright © 2011-2022 走看看