Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: Let me just talk about Java: Although there is no pointer, every variable is a reference if it is not a basic data type (such as int float ).
Reference is similar to pointer, but pointer operations are not allowed, such as p + 1 pointing to the next element.
Implementation of linked lists in various languages:
Singly-linked list/Element definition
Singly-linked list/Element insertion
When a basic data structure is implemented, the only function of the pointer is to point, which does not involve the pointer operation (this is not called the const pointer ), therefore, Java/Python references are sufficient. Even if B-tree or something is implemented, configuring an array is enough.
With the handwriting of a Python linked list node, there are examples below in Java, which are all the same.
Yes
class Node(object): def __init__(self, value, nextnode=None): self.value = value self._nextnode = nextnode def append(self, n): if not isinstance(n, Node): n = Node(n) self._nextnode, n = n, self._nextnode self._nextnode._nextnode = n
In Python, you can directly use dict to set dict to represent a graph. graph [u] [v] = w indicates a directed edge from u to v, the edge weight (or other information to be marked) is w.
If it is an undirected graph, you can limit u to <= v to avoid waste.
If u to v has multiple edges, dict sets, graph [u] [v] = {w0, w1 ..}
If the sequential relationship between the surviving edges or nodes is required, change dict/set to ordereddict.
The preceding method is equivalent to an adjacent table represented by hash and is suitable for sparse graphs. For a dense graph, you can use NumPy to store the adjacent matrix to reduce the overhead.
For details about how to design the Graph Processing API in a mature Python library, see Overview-NetworkX.
. There are many methods:
- Embedded data structures (such as list, dict, and tuple) are used for nesting/combination. they implicitly include the pointing/nesting relationship, as shown in the graph [u] [v] = {w0, w1 ..}
- The member variables and Nested classes of the class may contain the pointing/nesting relationship;
- References indicate pointing to links, but references cannot be computed like pointers. for example, p + 1 points to the next element, so there may be many restrictions.
- ...
Example in Leetcode
Class TreeNode: # The class structure already contains the def _ init _ (self, x): self. val = x self. left = None self. right = Nonel1 = TreeNode (0) l2 = TreeNode (1) l3 = TreeNode (2) # reference indicates to link l1.left = l2l1. right = l3
This can be answered angrily because the linked list has just been implemented in java. python does not comment on java and linked lists.
First of all, many people mentioned above that references are almost restricted pointers, which can fully implement pointer functions.
Code of the first node
private class Node
{ public Node
next; public T data; public Node(T d,Node
n) {data = d; next = n;}}
Java references will not be mentioned. all the variables in python are actually object references (even the basic types are the same). In other words, this reference is a pointer to the instance.
So it's not that there are no pointers, but they are all pointers, but you can't see them.
You can do the linked list. for python, there are two attributes in a class. next is still next, and you can point to the instance of the next node. The "reference" of Python and Java is a pointer that cannot be operated.
In fact, I prefer the concept of the go language and do not have any "references". Therefore, I avoid obfuscation of the "transfer references" concept. All values are passed, and pointers are pointers, but they cannot be computed. In fact, Java and python can be seen as hiding pointers with syntactic sugar at the language layer.
In Java, if class A has the attributes int a, B (class B) B, then A instantiate (B is newly created in A instance rather than being assigned an external value ), the memory opens up a reference address that contains an int A and B. B is instantiated like A, and then the address is thrown to the reference of B in. In fact, this is similar to B * B in A in C/C ++. Therefore, the implementation of linked lists, charts, and other data structures in C/C ++ is escaped to Java, and the expression of syntactic sugar is also changed.
In Python, data structures such as linked lists and trees correspond to Python's built-in list, tuple, and dict. Python, a dynamic language, is implemented in C. for Python list, it is implemented using a struct PyListObject to see that source code pointers are everywhere.
C/C ++ is similar to the way in which computer data is abstracted in the early days. pointers are actually the products of the glue layer that we Abstract Data and the data is aggregated but actually discrete in the memory. Later, the language abstracted the Data Representation in other ways, and hidden the pointer concept. it seems friendly, but he is still there. Basically, no pointer can be implemented in a more flexible way. The recycle mechanism can be directly discarded to automatically release the memory, which is also simple to write. The following is a simple python linked list:
# -*- coding: utf-8 -*-class Node(object): def __init__(self, datum, next): self.__datum = datum self.__next = next @property def datum(self): return self.__datum @property def next(self): return self.__next @next.setter def next(self, next): self.__next = nextclass LinkedList(object): def __init__(self): self.__head = None self.__tail = None @property def head(self): return self.__head @property def tail(self): return self.__tail def purge(self): self.__head = None self.__tail = None def is_empty(self): return self.__head is None def append(self, item): #Generates the node. datum = Node(item, None) #For the first node in a linked list, it will change the head. if self.__head is None: self.__head = datum else: self.__tail.next = datum self.__tail = datum def remove(self, item): pointer = self.__head prepointer = None #Finds the item while pointer and pointer.datum != item: prepointer = pointer pointer = pointer.next #Raise the error when not finding the item. if pointer is None: raise KeyError #When the linked list has one node, it will sets the head. if pointer == self.__head: self.__head = pointer.next else: prepointer.next = pointer.next if pointer == self.__tail: self.__tail = prepointer
In fact, this structure can be implemented without pointers at all.
See cons