Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   one class to implement a linked list of objects of another class (http://www.programmingforums.org/showthread.php?t=15195)

jason999x Feb 15th, 2008 11:19 PM

one class to implement a linked list of objects of another class
 
say you're managing a stock portfolio... of class Portfolio, and it is to contain a linked list of several of class Stock... or more like... Portfolio is supposed to contain a Node structure to contain each Stock and the pointer to the head list... is this how it is done?

:

  1. class Portfolio
  2. {
  3.   struct Node
  4.   {
  5.       Stock whateverstock;
  6.       Stock* head;
  7.   }
  8. }


Does this node actually contain a full Stock object instance? Or does a node within the Portfolio class only contain the pointer? There was some template for doing things like this... but I forget how that's done... let me attempt a more generic example...

:

  1. class Whatever
  2. {
  3.   struct Node
  4.   {
  5.       SomeOtherObject  thatObject;
  6.       SomeOtherObject* headptr;
  7.   }
  8. }


Note that NO inheritance is used here. Portfolio does not inherit anything from Stock or vice versa.

Jessehk Feb 15th, 2008 11:46 PM

Re: one class to implement a linked list of objects of another class
 
The general form of a linked list would probably be something like this:
:

  1. template <typename T>
  2. class LinkedList {
  3.     class Node {
  4.         T data;
  5.         Node *next;
  6.     };
  7.  
  8.     Node *head;
  9. public:
  10.     // etc
  11. };


Note that a node just has data and a pointer to the next node, while the list has only a pointer to the first node in the list.

jason999x Feb 15th, 2008 11:52 PM

Re: one class to implement a linked list of objects of another class
 
Thank you for your response... but to be more clear I will say two things...
1. We haven't started on templates yet...
2. Here is the site that explains what I need to implement... I'm not clear on what it's asking. http://people.eecs.ku.edu/~jvalland/...structions.pdf
It's like real life... you have a portfolio that has a list of stocks in it... I'm just trying to make sure we're all clear on this. But thanks for your response.

But I will try to interpret your response...
"class LinkedList" == Portfolio?
"T data" == Stock, as T can be any type? is this right?

oh and instead of "class Node", the assignment actually specified a structure... I guess that would be a little simpler than calling it "class Node".

In fact... this assignment is so confusing I really have no clue whatsoever he wants... it gives an example input file of stocks and whether to buy or sell them... but this whole transaction class seems so unnecessary... couldn't you just buy and sell each individual stock, maintain a portfolio of what stocks have been touched, and sum up the money buy/sell totals? I guess the transaction list is just something to keep track of the buy and sell actions?

Jessehk Feb 16th, 2008 8:45 AM

Re: one class to implement a linked list of objects of another class
 
I quickly read the assignment and it appears he wants you to read in stock data into a home-made linked-list and do operations on them based on some functions that you've been given the declarations for. It also appears that each Stock should carry it's own linked-list of transactions. So you have a linked-list of linked-lists.

Quote:

But I will try to interpret your response...
"class LinkedList" == Portfolio?
"T data" == Stock, as T can be any type? is this right?
Yup. That means you probably want something like this (this is just a rough outline, the tutorial might be more specific about the structure you need).
:

  1. class Transaction {
  2.     // etc
  3. };
  4.  
  5. class Portfolio {
  6.     class Stock {
  7.         Transaction *head;
  8.         // etc
  9.     };
  10.  
  11.     Stock *head;
  12. public:
  13.     // etc
  14. };


There's a great tutorial by Narue (a moderator here) on linked lists here.

I don't claim to know why the assignment requirements are what they are -- I just know how linked lists work. ;)

jason999x Feb 16th, 2008 11:00 AM

Re: one class to implement a linked list of objects of another class
 
Ok I'm gonna make a guess...

:

  1. class Portfolio
  2. {
  3.     // other things
  4.     struct StockListNode
  5.     {
  6.         Stock item;
  7.         StockListNode *next;
  8.     };
  9.     StockListNode* head;
  10. }
  11.  
  12. class Stock
  13. {
  14.     // ticker symbol, etc.
  15.   struct TransactionListNode
  16.   {
  17.         Transaction item;
  18.         TransactionListNode *next;
  19.   };
  20.   TransactionListNode* head;
  21. }
  22.  
  23. class Transaction
  24. {
  25. }


something like that? The assignment does say to use "structures"... I don't think I would define the class within a class as you seem to try to do...


Okay he finally made something kind of clear... he may ask us to do this differently from the others... but here's what he says on http://people.eecs.ku.edu/~jvalland/...ab2_notes2.pdf
Quote:

4.1 Definition of linked lists
The linked lists we are writing for this lab will be composed of two parts. First, the class/struct that will
define the linked list node: for example,
:

  1. struct Node
  2. {
  3. string value;
  4. Node *next;
  5. };

And second, a pointer to the first node in the linked list, initially it should be null.
:

  1. Node *head = NULL;


This follows like how I thought, just with the addition of the null pointer for the head.


But I have some other question here... why does one use the address of an i/o stream instead of the iostream itself in the parameter? I'll show you his prototypes for two functions, buy and save, in the Portfolio class
:

  1. void Buy(std::ifstream& in);
  2. void Sell(std::ifstream& in);

I figure just ifstream minus the &... but w/e.
The way we have been used to doing file i/o is doing it on the main.cpp, and then having other functions use that input data... all the instream variables were defined in main, usually. We (as in EECS at KU students) haven't yet dealt with i/o stream in parameters before... is the & convention?
Actually what I should really be asking is will (ifstream&) vs. (ifstream) change how I would code the Buy and Sell functions dramatically?

Jessehk Feb 17th, 2008 11:54 AM

Re: one class to implement a linked list of objects of another class
 
The & refers to a reference, which is a way of passing parameters to things such as functions.
I won't bother explaining references fully, as Narue does that nicely here.

By writing a function that takes a std::ifstream object, you can specify the source of the input.

For example:
:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. void readNLines( int n, std::ifstream &in ) {
  6.     std::string line;
  7.  
  8.     for ( int x = 0; x < n; x++ ) {
  9.         std::getline( in, line );
  10.  
  11.         std::cout << x + 1 << ": " << line << std::endl;
  12.     }
  13. }
  14.  
  15. int main() {
  16.     // Make sure in real code, you check for success or failure
  17.     // when opening files.
  18.  
  19.     // Open this file for reading
  20.     std::ifstream input( "example.cpp" );
  21.     readNLines( 3, input );
  22. }



All times are GMT -5. The time now is 7:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC