链接列表如何工作以及为什么我们在主类中提供节点类? [英] How does a linkedlist work and why do we provide a node class inside the main class?

查看:63
本文介绍了链接列表如何工作以及为什么我们在主类中提供节点类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是数据结构的新手,而且我自己没有人教我。我第一次开始倾向于链表,但我遇到了多个问题。所以我的问题如下,请详细回答它们,就像你对一个15岁的男孩一样。



1.为什么我们在其他类中提供Node类。为什么我们不能把它放在外面,如果我们可以如何放置它。

2.引用变量如何在Java中工作。它是一个用java构建的隐式指针。你可以用记忆图解释吗。



提前谢谢你。



我改进了我的问题和新版本如下:



干草谢谢你的回答我上传了我的代码:



我只是在知识的帮助下重新实现了它,我还没有完全实现它。

我的意思是链接列表类中的Node类。为什么我们不能将节点类放在外部作为单独的类而不是子类。如果我们能做到,我们应该如何实施它。请至少使用基本功能上传您的链接列表版本。

I new to data structures and on my own no one to teach me. I first started leaning Linked List but I came across multiple question. So my question are as follows please answer them in detail as you would to a 15 yr old boy.

1. Why do we provide the Node class inside the other class. Why cant we place it outside, if we can how should we place it.
2. How does the reference variable work in Java. Is it a implicit pointer built in java. can you please explain with a memory diagram.

Thank you in advance.

I have improved my question and the new version is below:

Hay thanks for your answer I have uploaded my code:

I re-implemented it with any help just my knowledge and I hasn't done it completely.
I mean the Node class inside the Linked list class. Why cant we just put the node class outside as separate class and a not a subclass. If we can do it how should we implement it. Please upload your version of Linked list at-least with basic functionality.

public class LinkedList {

	static class Node
	{
		int data;
		Node next;

		public Node(int data)
		{
			this.data = data;
			next = null;
		}

		public Node getNext()
		{
			return next;
		}

		public int getElement()
		{
			return data;
		}

		public void setNext(Node n)
		{
			next = n;
		}
	}

	Node head = null;
	Node tail = null;
	int size = 0;

	public boolean isEmpty(){ return size==0; }

	public int size(){ return size; }

	public int first()
	{
		if(isEmpty()){ return 0; }
		return head.getElement();
	}

	public int last()
	{
		if(isEmpty()) return 0;
		return tail.getElement();
	}

	public void addFirst(int data)
	{
		Node tmp = new Node(data);
		if(size == 0){head = tmp; tail = head; size++;}
	}

	public static void main(String args[])
	{


	}
}





我的尝试:



我刚刚记住了代码,但仍然理解了一些问题,只是为了一些额外的许可。我已经在java中实现了Linkedlist而没有泛型,并且具有ADD,DELETE,REMOVEFIRST,REMOVELAST,ADDFIRST,ADDLAST等功能。



What I have tried:

I have just memorized the code, But still understood the topics a little bit the question is just for some extra clearance. I have already implemented Linkedlist in java without generics and with features such as ADD, DELETE, REMOVEFIRST, REMOVELAST, ADDFIRST, ADDLAST.

推荐答案

