数据结构——使用双端链表实现队列(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,在队列头部进行两次移除操作,最后运行结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值