Solved

Linked list of what?

Posted on 2000-02-17
12
217 Views
Last Modified: 2013-12-14
I want to build a double,branched, linked list.
Now what should I use ?
A linked list of structures or linked of
objects(I mean classes), Or a linked list inside an object (whole linked list as a member of object)
I want some sugggestions on memory point of view also.
0
Comment
Question by:sumant032199
  • 6
  • 5
12 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 90 total points
ID: 2531767
>> A linked list of structures or linked of
>> objects(I mean classes),
Yes.

In C++ structures and classes are the same.  

I suppose what you mean is should the structures/classes have member functions for maintaining the list.  That is up to you and depends on your needs.  Like if this code needs to compatible with C, then no.  However, most likely you would be best of using member functions.

I guess I would say that if there is no good reason to not use member functions, then you should.

>> I want some sugggestions on
>> memory point of view also.
What does that even mean?
0
 
LVL 1

Expert Comment

by:ScottyDawg
ID: 2531771
When it comes to "struct" and "class" in c++ there isn't a great deal of difference apart from everything is by default public: in a struct and in a class its private:

Can you not make use of STL (Standard Template Library). It will no doubt have an implementation of a bi-directional linked list that you could use with very little effort.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2531810
the STL has the list<> template class whose interface functions in a way that strongly suggests that it be implimented as a doubley linked list.  However, how it is actually implimented is intirely up the the STL implimenter.  (Although I can't image anyone not using a linked list for it.)
0
 
LVL 3

Author Comment

by:sumant032199
ID: 2536208
One advantage I see in using objects as nodes of linked list is that I can perform various operations on nodes using member functions. And I can hide data. But in some popular books they have produced whole linked list as a member of only one object.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2536259
>> One advantage I see in using objects as
>> nodes of linked list is that I can perform
>> various operations on
>> nodes using member functions
Yes, definitely.  

But it may depend on your needs.  If yo have ot maintain C compatiblity, you can't do that.  If the objects in the linked list are to be of claeses that were already defined and connot be changed (like from a commercial library) you can't do this.

>>  But in some popular books
>> they have produced
>> whole linked list as a member
>> of only one object
What does that mean?  
0
 
LVL 3

Author Comment

by:sumant032199
ID: 2537350
Books.

struct list
{
      struct list *prev;
      .      
      .      
      struct list *next;
};
class linked_list
{
   private:        
          struct list *head;
   public:
                .
                .
                .
   
}LIST;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 2538006
I'm not sure what you mean by "whole linked list as a member of only one object"

How can it be a member?  How can anythjng be a member of only one object?  That makes no sense.

What is that weird code?  I would hope you wouldn't find that in any book.  That is a terrible mix of OLD style C stuff and C++ stuff
0
 
LVL 3

Author Comment

by:sumant032199
ID: 2538491
What I mean is since, head is a pointer to linked list starting and it is also private hence only member function can access it hence therotically whole linked list is hidden. If one can get the value of head(address of first node) then whole linked list will be "open".

0
 
LVL 22

Expert Comment

by:nietod
ID: 2538529
Yes, that is true.  But now I'm not sure what the question is.
0
 
LVL 3

Author Comment

by:sumant032199
ID: 2539484
I think my this question is somewhat weird. I am asking obvious things. I will be more prepared in my next question. Anyway thnaks a lot.

At last, one request(out of discussion).
Will you nietod tell me various uses/applications of typedef? I know only which are in C. Just give me idea where it can be used; like pointer to a member function. If you wish, just brief them or give some syntax.
0
 
LVL 3

Author Comment

by:sumant032199
ID: 2539491
I think my this question is somewhat weird. I am asking obvious things. I will be more prepared in my next question. Anyway thnaks a lot.

At last, one request(out of discussion).
Will you nietod tell me various uses/applications of typedef? I know only which are in C. Just give me idea where it can be used; like pointer to a member function. If you wish, just brief them or give some syntax.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2539636
Traditionally in C a struct was not a "complete type" the name of the struct had to be preceeded by a "struct" keyword to serve as a data type.    In C++ that is not the case (and I blieve that is not the case in C anymore.)  so

struct SomeStructType
{
   int x;
};

SomeStructType Str;

See?  no need to declare a typedef because SomeStructType is a type.  Same is true for classes of course.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now