알고리즘 문제 4 단일 체인 테이블로 대기열 실현
                                            
 15144 단어  테스트 개발 칼럼
                    
package com.interview;
public class LinkQueue<T> {
     
	class Node<T> {
     
		private T data;
		private Node<T> next;
		public Node() {
     
			this.data = null;
			this.next = null;
		}
		public Node(T data) {
     
			this.data = data;
			this.next = null;
		}
		public T getData() {
     
			return data;
		}
		public void setData(T data) {
     
			this.data = data;
		}
		public Node<T> getNext() {
     
			return next;
		}
		public void setNext(Node<T> next) {
     
			this.next = next;
		}
	}
	private Node<T> head;
	private Node<T> tail;
	public LinkQueue() {
     
		this.head = null;
		this.tail = null;
	}
	//  
	public boolean inQueue(T t) {
     
		//  。
		Node<T> p = new Node<T>(t);
		if (head == null) {
     
			head = p;
			tail = p;
		} else {
     
			tail.next = p;
			tail = tail.next;
		}
		return true;
	}
	public T outQueue() {
     
		if (head == null)
			return null;
		else {
     
			T t = head.getData();
			head = head.getNext();
			return t;
		}
	}
	//  
	public T peek() {
     
		if (head == null)
			return null;
		else {
     
			return head.getData();
		}
	}
	//  
	public boolean isEmpty() {
     
		return head == null;
	}
	//   push pop
}
  테스트를 진행하다
package test.interview;
import com.interview.LinkQueue;
public class LinkQueueTest {
     
    public static void main(String[] args) {
     
		
    	
		LinkQueue<Integer> linkQueue=new LinkQueue<Integer>();
        for (int i = 0; i < 10; i++) {
     
        	linkQueue.inQueue(i);
        	System.out.println(linkQueue.peek());
		}
        
        for (int i = 0; !linkQueue.isEmpty(); i++) {
     
			System.out.println(linkQueue.peek()+"  "+linkQueue.outQueue());
			
		}
	}
}
  0
0
0
0
0
0
0
0
0
0
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
nginx 정적 자원 서버 nginx. conf 설정 구축user root; worker_processes auto; #error_log /var/log/nginx/error.log; #pid /run/nginx.pid; # Load dynamic modules. See ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.