English 中文(简体)
C++ 中的列表执行情况
原标题:List implementation in C++
  • 时间:2012-05-26 17:07:41
  •  标签:
  • c++
  • list

我这次没有问题, 但我只是在找列表执行, 只是一个简单的列表, 上面有节点, 如诺德( int, nextNode) 。 过去我做过很多次, 但我的 c++ 有点生锈了 。 您能分享一下吗? 我在档案中, 在胶囊上搜索, 但我没有找到刺青 。

编辑:

*I decided to do mine and I don t understand why after using delete I still can get cout<getWrt()<*

#include <cstdio>
#include <cmath>
#include<iostream>

using namespace std;

class Node{

public:
    Node(Node* next, int wrt){
        this->next = next;
        this->wrt = wrt;

    }

    Node(const Node& obiekt){
        this->wrt = obiekt.wrt;
        this->next = obiekt.next;
    }
    ~Node(){}

    void show(){
        cout<<this->wrt<<endl;
    }

    int getWrt(){
        return this->wrt;
    }

    Node* getNext(){
        return this->next;
    }

 private:
    Node* next;
    int wrt;

};


int main()
{
Node* n  = new Node(NULL, 2);
n->show();
Node* n2 = new Node(*n);
n2->show();
delete n;
n->show();
n2->show();
return 0;
}
问题回答

基本清单的执行通常称为单项关联清单(或功能语言综合清单)。

功能定义直接切入清单的结构:

List := Empty | Cons T List

当然,这在C或C++并不真正有效, 所以我们需要将结构分为两个:

  • The list is implemented as a chain of Node
  • A List class hides this implementation detail

以下是一些简单的代码:

template <typename T>
struct Node {
    Node(T t): element(t) {}

    T element;
    std::unique_ptr<Node> next;
};

template <typename T>
class List {
    typedef Node<T> N;
public:
    List() {}

    bool empty() const { return head == nullptr; }

    T& front() { assert(!this->empty()); return head->elem; }
    T const& front() const { { assert(!this->empty()); return head->elem; }

    void pop() { assert(!this->empty()); swap(head, head->next); }

    void push(T t) {
        std::unique_ptr<N> n{ new Node {t} };

        n->next = std::move(head);

        head = std::move(n);
     }

private:
    std::unique_ptr<N> head;
};

您可以看到, 这个列表只是作为堆叠、 没有迭代等执行的...... 不过, 这是一个良好的开端 :

Aix说,你最好的赌注是和...

  • std::list, which is a doubly-linked list, which means it trades backward traversal speed for memory usage or
  • the less widely implemented std::slist (called forward_list in C++11), which is singly-linked and can only be traversed in one way.

"http://www.cplusplus.com/reference/stl/" rel="no follow"当然, cplusplus.com有这两个网站的参考信息。

作为STL的一部分,两个清单的实施都经过了广泛的测试、调整和调试。两者都支持标准STL算法,几乎没有理由不使用它们。





相关问题
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?