(九)鏈式隊列以及優先級隊列應用

鏈式隊列

鏈式存儲結構的隊列稱作鏈式隊列

鏈式隊列的存儲結構如下圖所示

鏈式隊列的實現

//結點類
public class Node {
  
	Object element; //數據域
	Node next;  //指針域
	
	//頭結點的構造方法
	public Node(Node nextval)
	{
		this.next = nextval;
	}
	
	//非頭結點的構造方法
	public Node(Object obj,Node nextval)
	{
	   this.element = obj;
	   this.next = nextval;
	}
	
	//獲得當前結點的後繼結點
	public Node getNext()
	{
		return this.next;
	}
	
	//獲得當前的數據域的值
	public Object getElement()
	{
		return this.element;
	}
	
	//設置當前結點的指針域
	public void setNext(Node nextval)
	{
		this.next = nextval;
	}
	
	//設置當前結點的數據域
	public void setElement(Object obj)
	{
		this.element = obj;
	}
	
	public String toString()
	{
	   return this.element.toString();	
	}
}

//隊列接口
public interface Queue {

   //入隊
   public void append(Object obj) throws Exception;
   //出隊
   public Object delete() throws Exception;
   //獲得隊頭元素
   public Object getFront() throws Exception;
   //判斷對列是否爲空
   public boolean isEmpty();
}

//鏈式隊列

public class LinkQueue implements Queue {
    
	Node front; //隊頭
	Node rear;  //隊尾
	int count; //計數器
	
	public LinkQueue()
	{
		init();
	}
	
    public void init()
    {
    	front=rear=null;
    	count=0;
    }
	
	@Override
	public void append(Object obj) throws Exception {
		// TODO Auto-generated method stub
		Node node = new Node(obj,null);
		
		//如果當前隊列不爲空。
		if(rear!=null)
		{
			rear.next = node; //隊尾結點指向新結點
		}
		
		rear = node; //設置隊尾結點爲新結點
		
		//說明要插入的結點是隊列的第一個結點
		if(front==null)
		{
		  front = node;	
		}
		count++;
	}

	@Override
	public Object delete() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty())
		{
			new Exception("隊列已空!");
		}
		Node node = front;
		front = front.next;
		count--;
		return node.getElement();
	}

	@Override
	public Object getFront() throws Exception {
		// TODO Auto-generated method stub
		if(!isEmpty())
		{
			return front.getElement();
		}
		else
		{
			return null;	
		}
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return count==0;
	}

}
鏈式隊列的應用

思路:設字符數組str中存放了要判斷的字符串。把字符數組中的字符逐個分別存入一個隊列和棧中,然後逐個出隊和出棧比較出隊的字符與出棧的字符是否相同,若全部相等則該字符串爲迴文。
//棧接口
public interface Stack {
    
	//入棧
	public void push(Object obj) throws Exception;
    //出棧
	public Object pop() throws Exception;
	//獲得棧頂元素
	public Object getTop() throws Exception;
	//判斷棧是否爲空
	public boolean isEmpty();
}

public class LinkStack implements Stack {
    
	Node head;  //棧頂指針
	int size;  //結點的個數
	
	public LinkStack()
	{
		head = null;
		size = 0;
	}
	
	@Override
	public Object getTop() throws Exception {
		// TODO Auto-generated method stub
		return head.getElement();
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return head==null;
	}

	@Override
	public Object pop() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty())
		{
			throw new Exception("棧爲空!");
		}
		Object obj = head.getElement();
		head = head.getNext();
		size--;
		return obj;
		
	}

	@Override
	public void push(Object obj) throws Exception {
		// TODO Auto-generated method stub
		head = new Node(obj,head);
		size++;
	}

}

public class Test {

	/**
	 * @param args
	 */
	
	public static boolean isHuiWen(String str)throws Exception
	{
		int n = str.length();
		LinkStack stack = new LinkStack();//創建堆棧
		LinkQueue queue = new LinkQueue();//創建隊列
		for(int i=0;i<n;i++)
		{
			stack.push(str.subSequence(i, i+1)); //把字符串每個字符壓進堆棧
			queue.append(str.subSequence(i, i+1));//把字符串每個字符壓入隊列
		}
		while(!queue.isEmpty()&&!stack.isEmpty())
		{
		   if(!queue.delete().equals(stack.pop()))
		   {
			  return false;   
		   }
		}
		
		return true;
	}
	
