java学习day13

117 阅读2分钟

1.成员内部类

在c语言中,对链表的数据结构是用结构体来表示,在java中使用内部类来表示链表结构体,成员内部类是定义在另一个类的内部的类,可以访问外部类的成员和方法,包括私有成员和方法。如文章中的Node类可以访问外部类中所有的成员变量和成员方法。

2.链表的插入删除

插入 删除 在这里插入图片描述

3.代码

在c中,对链表结构的定义

typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

在java中

package datastructure.list;

import sun.applet.Main;
import java.util.UUID;
public class LinkedList {
    class Node{
        int data;
        Node next;

        /**
         * The constructor
         * @param paraValue The data.
         */
        public Node(int paraValue){
            data = paraValue;
            next = null;
        }
    }

    Node header;

    /**
     * Construct an empty linked list.
     */
    public LinkedList(){
        header = new Node(0);
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    public String toString(){
        String resultString = "";

        if (header.next == null){
            return "empty";
        }

        Node tempNode = header.next;
        while (tempNode != null){
            resultString += tempNode.data + ",";
            tempNode  = tempNode.next;
        }

        return resultString;
    }

    /**
     * Reset to empty. Free the space through garbage collection.
     */
    public void reset() {
        header.next = null;
    }

    /**
     * Locate the given value. If it appears in multiple positions, simply return the first one.
     * @param paraValue The given value.
     * @return The position. -1 for not found.
     */
    public int locate(int paraValue){
        int tempPosition = -1;

        Node tempNode = header.next;
        int tempCurrentPosition = 0;
        while (tempNode != null){
            if (tempNode.data == paraValue){
                tempPosition = tempCurrentPosition;
                break;
            }

            tempNode = tempNode.next;
            tempCurrentPosition++;
        }

        return  tempCurrentPosition;
    }

    /**
     * Insert a value to a position. If the list is already full, do nothing.
     * @param paraPosition The given position.
     * @param paraValue The given value.
     * @return Success or not.
     */
    public boolean insert(int paraPosition, int paraValue){
        Node tempNode = header;
        Node tempNewNode;

        //find a preNode
        for (int i = 0; i<paraPosition; i++){
            if (tempNode.next == null){
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }
            tempNode = tempNode.next;
        }

        tempNewNode = new Node(paraValue);

        tempNewNode.next = tempNode.next;
        tempNode.next = tempNewNode;

        return  true;
    }


    /**
     * Delete a value at a position.
     * @param paraPosition The given position.
     * @return Success or not
     */
    public boolean delete(int paraPosition){
        if (header.next == null){
            System.out.println("Cannot delete element from an empty list.");
            return false;
        }

        Node tempNode = header;

        for (int i = 0; i < paraPosition; i++){
            if (tempNode.next == null){
                System.out.println("The position " + paraPosition + " is illegal.");
                return false;
            }

            tempNode = tempNode.next;
        }

        tempNode.next = tempNode.next.next;
        return true;
    }

    public static void main(String args[]) {
        LinkedList tempFirstList = new LinkedList();
        System.out.println("Initialized, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.insert(0, i);
        }
        System.out.println("Inserted, the list is: " + tempFirstList.toString());

        tempFirstList.insert(6, 9);

        tempFirstList.delete(4);

        tempFirstList.delete(2);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        tempFirstList.delete(0);
        System.out.println("Deleted, the list is: " + tempFirstList.toString());

        for (int i = 0; i < 5; i++) {
            tempFirstList.delete(0);
            System.out.println("Looped delete, the list is: " + tempFirstList.toString());
        }
    }
}


在这里插入图片描述