这是一个技术论坛,而不是教程论坛。您需要自己进行研究:请参阅链接列表java示例 - Google搜索 [< a href =https://www.google.com/search?q=linked+list+java+exampletarget =_ blanktitle =New Window> ^ ]。
This is a technical forum, not a tutorial forum. You need to do your own research: see linked list java example - Google Search[^].


Quote:

1。为什么我们在其他类中提供Node类。为什么我们不能把它放在外面,如果我们可以如何放置它

1. Why do we provide the Node class inside the other class. Why cant we place it outside, if we can how should we place it

如果我们实际上看不到代码那么这个问题就没什么意义了。

作为猜测,节点未在外部定义,因为它是一个实现细节。



This question makes poor sense if we cannot actually see the code.
As a wild guess, Node is not defined outside because it is an implementation detail.

Quote:

引用变量如何在Java中工作。它是一个用java构建的隐式指针。你可以用记忆图解释吗

How does the reference variable work in Java. Is it a implicit pointer built in java. can you please explain with a memory diagram

简答:。您可以在此处找到更详细的信息:如何Java引用不同于C指针? - 程序员堆栈交换 [ ^ ]

Short answer: yes. You might find a more detailed one here: How is a Java reference different from a C pointer? - Programmers Stack Exchange[^]


我不知道您的链接列表的实现方式。实际上,任何开发人员都应该从头开始做这样的练习,否则没有人能说这个人对软件开发有什么了解。使用Java,由于垃圾收集器,这比使用低级语言要容易得多。



I have no idea which implementation of linked list you mean. Actually, any developer is supposed to do such exercise from scratch, otherwise no one can say that such person has a clue on what software development is. With Java, this is much times easier than with lower-level languages, because of the garbage collector.

  1. 我不知道为什么这么做我们提供班级里面的班级(我们都是那些人?:-)),但我真的会使用两个班级。一个类表示对下一个/上一个列表元素的引用,另一个类表示节点的信息部分,即其内容。我会使用 Java泛型来实现节点。



    这样,它可以隔离节点的两个方面:特定于节点的完整类型的功能,以及与特定部分无关的通用部分;这个通用部分将实现纯列表功能:引用其他节点和列表操作。



    按照他们的方式,它不必是类中的类。不要将内部类与一个类的引用成员混合,而是另一个类的类型。您可以单独声明两个类,并在另一个类中使用一个类。这就是全部。
  2. 绘制图表(没有图形)太麻烦了。请在你脑海中绘制图表。更好的是,在关于该主题的数十本书中的任何一本书中阅读有关Java引用(或更一般地,托管引用(有时称为托管指针))。基本上,Java引用对象实际上是两个对象:对象本身和引用它的对象应用程序开发人员只能通过引用直接操作。对象本身在堆上,当你使用它的任何实例成员时通过引用访问。当你为另一个引用分配一个引用时,你获得两个引用变量,但实际引用对象只有一个。当你丢失所有引用时,引用的对象被认为是无法访问并最终将被销毁;它的内存将被回收。



    所以,你的图表只有两个框:引用对象及其引用。是不是很容易?:-)



    现在,引用就像指针一样,但它们并不是指针。他们是管理的。比如,应用程序开发人员无法保证对象保留其物理内存位置。允许内存管理重定位它,但它对应用程序开发人员透明地完成;整个引用集的完整性和引用的所有对象都是原子地保留。



    我对垃圾收集的描述大大简化了。我没有提供一些必要的细节,因为这不是你问题的一部分;所以你现在只需要基本的想法。
  1. I don't know why do "we" provide "the class inside class" (we? who are all those people? :-)), but I would really use two classes. One class would represent the references to next/previous list element, and another one would represent the "informational" part of the node, its content. I would use Java generics to implement the node.

    This way, it could isolate two aspects of the node: the functionality specific to the complete type of the node, and the universal part agnostic to the specific part; this universal part would implement pure list functionality: referenced to other nodes and list operations.

    By they way, it does not have to be "class in class". Don't mix inner classes with reference member of one class, of the type of another class. You can declare two classes separately and use one class in another one. That's all.
  2. It's too bothersome to draw a diagram (without graphics). Please draw the diagram in your head. Better yet, read about Java reference (or more generally, managed referenced (sometimes called managed pointers) in any of the tens of books on the topic. Basically, a Java reference object is really two objects: the object itself and the object referencing it. The application developer directly operates only by references. The object itself is on heap and is accessed through a reference when you use any of its instance members. When you assign one reference to another one, you obtain two reference variables, but the actual referenced object is only one. When you lose all referenced, the referenced object is considered unreachable and will be eventually destroyed; its memory will be reclaimed.

    So, you "diagram" is just two boxes: referenced object and the reference to it. Isn't it easy enough? :-)

    Now, references are like pointers, but they are not exactly pointers. They are managed. Say, the application developer is not guaranteed that the object keeps its physical memory location. The memory management is allowed to relocate it, but it's done transparently to the application developer; the integrity of the whole set of references and referenced all objects is atomically preserved.

    My description of garbage collection is greatly simplified. I did not provide some essential detail just because this is not a part of your question; so you only need the basic idea at this point.





-SA


这篇关于链接列表如何工作以及为什么我们在主类中提供节点类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