	public static void main(String[] args)throws Exception {
		String str1= "ABCDCBA" ;
		String str2 = "ABCDECAB" ;
		
		try
		{
		   if(Test.isHuiWen(str2))
		   {
			   System.out.println(str2+":是迴文!");
		   }
		   else
		   {
			   System.out.println(str2+":不是迴文!");
		   }
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

}
優先級隊列
優先級隊列是帶有優先級的隊列。
用順序存儲結構實現的優先級隊列稱作順序優先級隊列
用鏈式存儲結構存儲的優先級隊列稱作鏈式優先級隊列
順序優先級隊列
順序優先級隊列和順序循環隊列相比主要有兩點不同:

(1)對於順序優先級隊列來說,出隊列操作不是把隊頭數據元素出隊列,而是把隊列中優先級最高的數據元素出隊列。

(2)對於順序優先級隊列來說,數據元素由兩部分組成,一部分是原先意義上的數據元素,另一部分是優先級。通常設計優先級爲int類型的數值,並規定數值越小優先級越高。

設計順序優先級隊列(分爲兩個類)
數據元素類
優先級隊列類
//優先級隊列元素類
public class Element {

	private Object element; // 數據
	private int priority; // 優先級
    
	public Element(Object obj,int priority)
	{
		this.element = obj;
		this.priority = priority;
	}
	
	public Object getElement() {
		return element;
	}

	public void setElement(Object element) {
		this.element = element;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

}

//隊列接口
public interface Queue {

   //入隊
   public void append(Object obj) throws Exception;
   //出隊
   public Object delete() throws Exception;
   //獲得隊頭元素
   public Object getFront() throws Exception;
   //判斷對列是否爲空
   public boolean isEmpty();
}

//優先級隊列
public class PrioritySequenceQueue implements Queue {
    
	static final int defaultSize = 10; //默認隊列長度
	int front ; //隊頭
	int rear;  //隊尾
	int count;  //計數器
	int maxSize; //隊列最大長度
	Element[] queue; //隊列
	
	public PrioritySequenceQueue()
	{
		init(defaultSize);
	}
	
	public PrioritySequenceQueue(int size)
	{
		init(size);
	}
	
	public void init(int size)
	{
		maxSize = size;
		front= rear = 0;
		count=0;
		queue = new Element[size];
	}
	
	@Override
	public void append(Object obj) throws Exception {
		// TODO Auto-generated method stub
		//如果隊列已滿
		if(count>=maxSize)
		{
			throw new Exception("隊列已滿!");
		}
		queue[rear]=(Element)obj;
		rear++;
		count++;
	}

	@Override
	public Object delete() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty())
		{
			throw new Exception("隊列爲空!");
		}
		//默認第一個元素爲優先級最高的。
		Element min = queue[0];
		int minIndex = 0;
		for(int i=0;i<count;i++)
		{
			if(queue[i].getPriority()<min.getPriority())
			{
				min = queue[i];
				minIndex = i;
			}
		}
		
		//找的優先級別最高的元素後,把該元素後面的元素向前移動。
		for(int i=minIndex+1;i<count;i++)
		{
			queue[i-1]=queue[i]; //移動元素
		}
		rear--;
		count--;
		return min;
	}

	@Override
	public Object getFront() throws Exception {
		// TODO Auto-generated method stub
		if(isEmpty())
		{
			throw new Exception("隊列爲空!");
		}
		//默認第一個元素爲優先級最高的。
		Element min = queue[0];
		int minIndex = 0;
		for(int i=0;i<count;i++)
		{
			if(queue[i].getPriority()<min.getPriority())
			{
				min = queue[i];
				minIndex = i;
			}
		}
		return min;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return count==0;
	}

}

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
       PrioritySequenceQueue queue = new PrioritySequenceQueue();
       Element temp;
       
       //五個進程入隊
       queue.append(new Element(1,30));
       queue.append(new Element(2,20));
       queue.append(new Element(3,40));
       queue.append(new Element(4,20));
       queue.append(new Element(5,0));
       
       //按照優先級出隊。
       System.out.println("編號  優先級");
       while(!queue.isEmpty())
       {
    	   temp =(Element)queue.delete();
    	   System.out.println(temp.getElement()+" "+temp.getPriority());
       }
       
       
	}

}





發佈了31 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章