java 数组 模拟链表_java使用数组和链表实现队列示例

(1)用数组实现的队列:

//先自己定义一个接口

public interface NetJavaList {

public void add(Student t);    //继承该接口的类必须实现的方法

public Student get(int index);//队列的加入,取出,队列的大小

public int size();

}

定义一个学生类

class Student {

private String name ;   //私有属性 名字,学分

private int score ;

public Student(String name,int score){

this.name = name ;

this.score = score ;

}

public void printInfo(){

System.out.println("姓名"+name + "学分"+score ) ;

}

}

实现自定义接口

public class STList implements NetJavaList{

private Student[] str = new Student[0] ;

//增加队列的元素

public void add(Student t) {

Student[] src = new Student[str.length+1];

for(int i=0;i

src[i]=str[i] ;

}

src[str.length]=t ;

str = src ;

}

//得到队列中的某个元素

public Student get(int index) {

Student t = str[index];

return t;

}

//返回队列的长度

public int size() {

return str.length;

}

}

写个主函数类实现下队列

public class Manager {

public static void main(String[] args) {

STList sil = new STList() ;

for(int i=0;i<5;i++){

Student st = new Student("name"+i,i*10);

sil.add(st);

}

printList(sil) ;

}

//输出队列中的所有元素

public static void printList(STList t){

for(int i=0;i

Student f =t.get(i);

f.printInfo();

}

}

}

(2)链表实现的队列

先定义一个节点类;

public class LinkNode {

private Object obj ; //节点内的数据对象

private LinkNode next ; //对下一个节点的引用

//在创建节点对象的时候就传入节点的数据对象

public LinkNode(Object obj){

this.obj = obj ;

}

public Object getObj(){

return obj ;

}

public void setObj(Object obj){

this.obj = obj ;

}

public LinkNode getNext(){

return next ;

}

public void setNext(LinkNode next){

this.next =next ;

}

}

然后写个队列的实现方法类

public class LinkList {

public static LinkNode root ;//第一个节点

public LinkNode last = null ;//最后的一个节点

public static void main(String ara[]){

LinkList df = new LinkList() ;

df.add(1);

df.add(2);

df.add(3);

df.printLinkList(root);

df.move(root,2) ;

df.move(root,2) ;

df.printLinkList(root);

}

/*

* 插入节点

*/

public void add(Object obj){

//创建一个新的节点

LinkNode t = new LinkNode(obj);

if(root ==null){

root = t ;

last = root ;

}else{

last.setNext(t);

last = t ;

}

}

/*

* 输出操作

*/

public void printLinkList(LinkNode root){

if(null != root){

Object data = root.getObj();

System.out.println(data);

LinkNode temp = root.getNext();

printLinkList(temp) ;

}

}

/*

* 删除操作

*/

public LinkNode move(LinkNode root,int index){

if(this.getLength()

throw new RuntimeException("下标越界:"+index +

",size:" +this.getLength()) ;

}else{

int count = 1 ;LinkNode sd = root ;

while(count!=index-1){

sd = sd.getNext();

}

sd.setNext(sd.getNext().getNext());

return root ;

}}

/*

* 得到链表的长度

*/

public int  getLength(){

int count = 0 ;

if(root==null){

return count ;

}

LinkNode node =root.getNext();

while(null != node){

count ++ ;

node=node.getNext();

}

//System.out.println((count+1));

return count+1 ;

}

}

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值