대기열에 대한 Java 언어 설명

2397 단어
class Node{// 
	private int m_Data;
	private Node m_Next;
	
	Node(int data){
		m_Data=data;
		m_Next=null;
	}
	Node(int data,Node next){
		m_Data=data;
		m_Next=null;
	}
	void setData(int data){
		m_Data=data;
	}
	int getData(){
		return m_Data;
	}
	void setNext(Node next){
		m_Next=next;
	}
	Node getNext(){
		return m_Next;
	}
}

class LinkList{// 
	Node m_FirstNode;
	
	LinkList(){
		m_FirstNode=null;
	}
	LinkList(int data){
		m_FirstNode=new Node(data);
	}
	String visitAllNode(){
		Node next=m_FirstNode;
		String s="";
		while(next!=null){
			s=s+next.getData()+"; ";
			next=next.getNext();
		}
		return s;
	}
	void insertAtBegin(int data){
		if(m_FirstNode==null)
			m_FirstNode=new Node(data);
		else 
			m_FirstNode=new Node(data,m_FirstNode);
	}
	void insertAfterId(int data,int id){
		Node next=m_FirstNode;
		if(next==null)
			m_FirstNode=new Node(data);
		else{
			while(next.getNext()!=null && next.getData()!=id)
				next=next.getNext();
			next.setNext(new Node(data,next.getNext()));
		}
	}
	boolean removeAtId(int id){
		Node ahead=m_FirstNode;
		Node follow=ahead;
		
		if(ahead==null)
			return false;
		else if(ahead.getData()==id){
			m_FirstNode=m_FirstNode.getNext();
			return true;
		}
		else{
			ahead=ahead.getNext();
			while(ahead!=null){
				if(ahead.getData()==id){
					follow.setNext(ahead.getNext());
					return true;
				}
				follow=ahead;
				ahead=ahead.getNext();
			}
			return false;
		}
	}
	void  removeAll(){
		m_FirstNode=null;
	}
}
class Queue extends LinkList{// 
	boolean isEmpty(){
		if(m_FirstNode==null)
			return true;
		else
			return false;
	}
	void enQueue(int newdata){
		Node next=m_FirstNode;
		if(next==null)
			m_FirstNode=new Node(newdata);
		else{
			while(next.getNext()!=null)
				next=next.getNext();
			next.setNext(new Node(newdata));
		}
	}
	int deQueue(){
		int data;
		if(!isEmpty()){
			data=m_FirstNode.getData();
			m_FirstNode=m_FirstNode.getNext();
			return data;
		}
		else
			return -1;
	}
}
public class UseQueue {// 
	public static void main(String[] args){
		Queue queue=new Queue();
		for(int i=1;i<5;i++){
			queue.enQueue(i);
			System.out.println(queue.visitAllNode());
		}
		System.out.println();
		while(!queue.isEmpty()){
			System.out.println(queue.deQueue()+" ;");
			System.out.println(" :"+queue.visitAllNode());
		}
	}
}

좋은 웹페이지 즐겨찾기