数据结构——使用双端链表实现队列(java实现)
2015-11-27 13:13
791 查看
队列是这样一种数据结构:在队尾(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()
c0b4
{
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,在队列头部进行两次移除操作,最后运行结果如下:
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()
c0b4
{
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,在队列头部进行两次移除操作,最后运行结果如下:
相关文章推荐
- 数据结构——用(单端)链表实现栈(java实现)
- 数据结构实验之图论八:欧拉回路 无向图的欧拉回路判断
- C#数据结构之单链表(LinkList)实例详解
- C#数据结构之顺序表(SeqList)实例详解
- 数据结构实验之图论四:迷宫探索
- 数据结构实验之图论四:迷宫探索 【基于邻接表的广度优先搜索】
- 数据结构实验之图论七:驴友计划
- Redis数据结构详解之Zset(五)
- 第13周SHH数据结构-【项目4-Floyd算法的验证 】
- 第十二周--数据结构--判断图G中是否存在边<i,j>
- 第十二周--数据结构--计算图G中出度为0的顶点数
- 第13周SHH数据结构-【项目3-Dijkstra算法的验证 】
- 第13周SHH数据结构-【项目2-Kruskal算法的验证 】
- 第十二周--数据结构--求出度最大的一个顶点并输出该顶点编号
- 第13周SHH数据结构-【项目1-Prim算法的验证 】
- 第十二周--数据结构--输出出图G中每个顶点的出度
- redis数据结构-字典
- 一天一个数据结构之ArrayList
- 【数据结构】图的常用存储结构
- 数据结构 — 图 之 关键路径、关键活动 (文字表述)