如何随机创建一个包含20个包含3个不同对象的元素的链接列表,然后使用while循环删除旧列表并创建一个新列表? [英] How do I randomly create and then fill a linked list with 20 elements containing 3 distinct objects, and then use a while loop to delete the old list and create a new one?

查看:80
本文介绍了如何随机创建一个包含20个包含3个不同对象的元素的链接列表,然后使用while循环删除旧列表并创建一个新列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++编写一个同时使用堆栈和链接列表的程序.我不知道为什么需要使用链接列表(根据我的阅读,矢量会更有效),但我们必须这样做.这不是可选的.有3个形状对象(我认为它们是类对象),矩形",三角形"和圆形".我需要创建一个包含20种形状的链表,这些链表是随机生成的,并且该列表是在while循环中生成的.每当while循环进行一次迭代时,都会创建一个新的随机生成的链表,并删除或清除旧的链表.当用户键入某个输入时,列表开头的形状将被放置在堆栈中,或者根本不将其放置在堆栈中,而只是将其破坏,然后将下一个对象移到第一个位置.我如何制作这样的链表?我在制作一个链表时遇到了麻烦,更不用说这些规格了.哦,我在Google上做了这个搜索,发现没有什么帮助.

I am trying to write a program in C++ that uses both stacks and linked lists. I don''t know why we need to use a linked list (vector would have been more effective based on what I read), but we have to. It is not optional. There are 3 shape objects(they are class objects I think), "rectangle", "triangle", and "circle". I need to create a linked list containing 20 of these shapes, generated randomly, and the list is generated in a while loop. Each time the while loop goes through an iteration, a new randomly generated linked list is created and the old one is deleted or cleared. The shape at the beginning of the list is put in the stack when the user types in a certain input, or it is not put in the stack at all, but simply destroyed, with the next object moving to the first position. How do I make a linked list like this? I am having trouble simply making a linked list let alone one with these specs. Oh, and I did google this, and nothing I found was much help.

推荐答案

请参阅此CodeProject文章中链表的基本说明:
如何使用C/C ++创建链接列表 [ ^ ].

我不明白您怎么会错过链表上的资料.也许您尝试将问题文本中几乎所有内容都写下来,并希望以这种形式准确地找到所有细节?是这样,那将是非常幼稚的.您只需要使用一个一般性的想法,然后您就可以动脑了.如果您只了解指针的工作原理,甚至可以不做任何阅读就从头开始制作它们.如果您不太清楚地了解指针主题,则需要查找有关C ++指针而不是链接列表的信息.

这几乎是您需要了解的所有内容:
http://en.wikipedia.org/wiki/Linked_list [ http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29 [ ^ ].

如果您仍然认为这不能为您提供全部信息,则应该寻求帮助来提高您的认知能力,而不是在链表上……:-)

—SA
Please see elementary explanation of linked lists in this CodeProject article:
How to create Linked list using C/C++[^].

I cannot understand how could you miss material on linked lists. Maybe you tried to write almost everything you have in the text of your question and hoped to find it exactly in this form with all your detail? Is so, it would be very naive. You only need to use a general idea, and then you brain. You could even make them from scratch without any reading, if you understand just how pointers work. If you don''t understand the pointers topics very clearly, you need to look for information on C++ pointers, not on linked lists.

This is pretty much all you need to understand:
http://en.wikipedia.org/wiki/Linked_list[^],
http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29[^].

If you still think this does not give you all the information, you should look for help on improving your cognitive skills, not on linked lists… :-)

—SA


对不起,现在太累了,无法给出很多解释.也许这段代码可以帮助您将内容添加到列表中.

请注意,您可以在列表的头/尾添加/删除项目.在此处 [ [
Sorry, too tired right now to give much of an explanation. Perhaps this code will help you wrap your head around adding things to the list.

Note that you can add/remove items at the head/tail of the list. Have a look into std::list.pop_front and front over here[^]

#include <iostream>
#include <list>
#include <stdlib.h>

using namespace std;

class shape
{
    public:
        shape(){}
        virtual ~shape(){cout<<"boom\n";}
        virtual char *getType() = 0;     
    private:
};

class circle : public shape
{
    public:
        char *getType() {return "circle";}
};

class square : public shape
{
    public:
        char *getType() {return "square";}
};

class triangle : public shape
{
    public:
        char *getType() {return "triangle";}
};


typedef list<shape*> shapeList_t;
typedef shapeList_t::iterator shapeListIter_t;

int main()
{
    int i, numItems=10;
    shapeList_t myList;
    shapeListIter_t myIter;
    shape *curShape;

    // Insert
    for (i=0; i<numItems; i++)
    {
        switch (rand() %  3)
        {
            case 0: myList.push_back(new circle); break;
            case 1: myList.push_back(new square); break;
            case 2: myList.push_back(new triangle); break;
        }
    }

    // Display
    for (myIter=myList.begin(); myIter!=myList.end(); myIter++)
    {
        curShape = *myIter;
        cout << "Shape type: " << curShape->getType() << endl;
    }

    // Cleanup
    for (myIter=myList.begin(); myIter!=myList.end(); myIter++)
    {
        curShape = *myIter;
        delete curShape;        // free the memory that this list item consumes - calls curShape's destructor
    }
    myList.clear();             // free the memory that the list uses to point to each list item - empties the list

    return 0;
}



刚意识到我弄得一团糟-没有调用每种形状的正确析构函数-只有基类的析构函数是. Mea Culpa.

已修复-忘记将virtual关键字添加到基类的析构函数中.



Just realized I made a mess of things - the correct destructor for each shape isn''t called - only the base-class''s one is. Mea Culpa.

Fixed - forgot to add the virtual keyword to the base class destructor.


这篇关于如何随机创建一个包含20个包含3个不同对象的元素的链接列表,然后使用while循环删除旧列表并创建一个新列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