如何制作链表

时间:2013-01-24 08:01:34

标签: c++

所以我试图在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;
    }
}

2 个答案:

答案 0 :(得分: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;
}

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

Link # 1

Link # 2

Link # 3&lt; - 非常有用,我仍然在书签中

Link # 4&lt; - 单独 - 链接列表,基本的所有!

答案 1 :(得分:0)

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

您可以找到他们的文档here

如果您想自己编写,请使用正确的方法:不要将所有内容都放在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; }
};