我正在阅读一个教程,我也在谷歌上搜索过,但我找不到关于链接列表如何工作的详细说明的很好的解释......我真的对结构/格式感到困惑,我真的希望链接列表有意义对我来说,它们听起来很棒,因为它们是一个可调整大小和可修改的数组……如果您需要了解我在说什么,下面是我从 tut 中获得的一些代码。我对追加方法或删除等方法、它们的作用以及列表中的尾头事物的工作方式感到困惑......这本书只是从一个例子开始,没有给出解释..
请帮助解决这个困惑..
class ListEntry
{
int data;
ListEntry next;
public ListEntry( int d )
{
data = d;
next = null;
}
public int Data
{
get{ return data; }
set{ data = value; }
}
public ListEntry Next
{
get{ return next; }
set{ next = value; }
}
public override string ToString( )
{
return( data.ToString( ) );
}
}
class TestProgram
{
static void Main( )
{
List list = new List( );
list.Append( 3); …Run Code Online (Sandbox Code Playgroud) class Node(object):
def __init__(self, data = None, next = None):
self.data = data
self.next_node = next
def get_data(self):
return self.data
def get_next(self):
return self.next_node
def set_next(self, new_next):
self.next_node = new_next
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def insert(self, data):
new_node = Node(data, None)
if self.head is None:
self.head = new_node
else:
new_node.next = self.head
self.head = new_node
def print_list(self):
temp = self.head
while temp!=None:
print(temp.data)
if temp.next != None:
temp = temp.next
s = LinkedList()
s.insert(3) …Run Code Online (Sandbox Code Playgroud) 请注意它是如何以合理的方式删除它们的.这就像它改变了数组中的所有内容,但这不仅仅是ArrayLists的作用吗?
public static void main(String[] args) {
// create a list of integer to Test our logic.
LinkedList<Integer> halfOf = new LinkedList<Integer>();
halfOf.add(1);
halfOf.add(2);
halfOf.add(3);
halfOf.add(4);
halfOf.add(5);
halfOf.add(6);
halfOf.add(7);
halfOf.add(8);
halfOf.remove(0);
halfOf.remove(1);
halfOf.remove(2);
System.out.println(halfOf);
Run Code Online (Sandbox Code Playgroud)
给出输出:
[2, 4, 6, 7, 8]
Run Code Online (Sandbox Code Playgroud) 最佳排序算法,可用于使用c中的结构对链表进行排序,其中获取错误的可能性最小(例如,分段)?
我正在学习链表.我创建了一个模板实现,包括构造函数,插入器,析构函数,复制构造函数和重载赋值运算符.问题是我的测试程序在重载赋值运算符后没有输出任何内容.
对于我的赋值运算符,我使用Clear()函数在复制之前完全清除列表.我把它放在析构函数中并检查它是否正常工作.我还检查了我的复制构造函数,它也运行良好.
文件node.h:定义节点构建块
#include <iostream>
using namespace std;
template <typename T>
struct Node{
T _item;
Node<T>* _next;
Node() {
_item = T();
_next = NULL;
}
Node(T item){
_item = item;
_next = NULL;
}
// Print the value of a node
friend std::ostream& operator <<(std::ostream& outs, const Node<T> &printMe){
outs << "[" << printMe._item << "]";
return outs;
}
};
Run Code Online (Sandbox Code Playgroud)
文件list.h:定义链接列表模板
#include "node.h"
template <class T>
class List {
public:
// …Run Code Online (Sandbox Code Playgroud) 如果我们拥有std::set并且std::vector可以动态增长和缩小,为什么我们需要链表?
NB我真的不明白,为什么会有这么多的选票.唐氏选民,请留下评论.
在C++中存在一个公共结构数据库,例如在Java中有它java.util.Collection的所有子类.C++中也有类似的东西吗?我不会为我使用的每个数据结构编写代码.
class node{
public: node* next;
int data;
};
void insert_node(node* head, int val){
node* n = new node();
n->next = NULL;
n->data = val;
cout << n->data << endl;
if (head == NULL){ //point .1.
head = n; //point .2.
cout<<"checkpoint 1 reached"<<endl;
}
else{
node* p = head;
while (p->next !=NULL)
p= p->next ;
p->next = n;
head->next = n;
}
return ;
}
int main(){
node* head = NULL;
insert_node(head, 3);
insert_node(head, 4);
insert_node(head, 5);
insert_node(head, 6);
insert_node(head, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个涉及链表的程序.我编写了一个函数,它返回链表中的第n个节点,它以递归方式调用自身.我的程序编译并运行直到递归函数然后崩溃.这是节点的构造函数以及递归函数:
LinkedList::LinkedList():
head(head){
sizeInt = 0;
}
Node* LinkedList::get_nth(const int& n) const {
Node* node = new Node();
for(int counter = 1; counter <= n; counter++){
node = get_nth(counter + 1);
}
return node;
}
Run Code Online (Sandbox Code Playgroud)
这个功能有什么问题?如果您需要更多详细信息或代码,请告诉我们.
public LinkedList<MazeCoord> getPath() {
return getPath();
}
public class MazeCoord {
final private int row; // final (non-static) means it can't be updated once
final private int col; // it's initialized
// create a MazeCoord with the given row and column
public MazeCoord(int row, int col) {
this.row = row;
this.col = col;
}
// get the row of this MazeCoord
public int getRow() { return row; }
// get the col of this MazeCoord;
public int getCol() { return …Run Code Online (Sandbox Code Playgroud)