Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表标题C++;_C++_Pointers_Linked List - Fatal编程技术网

C++ 链表标题C++;

C++ 链表标题C++;,c++,pointers,linked-list,C++,Pointers,Linked List,我正在为data structures类制作一个简单的链表,并且很难理解头指针应该如何工作 我有 template <typename E> class SSLL{ template<typename E> struct Node { E data; Node* next; }; public: template <typename E> SSLL(); void

我正在为data structures类制作一个简单的链表,并且很难理解头指针应该如何工作

我有

template <typename E>
class SSLL{
template<typename E>
        struct Node {
            E data;
            Node* next;
        };
public:
template <typename E>
    SSLL();
    void push_front(E element);
private:
    Node<E> * head;
    Node<E> * tail;
};
template <typename E>
SSLL<E>::SSLL() {
    head = NULL;
    tail = NULL;
}
template <typename E>
void SSLL<E>::push_front(E element) {
    Node<E> * n = new Node<E>;
    n->data = element;
    n->next = head;
    head = n;
    if (!tail) {
        tail = n;
    }
}
模板
类SSLL{
模板
结构节点{
E数据;
节点*下一步;
};
公众:
模板
SSLL();
空推前(E元件);
私人:
节点*头;
节点*尾部;
};
模板
SSLL::SSLL(){
head=NULL;
tail=NULL;
}
模板
无效SSLL::推前(E元件){
Node*n=新节点;
n->数据=元素;
n->next=头部;
水头=n;
如果(!tail){
尾=n;
}
}
但是,这不是使head成为列表中的第一个元素,而不是指向第一个元素的指针吗

我试图将push_-front(E元素)更改为这个,但是得到了空指针错误

template <typename E>
void SSLL<E>::push_front(E element) {
    Node<E> * n = new Node<E>;
    n->data = element;
    n->next = head->next;
    head->next = n;
    if (!tail) {
        tail->next = n;
    }
}
模板
无效SSLL::推前(E元件){
Node*n=新节点;
n->数据=元素;
n->next=头部->下一步;
head->next=n;
如果(!tail){
tail->next=n;
}
}
我在网上找到的所有例子都有head=n,但我仍然很难理解为什么是这样而不是head->next=n


谢谢。

在大多数形式的链表中,头指针指向列表中的第一个节点或为空:

         +---+     +---+     +--+  
head --> | A | --> | B | --> |/0|  
         +---+     +---+     +--+  
                     ^  
                     |  
tail ----------------+  
许多初学者将列表与节点混淆。节点是包含数据的对象。列表是节点的集合

头部推压
在前面插入需要执行以下步骤:

  • 使新节点指向头部节点
  • 将头部节点更改为指向新节点 1) 使新节点指向头部节点:

             +---+  
    p_new -->| C |
             +---+  
               |  
               V  
             +---+     +---+     +--+  
    head --> | A | --> | B | --> |/0|  
             +---+     +---+     +--+  
    
    2) 使头部指向新节点:

             +---+  
    p_new -->| C |  
    head  -->|   |
             +---+  
               |  
               V  
             +---+     +---+     +--+  
             | A | --> | B | --> |/0|  
             +---+     +---+     +--+  
    
    在C++中,这看起来像:

    Node * p_node = new Node;
    p_node->next = head; // Step 1.
    head = p_node; // Step 2.
    

    我们可以用简单的代码在C++中生成新的头:

    ListNode* dummy = new ListNode(0);
    dummy->next = head;
    

    看起来你需要一行一行地使用调试器逐步检查代码,以了解发生了什么。另一个调试链表的好工具是笔和纸。我不骗你。抽吸盘。绘制节点。每次更改一次,将列表重新配置为操作后列表应处于的新配置。将列表的转换与代码中的转换进行比较,并相应地调整代码。很好。那更容易阅读。问问自己当
    head
    NULL
    时,第一次插入列表时
    head->next
    会发生什么情况?我建议您购买一本更好的数据结构书或资源。始终让您的链接列表和其他程序在没有模板的情况下工作。将工作链表转换为使用模板很简单。