Java implements the queue example using arrays and linked lists
- 2020-04-01 02:54:16
- OfStack
(1) queue implemented by array:
//Define an interface of your own & NBSP;
public interface NetJavaList {
public void add(Student t); //Methods that the class that inherits the interface must implement & NBSP;
public Student get(int index);//Queue in, queue out, queue size & NBSP;
public int size();
}
Define a student class
class Student {
private String name ; //Private property name, credit & NBSP;
private int score ;
public Student(String name , int score){
this.name = name ;
this.score = score ;
}
public void printInfo(){
System.out.println(" The name "+name + " credits "+score ) ;
}
}
Implement a custom interface
public class STList implements NetJavaList{
private Student[] str = new Student[0] ;
//Adds an element of the queue & NBSP;
public void add(Student t) {
Student[] src = new Student[str.length+1];
for(int i=0;i<str.length;i++){
src[i]=str[i] ;
}
src[str.length]=t ;
str = src ;
}
//Gets an element in the queue & NBSP;
public Student get(int index) {
Student t = str[index];
return t;
}
//Returns the length of the queue & NBSP;
public int size() {
return str.length;
}
}
Write a main function class to implement the next queue
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) ;
}
//All the elements in the output queue & NBSP;
public static void printList(STList t){
for(int i=0;i<t.size();i++){
Student f =t.get(i);
f.printInfo();
}
}
}
(2) queue implemented by linked list
Define a node class;
public class LinkNode {
private Object obj ; //The data object within the node & NBSP;
private LinkNode next ; //Reference to the next node & NBSP;
//The node's data object is passed when the node object is created.
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 ;
}
}
Then write a queue implementation method class
public class LinkList {
public static LinkNode root ;//The first node & NBSP;
public LinkNode last = null ;//The last node & NBSP;
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){
//Create a new node & NBSP;
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()<index || index <0){
throw new RuntimeException(" Subscript out of bounds: "+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 ;
}
}