数据结构——使用双端链表实现队列(java实现)

     队列是这样一种数据结构:在队尾(rear)插入数据项,在队首(front)移除数据项,队列的进出顺序是先进入的先被移除(先进先出,FIFO);

     Robert Lafore的书对队列的使用有如下描述:“它可以用于模拟真实世界的环境,例如模拟人们在银行里排队等待,飞机等待起飞,或者因特网上数据包等待传送。在计算机(或网络)操作系统里,有各种队列在安静地工作着。打印作业在打印队列中等待打印。当在键盘上敲击时,也有一个存储键入内容的队列。同样,如果使用文字处理程序敲击一个键,而计算机又暂时要做其他的事,敲击的内容不会丢失,它会排在队列中等待,知道文字处理程序有时间来读取它。利用队列保证了键入内容在处理时其顺序不会改变。

实现代码如下:

/*
 *Imagine a queue in front of you,your left side is the front of the queue,your right side is the rear of queue;
 *this seems more logical and close to real life
 *
*/
class Link
{
	public long dData;
	public Link next;
	public Link(long dData)
	{
		this.dData = dData;
	}
	public void displayLink()
	{
		System.out.print("{" + this.dData + "}");
	}
}

class FirstLastList
{
	Link first;
	Link last;

	public FirstLastList()
	{
		this.first = null;  //when create an object of LinkList,make sure it is empty!
		this.last = null;
	}

	public boolean isEmpty()
	{
		return first == null;
	}

	public void insertLast(long key)	//this method will be used when I create insert() method 
	{									//in Queue(not the class Queue,I just mean a Queue)
		Link newLink = new Link(key);
		if(this.isEmpty())				//if list is empty
		{
			first = newLink;			//draw a picture can help me understand it !
			last = newLink;
			newLink.next = null;
		}
		else
		{
			last.next = newLink;
			last = newLink;
			newLink.next = null;
		}
	}

	public long deleteFirst()		//this method will be used when I create remove() method in Queue(not the class Queue,I just mean a Queue)
	{
		Link current = null;
		if(this.isEmpty())
		{
			System.out.println("Your stack is empty");
			return -1;
		}
		else if(first==last)
			{
				current = first;
			    first = null;
			    last = null;
			    return current.dData;
			}
			else
			{
				current = first;
				first = first.next;
				return current.dData;
			}
			
	}

	public void displayList()
	{
		Link current = first;
		System.out.print("Queue (front-->rear): ");
		if(this.isEmpty())
		{
			System.out.println("Your list is empty, nothing to show!");
		}
		else
		{
			while(current!=null)
			{
				current.displayLink();
				current = current.next;
			}
			System.out.println("");
		}
	}
}

class LinkQueue
{
	FirstLastList list = new FirstLastList();	//two-ended list
	public void insert(long key)
	{
		list.insertLast(key);
	}
	public long remove()
	{
		return list.deleteFirst();
	}
	public void showQueue()
	{
		list.displayList();
	}
}

class LinkQueueApp
{
	public static void main(String[] args)
	{
		LinkQueue theQueue = new LinkQueue();
		theQueue.insert(12);	//insert four elements
		theQueue.insert(13);
		theQueue.insert(14);
		theQueue.insert(15);

		theQueue.showQueue();	//look at what is in the queue

		theQueue.remove();		//remove two elements ,from right side
		theQueue.remove();
		theQueue.showQueue();	//look at what is in the queue now!
	}
}

代码的思路:首先创建一个新队列,接着在队列中一次插入12、13、14、15,在队列头部进行两次移除操作,最后运行结果如下:


/* * 基于双向链表实现双端队列结构 */ package dsa; public class Deque_DLNode implements Deque { protected DLNode header;//指向头节点(哨兵) protected DLNode trailer;//指向尾节点(哨兵) protected int size;//队列中元素的数目 //构造函数 public Deque_DLNode() { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); trailer.setPrev(header); size = 0; } //返回队列中元素数目 public int getSize() { return size; } //判断队列是否为空 public boolean isEmpty() { return (0 == size) ? true : false; } //取首元素(但不删除) public Object first() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return header.getNext().getElem(); } //取末元素(但不删除) public Object last() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); return trailer.getPrev().getElem(); } //在队列前端插入新节点 public void insertFirst(Object obj) { DLNode second = header.getNext(); DLNode first = new DLNode(obj, header, second); second.setPrev(first); header.setNext(first); size++; } //在队列后端插入新节点 public void insertLast(Object obj) { DLNode second = trailer.getPrev(); DLNode first = new DLNode(obj, second, trailer); second.setNext(first); trailer.setPrev(first); size++; } //删除首节点 public Object removeFirst() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = header.getNext(); DLNode second = first.getNext(); Object obj = first.getElem(); header.setNext(second); second.setPrev(header); size--; return(obj); } //删除末节点 public Object removeLast() throws ExceptionQueueEmpty { if (isEmpty()) throw new ExceptionQueueEmpty("意外:双端队列为空"); DLNode first = trailer.getPrev(); DLNode second = first.getPrev(); Object obj = first.getElem(); trailer.setPrev(second); second.setNext(trailer); size--; return(obj); } //遍历 public void Traversal() { DLNode p = header.getNext(); while (p != trailer) { System.out.print(p.getElem()+" "); p = p.getNex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值