English 中文(简体)
如何在联系清单中将这种功能称为“新诺德”,以纳入另一个称为“普什框架”的职能?
原标题:How does one call the function "createNewNode" in a link list to insert it into another function called "pushFront"?
  • 时间:2024-02-17 00:56:35
  •  标签:
  • c++

在试图将一个称为“新诺德”的职能称为“推动阵线”的职能时,人们本应通过两个参数,但不知道它想要通过什么。 在试图说明这一点后,没有任何工作。 该守则是:

struct SingleLinkedListNode {
    int value;              //Integer value of the node
    SingleLinkedListNode* next = 0; //Pointer to the next node in the list
                //Next pointer is initialized to NULL
};

struct SingleLinkedList {
    SingleLinkedListNode* head = 0; //Pointer to the beginning of the list
    SingleLinkedListNode* tail = 0; //Pointer to the end of the list
    int size = 0;           //Counts the number of nodes in the list
};

SingleLinkedListNode* createNewNode(const int value, SingleLinkedListNode* next) {
    SingleLinkedListNode* n = new SingleLinkedListNode;
    n -> value = value;
    n -> next = next;

    return n;
}

void pushFront(SingleLinkedList& list, const int value) {
    //id the list is empty
    if (list.head == NULL) {          
        n = createNewNode(value, //I dont know);
        list.head -> n;
        n -> list.tail;
    }
    //else there is already a nod in the list
    else {
        n = createNewNode(value,);
        
        list.head -> n;
        n -> next      
    }
}
问题回答

In this case the second argument to createNewNode needs to be either nullptr if the list is empty, or list.head if it s not. In the first case we also need to update list.tail as well as list.head.

页: 1

void pushFront(SingleLinkedList& list, const int value) {
    if (list.head == nullptr) {          
        auto n = createNewNode(value, nullptr);
        list.head = n;
        list.tail = n;
        list.size = 1;
    }
    else {
        auto n = createNewNode(value, list.head);
        
        list.head = n;
        list.size++; 
    }
}

Of course, since this is C++, createNewNode should be a constructor for the LinkedListNode struct.

struct SingleLinkedListNode {
    int value;              
    SingleLinkedListNode* next = nullptr; 
  
    SingleLinkedListNode(int value, SingleLinkedListNode *next=nullptr)
    : value(value), next(next)
    { }
};

pushFront 应为<代码>的成员职能。 缩略语

struct SingleLinkedList {
    SingleLinkedListNode *head = nullptr; 
    SingleLinkedListNode *tail = nullptr; 
    int size = 0; 

    void pushFront(int value) {
        if (head == nullptr) {
            auto n = new SinglyLinkedListNode(value);

            head = n;
            tail = n;
            size = 1;
        }
        else {
            auto n = new SinglyLinkedListNode(value, head);

            head = n;
            size++;
        }
    }         
};

当然,节点是清单的执行细节,因此:

struct SingleLinkedList {
    private:
    struct Node {
        int value;              
        Node *next = nullptr; 
  
        Node(int value, Node *next=nullptr)
        : value(value), next(next)
        { } 
    };
    
    public:
    Node *head = 0; 
    Node *tail = 0; 
    int size = 0; 

    void pushFront(int value) {
        if (head == nullptr) {
            auto n = new Node(value);

            head = n;
            tail = n;
            size = 1;
        }
        else {
            auto n = new Node(value, head);

            head = n;
            size++;
        }
    }         
};

还有一个问题是,删除一个名单将使所有这些节点都离去,因此我们需要一个 des子。

Of course, you also need to think about move and copy behaviors, so review rule of three/five/zero.

struct SingleLinkedList {
    private:
    struct Node {
        int value;              
        Node *next = nullptr; 
  
        Node(int value, Node *next=nullptr)
        : value(value), next(next)
        { } 
    };
    
    public:
    Node *head = 0; 
    Node *tail = 0; 
    int size = 0; 

    ~SingleLinkedList() {
        Node *itr = head;

        while (itr) {
            Node *temp = itr->next;
            delete itr;
            itr = temp;
        }
    }

    void pushFront(int value) {
        if (head == nullptr) {
            auto n = new Node(value);

            head = n;
            tail = n;
            size = 1;
        }
        else {
            auto n = new Node(value, head);

            head = n;
            size++;
        }
    }         
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签