-3

所以我正在尝试用 C++ 编写一个链表,但我不知道该怎么做。我或多或少地理解了这个概念,我看到了一个给我这段代码的教程,但它并没有很好地解释它。我想知道你们是否可以帮助我完成接下来的步骤,向我解释我刚刚做了什么,并向我解释如何继续。我希望它以堆叠的方式添加元素,弹出或推送元素,后进先出,最后释放元素并释放内存以防止内存泄漏。谢谢你。

    #include <iostream>
using namespace std;

struct node
{
    int num;
    node *link;
}*p;

void main()
{
    node *root;

    root = new node;
    root->num=5;
    root->link = p;
    p = root;

    node *q;

    for(q = p; q != NULL; q = q->link)
    {
        cout<<q->num;
        cout<<endl;
    }
}
4

2 回答 2

0

一个简单的链表演示

#include <iostream>
using namespace std;

typedef struct NODE
{
    int value;
    NODE *next;
};



int main()
{
    int opt;
    NODE *root = NULL;
    NODE *tmp = NULL;
    NODE *last = NULL;
    do
    {
        cout << "Simple Singly Linked-List Example\n\n";
        cout << "\t1. Create Root\n";
        cout << "\t2. Add Node\n";
        cout << "\t3. Delete First Node\n";
        cout << "\t4. Display Last Node\n";
        cout << "\t5. Display Nodes\n";
        cout << "\t6. Exit Program.\n\n";

        cout << "Enter a choice : ";
        cin >> opt;

        switch (opt)
        {

            case 1: // create root;
                if (root != NULL) // root already exists no need to ceate one
                {
                    cout << "Root already exists\n\n";
                    break;
                }

                root = new NODE;
                cout << "Enter Value for new Node : ";
                cin >> root->value;
                root->next = NULL;

                // root & last will be same for the when the node count is ONE
                last = root;
                cout << "\nRoot node has been created successfully\nPress any key to continue...\n\n";
                cin.get();

                break;

            case 2:
                if (root == NULL)
                {
                    cout << "Create root node first\n\n";
                    break;
                }

                tmp = new NODE;
                cout << "Enter Value for new Node : ";
                cin >> tmp->value;
                tmp->next = NULL;
                // attach it to the last node
                last->next = tmp;


                //set newly created node as last node
                last = tmp;
                cout << "\nRoot node has been created successfully\nPress any key to continue...\n\n";
                cin.get();
                break;

            case 3:
                if (root == NULL)
                {
                    cout << "No nodes to delete\n\n";
                    break;
                }

                // we have to delete the root node and set the next node as root
                tmp = root->next;
                cout << "Deleted the node with value : " << root->value << "\nPress any key to continue...\n\n";
                delete root;
                cin.get();

                //set second node as root now
                root = tmp;
                break;

            case 4:
                if (root == NULL)
                {
                    cout << "No nodes to delete\n\n";
                    break;
                }

                // delete the very last node (easy) and update the next pointer of second last node to null (tricky)

                //first lets find the second last node
                tmp = root;
                while(tmp->next != NULL)
                {
                    tmp = tmp->next;
                }
                //update the second last node next pointer to null
                cout << "Deleted the node with value : " << last->value << "\nPress any key to continue...\n\n";
                cin.get();
                delete last;

                //update second last one as last node
                last = tmp;
                last->next = NULL;

                break;
            case 5:
                if (root == NULL)
                {
                    cout << "No nodes to disply\n\n";
                    break;
                }

                tmp = root;
                cout << "Node Values  : ";
                while(tmp)
                {
                    cout << tmp->value << ",\t";
                    // set to print next node
                    if (tmp->next)
                        tmp = tmp-next;
                    else
                        break;

                }
                cout << "\nPress any key to continue\n\n";
                cin.get();
                break;

            default:
                cout << "Invalid Option\n\nPress any key to continue...\n\n";
                cin.get();

        }
    }while(opt != 6);

    // always a good practise to delete objects u have created;
    tmp = root;
    while(tmp)
    {
        //sorry for using the last node pointer, dont seems subtle
        last = tmp;
        tmp = tmp->next;
        cout << "Deleted Node with value : " << last->value << "\n";
        delete last;
    }

    return 0;
}

检查有关链接列表的几个链接

链接#1

链接#2

链接#3 <--非常有用,我仍然在书签中

Link #4 <--单链表,最基本的!

于 2013-01-24T08:04:59.710 回答
0

首先,既然您已经使用了标准库 ( #include <iostream>),请注意标准库已经有列表: ( #include <list>) 以及其他容器

你可以在这里找到他们的文档

如果您想自己编写,请使用适当的方法:不要将所有内容都放入 main,而是定义适当的类和方法来操作元素。您可以参考我作为“灵感”链接的标准文档。

你可能最终会得到类似的东西

class list
{
    struct element
    {
        element* pnext;
        int value;
    };

    element* root;

public:

    list() :root() 
    {}

    ~list() 
    { while(root) pop(); }

    list(const list&) =delete;

    list& operator=(const list&) =delete;

    void push(int val) 
    { 
        element* p = new element;
        p->value = val;
        p->pnext = root;
        root = p;
    }

    void pop()
    {
        element* pe = root;
        root = pe->pnext;
        delete pe;
    }

    class index
    {
        element* p;
        index(element* s) :p(s) {}
        friend class list;
    };

    index start() const const 
    { return index(root); }

    index next(index i) const 
    { return index(i.p->pnext); }

    int at(index i) const 
    { return i.p->value; }

    bool is_end(index i) const 
    { return i.p == nullptr; }
};
于 2013-01-24T08:11:32.190 回答