Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jul-12 17:16pm    
Not clear what's your problem. Linked lists and stacks are described everywhere. What have you done so far?
--SA
Vitaly Tomilov 2-Jul-12 19:16pm    
Does that even look like a message subject? Just really bad way of approaching a question, which discourages from answering it...

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
 
Share this answer
 
v4
Comments
jayburn00 2-Jul-12 18:40pm    
Actually, the problem is that I have not used C++ for atleast a year, and have only been messing around with python scripts. As a result, I am failing royally when it comes to having the correct syntax and having things written in a way that C++ understands. I know what a linked list is and how it works, I have trouble writing the code to make it is the problem I have. Actually, I have made a linked list, but a very basic one. I don't know how I will make a random one each time the while loop goes through. That is my real problem. I can post my linked list code if you want, it works, but it does not have the functionality that is required, I don't know how to fill it randomly, delete and create a new one each time the while loop goes through. Basically, I can make one linked list, but not something that makes one and then replaces it with a new one each time the loop goes through.
Sergey Alexandrovich Kryukov 2-Jul-12 18:47pm    
"I have trouble" is not really informative. How can it provide a clue on how to help you?

One year is nothing, if you just want to come back to the trail.

From your complains, I don't really know how else to help you. Perhaps you just need to start writing code, and not right from the recipes, but using you own brain, and if you face a problem, try to overcome it. If you stuck with some the problems, then ask another question and show what have you done so far. Do you think there is another way? I don't think so, unless someone just do all your work for you. Care to wait for that? :-)
--SA
jayburn00 2-Jul-12 19:48pm    
Ok, a specific problem I am having is making a linked list that gets filled with random data of a specific size. I can make a linked list with data that is defined within the code, but I don't know how to randomly fill the list.
Sergey Alexandrovich Kryukov 2-Jul-12 20:02pm    
Random value or randomly selected nodes, or both. There is nothing special you need to know; you just need to do the development.
Use:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
--SA
jayburn00 2-Jul-12 20:31pm    
I know about that. I know how to do randomness, I usually use time for a seed. What I don't know is how to fill each node with one of three randomly selected variables. I can put in code that defines the value without it choosing "randomly", but I don't know how to make the list randomize each time it is created. Let me put it this way: I can make 1->2->3->1->2->2 by specifying exactly what each part of the list holds, but I can't do this ?->?->?->? where I don't actually know what order it will put the numbers in each time the list is generated. I can also do x=? where I make a single variable random. I tried to do it similar to defining a single variable as a random number, but it didn't work.
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[^]

C++
#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;
}


EDIT: 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.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900