我必须制作自己的双链表。我是初学者,所以请原谅我的知识不足。这个列表必须实现 List java 接口,所以我也有一个 remove(int)、一个 remove(Object) 和一个 clear() 方法,其中 clear() 方法不做它的工作,这意味着它不删除列表的所有元素,只有一些。
这是 clear() 方法:
public void clear() {
for (T t : this) {
this.remove(t);
}
this.remove(this.size);
}
Run Code Online (Sandbox Code Playgroud)
remove(Object) 方法:
public boolean remove(Object o) {
if (this.indexOf(o) >= 0){
remove(this.indexOf(o));
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
最后,remove(int) 方法:
public T remove(int index) {
if (getNode(index) == null || this.isEmpty()) {
return null;
} else if (this.size == 1){
this.size = 0;
Node<T> currentNode = this.firstNode;
this.firstNode = null;
this.lastNode = …Run Code Online (Sandbox Code Playgroud) 我现在正在研究泛型,根据我的理解,我们使用泛型,所以我们可以避免/消除强制转换的需要,那么为什么在添加项目和链接列表时我必须进行强制转换?
public class ThisIsCode<E> implements Code{
LinkedList<E> list = new LinkedList<>();
public final void add(E... item) {
List<E> thingie = new LinkedList<>();
for (E i: item) {
thingie.add((E) item);
}
list.add((E)thingie);
}
Run Code Online (Sandbox Code Playgroud)
我也必须在这个方法中进行转换,并在数组中?我不认为我应该将项目放入数组中,所以我有点困惑.
public void addSingle(Object item) {
add((E[]) item);
}
Run Code Online (Sandbox Code Playgroud) 我在C中创建了一个非常简单的链表列表程序.
#include<stdio.h>
#include<stdlib.h>
int main(){
struct Int{
int num;
struct Int *ptr;
};
typedef struct Int NODE;
NODE *start;
start = (NODE *) malloc(sizeof(NODE));
(*start).num = 100;
(*start).ptr = (NODE *) malloc(sizeof(NODE));
(*start).(*ptr).num = 123;
(*start).(*ptr).ptr = NULL;
}
Run Code Online (Sandbox Code Playgroud)
当我将最后两行替换为: -
start -> ptr -> num = 123;
start -> ptr -> ptr = NULL;
Run Code Online (Sandbox Code Playgroud)
错误解决了.
问题是为什么我不能用(* start).而不是.start ->根据这个答案这是什么意思" - >"?
两者都是一样的.
一个单链表,我想通过递归来修改它。但我不明白这一行的含义head->next->next = head;。
为什么需要head->next->next?
struct Node{
int data;
Node* next;
};
Run Code Online (Sandbox Code Playgroud)
下面是实现代码:
Node* reverseByRecursion(Node *head)
{
if(head == NULL || head->next == NULL)
return head;
Node *newHead = reverseByRecursion(head->next);
head->next->next = head;
head->next = NULL;
return newHead;
}
Run Code Online (Sandbox Code Playgroud) 这是我的Node类:
private class Node<T>
{
public T Data { get; set; }
public Node<T> PreviousNode { get; set; }
public Node<T> NextNode { get; set; }
public Node(object data, Node<T> next, Node<T> previous)
{
Data = (T) data;
PreviousNode = previous;
NextNode = next;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的链接列表类的一部分,这里是我的反向功能存储:
public class DoublyLinkedList<T> :IList<T>
{
private Node<T> headerNode;
public DoublyLinkedList()
{
headerNode = new Node<T>(null, null, null);
headerNode.NextNode = headerNode;
headerNode.PreviousNode = headerNode;
Count = 0;
}
public void Insert(int index, T …Run Code Online (Sandbox Code Playgroud) 我正在尝试c使用结构动态创建链接列表并打印它.但我的下面的代码是抛出运行时错误可以任何人告诉我为什么我收到此错误.这是我的代码.
#include <stdio.h>
struct cnode
{
int value;
struct cnode *next;
};
void print_list(struct cnode* start)
{
while(start->next != NULL)
{
printf("%d->", start->value);
start = start->next;
}
}
int main(void)
{
int i,n,val;
//List length
scanf("%d", &n);
//Head
struct cnode* start;
scanf("%d", &val);
start->value = val;
struct cnode* temp = start;
for (i=1; i<=n-1; i++)
{
struct cnode* node;
scanf("%d", &val);
node->value = val;
temp->next = node;
temp = node;
}
temp->next = NULL;
print_list(start);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 是否可以自己创建一个类的新对象(在python中)?
为了进一步解释这个想法,我编写了这段代码,但我认为它不起作用。
新对象应明显独立于当前对象(新属性等)。
class LinkedList:
def __init__(self):
""" Construct an empty linked list. """
self.first = None
self.last = None
def insert_before(self, new_item, next_item):
""" Insert new_item before next_item. If next_item is None, insert
new_item at the end of the list. """
# First set the (two) new prev pointers (including possibly last).
if next_item is not None:
new_item.prev = next_item.prev
next_item.prev = new_item
else:
new_item.prev = self.last
self.last = new_item
# Then set the (two) new next pointers (including …Run Code Online (Sandbox Code Playgroud) 由于LinkedList的Java实现是双向链表.如果说传递给get get(int index)方法的索引超过了链表的中间,那么它是否更有意义呢?该值是使用列表尾部的降序迭代器获得的?
这会发生吗?
如果不是为什么?
示例说我有一个如下所示的链接列表:
head tail
A <-> B <-> C <-> D <-> E <-> F <-> G
Run Code Online (Sandbox Code Playgroud)
我说linkedList.get(5)得到第二个linkList中的最后一个元素.java会在内部使用降序迭代器(从尾部开始)来检索F,还是不行?由于5已超过链表的中间位置.
In this function, I get segmentation fault. I think it has something to do with memory allocation. What mistake am I making?
Now, if I initialize Node* a =NULL, i get my head pointer as NULL in the end.
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* addTwoLists(Node* first, Node* second) {
// Code here
Node *a;
Node *head = a;
int bor = 0;
while(first->next && second->next) …Run Code Online (Sandbox Code Playgroud) 我不问如何找到它,那就是在已经回答了这个其他问题:
但是,仅深入Java语言中的数据结构,我发现LinkedList实现具有一个getLast()方法,该方法在ArrayList实现中未实现(出于某些原因,我认为是这样)。我找不到其他类似的问题,也无法在Internet上发布解释该问题的信息,因此我决定在这里提出此问题。
我们可以认为,这是不优雅从一个ArrayList获得最后一个元素的电流的方式,通常这种实现比LinkedList的更广泛的应用,因为它执行在更大范围内的情况下更好,因为讨论在这里。
有人知道为什么ArrayList不实现该方法吗?
编辑:我已编辑我的问题,以避免混淆和基于意见的答案。根据事实和参考,安德烈亚斯(Andreas)的以下答案是我一直在寻找的答案。