将工程可扩展性融入应用程序(Engineering scalability into an application)
这意味着什么 - 在应用程序中设计可伸缩性。 设计模式是否会使应用程序更具可扩展性? 这个问题主要是在Web应用程序或基于SOA中间件的应用程序的上下文中。
What does it mean to say - Engineering scalability into applications. Are there design patterns that would make an application more scalable? This question is mainly in the context of web applications or SOA middleware based applications.
原文:https://stackoverflow.com/questions/1074114
最满意答案
前面的大事:这是一个二叉树,而不是链表。 将其称为链接列表只会导致混淆。
在主题上,递归不是链接列表的方法。 对于一棵树它可能是。 但你有什么不起作用有几个原因。
1)没有终止条件。 递归将犁入NULL指针并做坏事。 如果
nodePtr
为NULL,则首先在recursiveCreate
中退出。2)您正在设置当前节点以指向错误的东西。 例如,
n->right = ptr->right;
让节点指向错误列表中的节点。 这几乎是一个糟糕的结局。 您想指向由
recursiveCreate
创建的节点。让我们来看看这将是什么样子:
nodePtr LinkedList::recursiveCreate(nodePtr ptr) { if (ptr == nullptr) { return nullptr; // source tree stops here } nodePtr n = new node; //create new node n->elt = ptr->elt; //copy value into that new node n->right = recursiveCreate(ptr->right); //call on right node n->below = recursiveCreate(ptr->below); //call on below node return n; }
和
LinkedList::LinkedList(const LinkedList &list) { nodePtr current = nullptr; if (list.head == nullptr) // NAG!!! For Crom's sake! 0 is not a pointer! head == nullptr; // Use nullptr and get type checking working for you. else { head = recursiveCreate(list.head); } }
特殊奖金运营商
LinkedList & operator=(LinkedList list) // pass by reference. Copy constructor copies // for you { std::swap(head, list.head); // steal head from copy and give copy the empty return *this; // all done. }
The up front big thing: This is a binary tree, not a linked list. Calling it a linked list just leads to confusion.
On topic, recursion is not the way to go for a linked list. For a tree it could be. But what you have does not work for a couple reasons.
1) There is no terminating condition. The recursion will plow into a NULL pointer and do bad things. First off in
recursiveCreate
you want to exit ifnodePtr
is NULL.2) You're setting the current node to point at the wrong stuff. For example,
n->right = ptr->right;
has the node pointing at a node in the wrong list. That's an almost certain bad end. You want to point at the node created by
recursiveCreate
.Let's take a look at what this would have to look like:
nodePtr LinkedList::recursiveCreate(nodePtr ptr) { if (ptr == nullptr) { return nullptr; // source tree stops here } nodePtr n = new node; //create new node n->elt = ptr->elt; //copy value into that new node n->right = recursiveCreate(ptr->right); //call on right node n->below = recursiveCreate(ptr->below); //call on below node return n; }
and
LinkedList::LinkedList(const LinkedList &list) { nodePtr current = nullptr; if (list.head == nullptr) // NAG!!! For Crom's sake! 0 is not a pointer! head == nullptr; // Use nullptr and get type checking working for you. else { head = recursiveCreate(list.head); } }
Special bonus operator
LinkedList & operator=(LinkedList list) // pass by reference. Copy constructor copies // for you { std::swap(head, list.head); // steal head from copy and give copy the empty return *this; // all done. }
相关问答
更多-
链接列表构造函数(Linked List constructor)[2019-11-27]
而不是设置curr = phead->next ,尝试phead->next = curr 您的代码似乎是将curr的值设置为phead-> next的值,这在此代码中永远不会更改。 在curr旁边设置phead->将使得phead列表包含在curr值中创建的节点,将curr添加到链接列表中。 我希望这有帮助! Instead of setting curr = phead->next, try phead->next = curr Your code appears to be setting the v ... -
链表的递归(Recursion of Linked List)[2022-05-24]
看起来你在递归中有点束缚。 我修改了您的方法以接受Node以及上一次迭代中的产品。 在迭代的每一步,我都更新已存在的List的值,因此不需要使用new运算符。 public static void product(Node curr, int value) { if (curr == null) { return; } else { int data = value * curr.getData(); // compute current produ ... -
递归和链表(Recursion and linked list)[2022-04-03]
递归是一种自我调用的方法吗? 是的,通常。 有关细节,示例和例外,请参阅Wikipedia 。 如果简化方法,您可以在自己的代码中理解递归。 您编写的代码更类似于过程代码,而Ruby通常使用更类似于此的面向对象的代码: class LinkedListNode attr_accessor :value, :next_node def initialize(value, next_node = nil) @value = value @next_node = next_node ... -
c ++ - 单链接循环列表的复制构造函数[关闭](c++ - copy constructor for singly linked circular list [closed])[2022-04-09]
代码不完整。 您必须首先确保正确构造CCircularList和srcList。 此外,由于您复制了引用节点数据和下一个地址,因此节点构造函数似乎有些错误。 我认为更正确可能有些像 Node::Node(const Node& oldNode) : nData(oldNode.nData) , next(nullptr) {;} Indeeed,错误表明你传递的某些sPtr为null。 你可以查看断言做 CCircularList::CCircularList(CCircularList& srcList ... -
而不是newNode-> value =(* thisNode-> value); 你需要newNode-> value = thisNode-> value; Instead of newNode->value = (*thisNode->value); you need to newNode->value = thisNode->value;
-
我想你只需要存储上一个节点(prv)。 假设您的节点有prv作为数据成员。 List(const List ©ing) : head(NULL) { Node* cur = copying.head; int size = copying.size(); Node* end = NULL; Node* prv = NULL: for(int q = 0; q < size; q++) { Node* n = new Node; ...
-
在没有实际看到代码的情况下,我无法肯定地说,但我会说,对于链表实现, 很可能需要复制构造函数和赋值运算符,以防止您的类具有破坏的复制语义由构造函数执行的动态分配,以及析构函数执行的取消分配。 如果你看到一个过去没有这些的实现,它很可能是它被打破/错误/错误,这在互联网上并不常见,那里存在无穷无尽的初学者实现并被传递掉作为“如何做到这一点”在非同行评审的教程中。 遗憾的是,它在大学教材中甚至相当普遍。 从一本好书中学习C ++,并阅读有关三级规则的内容 。 然后删除你的实现并使用std::list ! Wit ...
-
前面的大事:这是一个二叉树,而不是链表。 将其称为链接列表只会导致混淆。 在主题上,递归不是链接列表的方法。 对于一棵树它可能是。 但你有什么不起作用有几个原因。 1)没有终止条件。 递归将犁入NULL指针并做坏事。 如果nodePtr为NULL,则首先在recursiveCreate中退出。 2)您正在设置当前节点以指向错误的东西。 例如, n->right = ptr->right; 让节点指向错误列表中的节点。 这几乎是一个糟糕的结局。 您想指向由recursiveCreate创建的节点。 让我们来 ...
-
链接列表连接(Linked List Concatenation)[2022-09-09]
要连接两个链接列表,必须使第一个列表的最后一个节点指向第二个列表的第一个节点。 Node first_list = ... // head node Node second_list = ... // head node ... Node last_node = first_list.getLastNode() last_node.setNext(second_list) 现在专注于实现getLastNode() 。 它可以通过使用递归或迭代非常简单地完成,字面上是2行。 To concatenate ... -
您的循环条件不正确。 您希望在有下一个节点时循环。 你正在做相反的事情: while(!copyPtr) 应该: while (copyPtr) 另请注意,如果要复制的列表为空,则复制构造函数将不正确。 在进行任何检查之前,请取消引用listObj.head 。 Your loop condition is incorrect. You want to loop while there is a next node. You are doing the opposite: while(!copyPtr ...