队列单链表.doc_第1页
队列单链表.doc_第2页
队列单链表.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

队列的单链表实现如下:结点类:public class Node /结点类 public int element; public Node link; public Node() /无参构造函数 link=null; public Node(int newelement,Node newlink) /带参构造函数 element=newelement; link=newlink;public void setElement(int newelement) /设置当前结点的数据元素 element=newelement;public int getElement() /取当前结点的数据元素return element;public void setLink(Node newlink) /设置下一个结点的对象引用link=newlink;public Node getlink() /取下一个结点的对象引用 return link;队列类:public class SeqList /队列类 public Node front; public Node rear; public int size; public SeqList() /无参构造函数 front=rear=new Node(); public void append(int number) /入队列操作 rear.setLink(new Node(number,rear.link); rear=rear.link; size+; public int delete()throws Exception /出队列操作if (size=0) throw new Exception(队列已空!); int temp; temp=front.link.getElement(); front.setLink(front.link.link); size-; return temp; public int getFront()throws Exception /取队首元素if (size=0) throw new Exception(队列已空!); return front.link.getElement();public boolean notEmpty() /判断非空否if(size=0)return false; return true;public void show() /显示当前队列中的数据元素Node CuFront=new Node(); CuFront=front;for(int i=0;isize;i+)System.out.print(CuFront.link.getElement()+ ); CuFront.setLink(CuFront.link);CuFront=CuFront.link; 测试程序:import java.io.*;public class TestSeqListpublic static void main(String args)throws IOExceptionBufferedReader Inputnumber=new BufferedReader(new InputStreamReader(System.in); String numberstring; System.out.println(请输入队列的长度:); numberstring=Inputnumber.readLine(); int temp=Integer.parseInt(numberstring); SeqList list=new SeqList(); System.out.println(请输入入队的数据:); tryfor(int j=0;jtemp;j+) numberstring=Inputnumber.readLine(); int number=Integer.parseInt(numberstring); list.append(number); System.out.println(队列中的数据为:); list.show(); System.out.println(); System.out.println(出队列两个数据为:); System.out.println(list.delete()+ +list.delete()+n); System.out.println(此时队列中的数据为:); list.show(); System.out.println(); System.out.println(取队首数据为:+n+list.getFront();

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论