Java构造结构体类型

结构体类型

结构体数组

public static void main(String[] args) {
    Animal[] animals = new Animal[3];
    for(int i=0;i<3;i++){
        //注意这里还要为数组中的每个元素定义一个对象
        animals[i] = new Animal();
    }
    animals[1].age=1;
    animals[2].name=2;
    animals[0].height=3;
    System.out.println("object1:"+animals[0].height+"object2:"+animals[1].age+"object3:"+animals[2].name);
}
static class Animal{
    int name;
    int age;
    int height;
    //这里有必要可以加入get、set方法
}

链表的定义和初始化

public static void main(String[] args) {
    MyLinkedList myLinkedList = new MyLinkedList();
    myLinkedList.init();
    myLinkedList.print(myLinkedList.head);
    System.out.println("这个单链表的长度为:"+myLinkedList.get_Length());
}
//ode类单独拎出来作为一个类
public static class Node<T>{
    public T data;
    public Node next;
    public Node(T data){
        this.data=data;
    }
}
public static class MyLinkedList{
    public Node head;
    public Node current;

    //增加节点
    public void add(int data){
        //如果头结点为空
        if(head==null){
            head = new Node(data);
            current = head;
        }else {
            current.next = new Node(data);
            current=current.next;
        }
    }
    //打印链表
    public void print(Node node){
        //如果当前节点不为空
        if(node==null) return;
        current=node;
        while (current!=null){
            System.out.print(current.data+"  ");
            current = current.next;
        }
    }
    //初始化链表并返回表头
    public Node init(){
        for(int i=0;i<10;i++){
            this.add(i);
        }
        return head;
    }
    //求链表长度
    public int get_Length(){
        if(head==null) return -1;
        int length = 0;
        current = head;
        while (current!=null){
            length++;
            current=current.next;
        }
        return length;
    }
}

含有对象的结构体单链表

public static void main(String[] args) {
    LinkedList list = new LinkedList();
    list.init();
    list.print(list.head);
    System.out.println();
    Animal animal = new Animal(1,1,1);
    list.delete(animal);
    list.print(list.head);
}
//定义一个结构体
public static class Animal{
    int name;
    int age;
    int height;
    public Animal(int name,int age,int height){
        this.name=name;
        this.age=age;
        this.height=height;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name=" + name +
                ", age=" + age +
                ", height=" + height +
                '}';
    }

    public int getName() {
        return name;
    }

    public void setName(int name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
public static class Node{
    Animal data;
    Node next;

    public Node(Animal data){
        this.data=data;
    }
    public Node(){

    }
}
public static class LinkedList{
    public Node head;
    public Node current;

    public void add(Animal a){
        if(head==null){
            head = new Node(a);
            current=head;
        }
        else {
            current.next=new Node(a);
            current=current.next;
        }
    }
    //初始化链表
    public void init(){
        for(int i=0;i<10;i++){
            Animal a1 = new Animal(i,i,i);
            this.add(a1);
        }
    }
    //打印
    public void print(Node node){
        if(node==null){
            return;
        }
        current=head;
        while (current!=null){
            System.out.print(current.data.toString()+"     ");
            current=current.next;
        }
    }
    //删除节点
    public void delete(Animal animal){
        if(head==null) return;
        current = head;
        Node preNode = new Node();
        while (current.next!=null){
            preNode = current;
            current=current.next;
            if(animal.getName()==current.data.getName()){
                preNode.next=current.next;
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("未找到该节点");
        return;
    }
}

你可能感兴趣的:(Java,java)