这个链表是如何流动的?
How does this linklist flow?
节点结构如下:
struct Node {
int value;
Node *next;
};
// construct a new node
Node * cons( int x, Node* p) {
return new Node{x, p};
}
现在如果我要输入我的正文:
Node *p;
p = cons(3,nullptr);
p = cons(2,p);
p = cons(1,p);
p = cons(4,p);
对于我的第一个节点,是否有比 nullptr 更好的价值开始?据我了解,这是 4 个节点的顺序。那是对的吗?节点值 3 是列表中的第一个。所以这个函数会按顺序搜索我的链接列表,因为它会查看节点值 3、2、1、4,然后是空节点。
//search linklist in order for value x
bool searchInOrder(int x, Node *p){
while(p->next != nullptr){
if(x == p->value)
return true;
p = p->next;
}
return false;
}
问题:
Is there a better value to start off with for my first node instead of nullptr
?
答案:
That is the best way to create the first node of a linked list.
问题:
From what I understand this is 4 Nodes in order. Is that correct? Node value 3 is the first on the list. So this function would search my linklist in order as in goes to view node value 3, 2,1,4,then empty node.
答案:
Yes there are 4 Nodes. However, the Node with value 3 is not the first Node in the list.
之后
p = cons(3,nullptr);
列表是:
+---+ +-----+
+ p + ---> | 3 | ---> NULL
+---+ +-----+
之后
p = cons(2,p);
列表是:
+---+ +-----+ +-----+
| p | ---> | 2 | ---> | 3 | ---> NULL
+---+ +-----+ +-----+
之后
p = cons(4,p);
列表是:
+---+ +-----+ +-----+ +-----+ +-----+
| p | ---> | 4 | ---> | 1 |---> | 2 | ---> | 3 | ---> NULL
+---+ +-----+ +-----+ +-----+ +-----+
节点结构如下:
struct Node {
int value;
Node *next;
};
// construct a new node
Node * cons( int x, Node* p) {
return new Node{x, p};
}
现在如果我要输入我的正文:
Node *p;
p = cons(3,nullptr);
p = cons(2,p);
p = cons(1,p);
p = cons(4,p);
对于我的第一个节点,是否有比 nullptr 更好的价值开始?据我了解,这是 4 个节点的顺序。那是对的吗?节点值 3 是列表中的第一个。所以这个函数会按顺序搜索我的链接列表,因为它会查看节点值 3、2、1、4,然后是空节点。
//search linklist in order for value x
bool searchInOrder(int x, Node *p){
while(p->next != nullptr){
if(x == p->value)
return true;
p = p->next;
}
return false;
}
问题:
Is there a better value to start off with for my first node instead of
nullptr
?
答案:
That is the best way to create the first node of a linked list.
问题:
From what I understand this is 4 Nodes in order. Is that correct? Node value 3 is the first on the list. So this function would search my linklist in order as in goes to view node value 3, 2,1,4,then empty node.
答案:
之后Yes there are 4 Nodes. However, the Node with value 3 is not the first Node in the list.
p = cons(3,nullptr);
列表是:
+---+ +-----+
+ p + ---> | 3 | ---> NULL
+---+ +-----+
之后
p = cons(2,p);
列表是:
+---+ +-----+ +-----+
| p | ---> | 2 | ---> | 3 | ---> NULL
+---+ +-----+ +-----+
之后
p = cons(4,p);
列表是:
+---+ +-----+ +-----+ +-----+ +-----+
| p | ---> | 4 | ---> | 1 |---> | 2 | ---> | 3 | ---> NULL
+---+ +-----+ +-----+ +-----+ +-----+