0
点赞
收藏
分享

微信扫一扫

日撸代码300行:第13天

北冥有一鲲 2022-01-20 阅读 33
java链表

今天的代码中出现了class定义的类,于是网上查了一下public修饰的类于没有public修饰的类的区别。链接为:https://blog.csdn.net/jingzi123456789/article/details/71515728,学习之后总结来说如下:
1.一个 .java文件中,只能有一个public class声明的类,但是可以有多个class声明的类。即每个编译单元都有单一的公共接口,用public类实现。此时,mian()就必须要包含在public类中。
2.使用了public class定义的类,类名必须与文件名完全一致,否则编译时会出错。使用class定义的类,文件名不需要与类名相同,但是编译之后生成的文件与类名相同。如果使用class定义多个类,就会形成多个*.class文件。
3.没有public修饰的类,该类就拥有了包访问权限,即该类只可以用于该包之中。

package datastructure.list;

import javax.swing.text.Position;

/**
 * 
 * Linked List
 * 
 * @author WX873
 *
 */
public class LinkedList {
	/**
	 * An inner class
	 */
	class Node{
		
		/**
		 * The data
		 */
		int data;
		
		/**
		 * The reference to the next node.
		 */
		Node next;
		
		/**
		 * The constructor
		 * 
		 */
		public Node(int paraValue) {
			// TODO Auto-generated constructor stub
			data = paraValue;
			next = null;    //每一个节点下一个都是为空,调用的时候会指向下一个节点
		}//of class Node
		 
	}//of class Node
	
	/**
	 * The header node.The data is never used.
	 */
	Node header;
	
	/**
	 * Construct an empty linked list.
	 */
	public LinkedList() {
		// TODO Auto-generated constructor stub
		header = new Node(0);
		//header.next = null;
	}//of the first constructor
	
	/**
	 * *********************************************
	 * Overrides the method claimed in Object, the superclass of any class.
	 * *********************************************
	 */
	public String toString() {
		String resultString = "";
		
		if (header.next == null) {
			return "empty";
		}//of if
		
		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ",";
			tempNode = tempNode.next;
		}//of while
		return resultString; 
	}//of toString
	
	/**
	 * ***********************************************
	 * Reset to empty. Free the space through garbage collection.
	 * ***********************************************
	 */
	public void reset() {
		header.next = null;
	}//of reset
	
	/**
	 * *********************************************************
	 * 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;
			}//of if
			tempCurrentPosition ++;
			tempNode = tempNode.next;
		}//of while
		return tempPosition;
	}//of locate
	
	/**
	 * *************************************************
	 * 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;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			}//of if
			tempNode = tempNode.next;
		}//of for i
		
		//Construct a new node.
		tempNewNode = new Node(paraValue);
		
		//Now link them.
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;
		return true;
	}//of insert
	
	/**
	 * ************************************************
	 * 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;
		}//of if
		
		Node tempNode = header;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + "is illegal.");
				return false;
			}//of if
			
			tempNode = tempNode.next;
		}//of for i
		tempNode.next =tempNode.next.next;//跳出for循环最后一步tempNode不再自增1.
		return true;
	}//of delete
	
	/**
	 * *********************************************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 * 
	 * *********************************************
	 */
	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);
		}//of for i
		System.out.println("Inserted, the list is: " + tempFirstList.toString());
		
		tempFirstList.insert(5, 9);
		System.out.println("Inserted, the list is: " + tempFirstList.toString());
		
		tempFirstList.delete(4);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());
	}//of main

}//of class LinkedList

写代码的时候有两个地方一直困惑,想通之后在相应的位置写了汉语注释。今天的学习通过手画链表才理解。自己手画的表如下图:

在这里插入图片描述

明天补充一个图,顺便巩固复习一下。

举报

相关推荐

0 条评论