Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   linked list (http://www.velocityreviews.com/forums/t957866-linked-list.html)

Logan Lee 02-21-2013 06:33 AM

linked list
 
#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();

}

Paul N 02-21-2013 09:45 PM

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


On Feb 21, 6:33*am, Logan Lee <guniti...@gmail.com> 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();
> }



Jorgen Grahn 02-22-2013 02:22 PM

Re: linked list
 
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 <guniti...@gmail.com> 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 .

Andrey Tarasevich 02-22-2013 11:07 PM

Re: linked list
 
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 02-22-2013 11:09 PM

Re: linked list
 
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




All times are GMT. The time now is 07:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.