Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2008, 11:19 PM   #1
jason999x
Newbie
 
Join Date: Feb 2008
Posts: 6
Rep Power: 0 jason999x is on a distinguished road
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?

c++ Syntax (Toggle Plain Text)
  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...

c++ Syntax (Toggle Plain Text)
  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.
jason999x is offline   Reply With Quote
Old Feb 15th, 2008, 11:46 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
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:
c++ Syntax (Toggle Plain Text)
  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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Feb 15th, 2008, 11:52 PM   #3
jason999x
Newbie
 
Join Date: Feb 2008
Posts: 6
Rep Power: 0 jason999x is on a distinguished road
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?

Last edited by jason999x; Feb 16th, 2008 at 12:20 AM.
jason999x is offline   Reply With Quote
Old Feb 16th, 2008, 8:45 AM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
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).
c++ Syntax (Toggle Plain Text)
  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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Feb 16th, 2008, 11:00 AM   #5
jason999x
Newbie
 
Join Date: Feb 2008
Posts: 6
Rep Power: 0 jason999x is on a distinguished road
Re: one class to implement a linked list of objects of another class

Ok I'm gonna make a guess...

c++ Syntax (Toggle Plain Text)
  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,
c++ Syntax (Toggle Plain Text)
  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.
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  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?

Last edited by jason999x; Feb 16th, 2008 at 11:23 AM.
jason999x is offline   Reply With Quote
Old Feb 17th, 2008, 11:54 AM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
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:
c++ Syntax (Toggle Plain Text)
  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. }
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list problems bl00dninja C++ 6 Feb 17th, 2008 10:30 AM
Linked List Of Objects Ade C++ 10 May 14th, 2007 10:25 AM
singly-linked list templaste class in C++ w/ example driver bl00dninja Show Off Your Open Source Projects 0 Sep 11th, 2006 1:05 AM
Singly Linked List Help Firebar Java 3 May 22nd, 2005 10:56 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:10 PM.

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