views:

3541

answers:

8

How can I create a list in C++? I need it to create a linked list. How would I go about doing that? Are there good tutorials or examples I could follow?

+14  A: 

You should really use the STL List class. Unless, of course, this is a homework question, or you want to know how lists are implemented by STL.

You'll find plenty of simple tutorials via google, like this one. If you want to know how linked lists work "under the hood", try searching for C list examples/tutorials rather than C++.

Roddy
sorry.. im studying all by my self,, not from school
jesse
A: 

Why reinvent the wheel. Just use the STL list container.

#include <list>

// in some function, you now do...
std::list<int> mylist; // integer list

More information...

mepcotterell
std::list is a parameterized type and new returns a pointer, your example should be more like: list<int>* mylist = new list<int>; or better yet, simply: list<int> mylist;
Ferruccio
-1 for posting code that doesn't even compile.
Evan Teran
A: 

We are already in 21st century!! Don't try to implement the already existing data structures. Try to use the existing data structures.

Use STL or Boost library

Vinay
Imo it's always a good idea to create some - simple - implementation of features or structures one tries to get a grasp of. ;)
karx11erx
I agree with karx. We had assignments like this in school and I would say, I am way more appreciative of the code that is already written, and more understanding of what it actually does because of it.A similar case would also be learning assembler. Knowledge of the abstracted is generally good.
Wes P
If it is a assignment then it is simple to implement, if it is a part of a larger project i.e., if it is used in a larger project to store the data then we should not implement we should use the existing library.
Vinay
+4  A: 

If you are going to use std::list, you need to pass a type parameter:

list<int> intList;
list<int>* intListPtr = new list<int>;

If you want to know how lists work, I recommending googling for some C/C++ tutorials to gain an understanding of that subject. Next step would then be learning enough C++ to create a list class, and finally a list template class.

If you have more questions, ask back here.

karx11erx
+1  A: 

I'm guessing this is a homework question, so you probably want to go here. It has a tutorial explaining linked lists, gives good pseudocode and also has a C++ implementation you can download.

I'd recommend reading through the explanation and understanding the pseudocode before blindly using the implementation. This is a topic that you really should understand in depth if you want to continue on in CS.

brien
this is not a home work question, im self studying
jesse
+11  A: 

I take it that you know that C++ already has a linked list class, and you want to implement your own because you want to learn how to do it.

First, read http://stackoverflow.com/questions/392397/arrays-whats-the-point , which contains a good answer of basic data-structures. Then think about how to model them in C++:

struct Node {
    int data;
    Node * next;
};

Basically that's all you need to implement a list! (a very simple one). Yet it has no abstractions, you have to link the items per hand:

Node a={1}, b={20, &a}, c={35, &b} d={42, &c};

Now, you have have a linked list of nodes, all allocated on the stack:

d -> c -> b -> a
42   35   20   1

Next step is to write a wrapper class List that points to the start node, and allows to add nodes as needed, keeping track of the head of the list (the following is very simplified):

class List {
    struct Node {
        int data;
        Node * next;
    };

    Node * head;

public:
    List() {
        head = NULL;
    }

    ~List() {
        while(head != NULL) {
            Node * n = head->next;
            delete head;
            head = n;
        }
    }

    void add(int value) {
        Node * n = new Node;
        n->data = value;
        n->next = head;
        head = n;
    }

    // ...
};

Next step is to make the List a template, so that you can stuff other values (not only integers).

If you are familiar with smart pointers, you can then replace the raw pointers used with smart pointers. Often i find people recommend smart pointers to starters. But in my opinion you should first understand why you need smart pointers, and then use them. But that requires that you need first understand raw pointers. Otherwise, you use some magic tool, without knowing why you need it.

Johannes Schaub - litb
A: 

hello, you can acess my article about list

http://alinebossi.wordpress.com/2010/03/01/listas/