如何在Java中创建链接列表数据结构? [英] How do I create a Linked List Data Structure in Java?
问题描述
Java中熟悉的开发人员的明显解决方案是使用 java.util 中已经提供的 LinkedList 类。但是,您希望由于某些原因自己实现。这是一个列表列表开头插入新链接的链接列表的快速示例,从列表的开头删除并循环遍历列表以打印其中包含的链接。此实施的增强功能包括使其成为双链表,从中间添加方法插入和删除或者结束,还可以添加 get 和排序方法。
注意:在示例中,Link对象实际上并不包含另一个Link对象 - nextLink 实际上只是引用另一个链接。
class Link {
public int data1;
public double data2;
public Link nextLink;
//链接构造函数
public Link(int d1,double d2){
data1 = d1;
data2 = d2;
}
//打印链接数据
public void printLink(){
System.out.print({+ data1 +,+ data2 + });
}
}
class LinkList {
private Link first;
// LinkList构造函数
public LinkList(){
first = null;
}
//如果列表为空,则返回true
public boolean isEmpty(){
return first == null;
}
//在列表的第一列插入一个新的链接
public void insert(int d1,double d2){
Link link = new Link d1,d2);
link.nextLink = first;
first = link;
}
//删除列表中第一个链接
public Link delete(){
Link temp = first;
first = first.nextLink;
return temp;
}
//打印列表数据
public void printList(){
Link currentLink = first;
System.out.print(List:);
while(currentLink!= null){
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println();
}
}
class LinkListTest {
public static void main(String [] args){
LinkList list = new LinkList();
list.insert(1,1.01);
list.insert(2,2.02);
list.insert(3,3.03);
list.insert(4,4.04);
list.insert(5,5.05);
list.printList();
while(!list.isEmpty()){
链接deletedLink = list.delete();
System.out.print(deleted:);
deletedLink.printLink();
System.out.println();
}
list.printList();
}
}
What's the best way to make a linked list in Java?
The obvious solution to developers familiar to Java is to use the LinkedList class already provided in java.util. Say, however, you wanted to make your own implementation for some reason. Here is a quick example of a linked list that inserts a new link at the beginning of the list, deletes from the beginning of the list and loops through the list to print the links contained in it. Enhancements to this implementation include making it a double-linked list, adding methods to insert and delete from the middle or end, and by adding get and sort methods as well.
Note: In the example, the Link object doesn't actually contain another Link object - nextLink is actually only a reference to another link.
class Link {
public int data1;
public double data2;
public Link nextLink;
//Link constructor
public Link(int d1, double d2) {
data1 = d1;
data2 = d2;
}
//Print Link data
public void printLink() {
System.out.print("{" + data1 + ", " + data2 + "} ");
}
}
class LinkList {
private Link first;
//LinkList constructor
public LinkList() {
first = null;
}
//Returns true if list is empty
public boolean isEmpty() {
return first == null;
}
//Inserts a new Link at the first of the list
public void insert(int d1, double d2) {
Link link = new Link(d1, d2);
link.nextLink = first;
first = link;
}
//Deletes the link at the first of the list
public Link delete() {
Link temp = first;
first = first.nextLink;
return temp;
}
//Prints list data
public void printList() {
Link currentLink = first;
System.out.print("List: ");
while(currentLink != null) {
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
}
class LinkListTest {
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(1, 1.01);
list.insert(2, 2.02);
list.insert(3, 3.03);
list.insert(4, 4.04);
list.insert(5, 5.05);
list.printList();
while(!list.isEmpty()) {
Link deletedLink = list.delete();
System.out.print("deleted: ");
deletedLink.printLink();
System.out.println("");
}
list.printList();
}
}
这篇关于如何在Java中创建链接列表数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!