[英]How to create a linked list in java without importing linked lists
我是一名大學計算機科學專業的學生,我在這個項目中遇到了麻煩,我想在不導入鏈表的情況下使用節點創建一個鏈表,以及對列表執行一些方法。 在編碼方面我是初學者,所以假設我什么都不知道,因為可能就是這種情況哈哈。
import java.io.*;
import java.lang.*;
public class List {
public int listCount = 0;
public char[] linkedList;
public List() throws FileNotFoundException {
}
public List(char[] array) throws FileNotFoundException {
if (array.length == 1) {
Node head = new Node(array[0]);
} else if (array.length > 1) {
Node head = new Node(array[0]);
Node traverse = head;
for (int i = 1; i < array.length; i++) {
while (traverse.nextNode != null) {
traverse = traverse.nextNode;
}
traverse.nextNode = new Node(array[i]);
listCount++;
}
}
}
public List(String w) throws FileNotFoundException {
char[] array2 = new char[w.length()];
for (int i = 0; i < w.length(); i++) {
array2[i] = w.charAt(i);
}
List str = new List(array2);
}
/* Find the character at a index
@param int index
return the character at the chosen index
*/
public char charAt(int index) throws IndexOutOfBoundsException {
char results = linkedList[0];
if (index < linkedList.length && index >= 0) {
results = linkedList[index];
}
return results;
}
public String concat(int index1, int index2) {
return null;
}
/* Determine if the list is empty
return whether the given conditions are true or false
*/
public boolean isEmpty() {
for (int i = 0; i < linkedList.length; i++) {
if (!linkedList.equals(null)) {
System.out.println("This list is not empty");
return false;
}
}
System.out.println("List is empty");
return true;
}
/* Determine the size of the list
return the size of the list
*/
public int size() {
return listCount;
}
/* Create a new String between 2 index's including the start and end index
@param beginIndex is the starting point of the new String
@endIndex is the ending point of new String
return the new String
*/
public String subString(int beginIndex, int endIndex) {
return null;
}
public void insert(Object x) throws IndexOutOfBoundsException {
if (listCount > 100 || listCount < 0) {
throw new IndexOutOfBoundsException("Bag is too large");
} else {
this.linkedList[listCount] = (char) x;
listCount++;
}
}
}
我提前感謝任何幫助或指示。 我們正在使用單獨的節點、幫助程序和驅動程序 class 以及一個 .txt 文件分配到我的列表中。 我也堅持使用 concat 和 substring 方法,但我想確保我首先得到正確的框架。 再次感謝你。
如果我正確理解您的問題,您是在詢問如何在不導入特定類型的情況下訪問它。
當使用其簡單名稱時,需要導入來識別引用的類型。 要引用一個類型而不在導入中聲明它,您需要使用它的完全限定名稱。 例如
java.util.List<String> someList = new java.util.ArrayList<>();
無需導入 List 和 ArrayList 即可工作,因為通過聲明 package class 很明顯哪個 ZA2F2ED4F8EBC2CBB61DZC 被引用。
稍后我會嘗試編寫代碼,但這是我發現的一本書可能對您有所幫助。
https://cin.ufpe.br/~grm/downloads/Data_Structures_and_Algorithms_in_Java.pdf我從 Pearson 公司買了一本關於 DATA STRUCTURE 的書,它確實是一本好書,但我不記得了我匆忙做了:
public class List {
private Node head = null;
private Node foot = null;
private Node newNode = null;
private Node auxNode = null;
public List() {
this.head = new Node();
this.foot = new Node();
}
public class Node {
private int adress;
private Node nextNode;
}
public void add(int value) {
this.newNode = new Node();
newNode.adress = value;
if (head == null) {
// Head of the list receive the values of the NEW NODE, so the head of the list
// is not null enymore
head = newNode;
head.nextNode = null;
} else {
// In this case Head is not null
/*The auxiliary node will receive the head and the new Node will become the new Head from the list*/
auxNode = new Node();
auxNode = head;
/*
while(auxNode.nextNode != null ) {
}
auxNode = head;
//head of the list is empty, so we can add the new node
head = newNode;//Here the new node is empty because was transfered to the head
head.nextNode = auxNode; //The head of the list receive the old node that used to be the head
if (head.nextNode == null) {
head.nextNode = newNode;
} else if (head.nextNode != null) {
}*/
}
}
}
```
I hope this help you to get some lead
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.