linked list

Discussion in 'C++' started by Logan Lee, Feb 21, 2013.

  1. Logan Lee

    Logan Lee Guest

    #include <iostream>
    using namespace std;

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

    int main() {
    node* head;
    node* newnode;
    head=NULL;

    for (int i=0; i<3; i++) {
    newnode=new node;
    newnode->data=i+1;
    newnode->next=head;
    head=newnode;
    }

    while (head!=NULL) {
    cout << head->data << endl;
    head=head->next;
    }
    cin.get();

    }
     
    Logan Lee, Feb 21, 2013
    #1
    1. Advertising

  2. Logan Lee

    Paul N Guest

    You haven't asked a question, but I'll make one comment:


    On Feb 21, 6:33 am, Logan Lee <> wrote:
    > #include <iostream>
    > using namespace std;
    >
    > struct node {
    >        int data;
    >        node* next;
    >
    > };
    >
    > int main() {
    >     node* head;
    >     node* newnode;
    >     head=NULL;
    >
    >     for (int i=0; i<3; i++) {



    >         newnode=new node;
    >         newnode->data=i+1;
    >         newnode->next=head;
    >         head=newnode;


    Part of the idea behind C++ is that you have functions hidden in your
    classes to do this sort of messy stuff, so that the users of the class
    don't have to worry about it. The above four lines might then be
    reduced to something like:

    mylist.addnode(i+1);

    >     }
    >
    >     while (head!=NULL) {
    >           cout << head->data << endl;
    >           head=head->next;
    >     }
    >     cin.get();
    > }
     
    Paul N, Feb 21, 2013
    #2
    1. Advertising

  3. Logan Lee

    Jorgen Grahn Guest

    On Thu, 2013-02-21, Paul N wrote:
    > You haven't asked a question, but I'll make one comment:
    >
    >
    > On Feb 21, 6:33 am, Logan Lee <> wrote:
    >> #include <iostream>
    >> using namespace std;
    >>
    >> struct node {
    >>        int data;
    >>        node* next;
    >>
    >> };
    >>
    >> int main() {
    >>     node* head;
    >>     node* newnode;
    >>     head=NULL;
    >>
    >>     for (int i=0; i<3; i++) {

    >
    >
    >>         newnode=new node;
    >>         newnode->data=i+1;
    >>         newnode->next=head;
    >>         head=newnode;

    >
    > Part of the idea behind C++ is that you have functions hidden in your
    > classes to do this sort of messy stuff, so that the users of the class
    > don't have to worry about it. The above four lines might then be
    > reduced to something like:
    >
    > mylist.addnode(i+1);


    And then you address more and more weaknesses, until finally you end
    up with std::list. It would be interesting to follow such a
    transformation.

    To the original poster: your code is basically C code. It's fine to
    learn how to use the C part of C++ this way, but it's also important
    to understand that you almost never write code like this in C++. You
    use the standard containers -- sometimes std::list, but more often
    std::vector and some others. You yourself can concentrate on more
    interesting tasks.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 22, 2013
    #3
  4. On 2/20/2013 10:33 PM, Logan Lee wrote:
    > #include <iostream>
    > using namespace std;
    >
    > struct node {
    > int data;
    > node* next;
    > };
    >
    > int main() {
    > node* head;
    > node* newnode;
    > head=NULL;
    >
    > for (int i=0; i<3; i++) {
    > newnode=new node;
    > newnode->data=i+1;
    > newnode->next=head;
    > head=newnode;
    > }
    >
    > while (head!=NULL) {
    > cout << head->data << endl;
    > head=head->next;
    > }
    > cin.get();
    > }


    Memory leak.

    Your 'head' pointer is the only access path that leads to your linked
    list elements. And you wasted it in your output cycle.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 22, 2013
    #4
  5. On 2/22/2013 3:07 PM, Andrey Tarasevich wrote:
    > On 2/20/2013 10:33 PM, Logan Lee wrote:
    >> #include <iostream>
    >> using namespace std;
    >>
    >> struct node {
    >> int data;
    >> node* next;
    >> };
    >>
    >> int main() {
    >> node* head;
    >> node* newnode;
    >> head=NULL;
    >>
    >> for (int i=0; i<3; i++) {
    >> newnode=new node;
    >> newnode->data=i+1;
    >> newnode->next=head;
    >> head=newnode;
    >> }
    >>
    >> while (head!=NULL) {
    >> cout << head->data << endl;
    >> head=head->next;
    >> }
    >> cin.get();
    >> }

    >
    > Memory leak.
    >
    > Your 'head' pointer is the only access path that leads to your linked
    > list elements. And you wasted it in your output cycle.


    .... although technically you still have 'newnode'. But the general point
    still stands.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 22, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    503
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    494
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    531
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    695
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    789
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page