Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > linked list

Reply
Thread Tools

linked list

 
 
Logan Lee
Guest
Posts: n/a
 
      02-21-2013
#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();

}
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      02-21-2013
You haven't asked a question, but I'll make one comment:


On Feb 21, 6:33*am, Logan Lee <(E-Mail Removed)> 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();
> }


 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-22-2013
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 <(E-Mail Removed)> 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 .
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-22-2013
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

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-22-2013
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments