Linked Lists: creating a find and insertSort RECURSIVELY, and other things

Discussion in 'C' started by jason999x, Feb 16, 2008.

  1. jason999x

    jason999x New Member

    Joined:
    Feb 16, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have been able to find things on how to do these things iteratively, but I need to know how to do it recursively... couldn't find any such thing.

    prototype from .h:
    Code:
    Item* FindR(string name, ListNode* node);
    Item* InsertSortR(string name, ListNode* &node);
    
    prototype of Portfolio.h based on this project I'm doing
    Code:
    class Portfolio
    {
        private:
           // there may or may not be private members, not sure yet
           // unless this list of stocks counts
           struct StockListNode
           {
              Stock item;
              StockListNode *next;
           };
           StockListNode* head;
    
       public:
           void Buy(ifstream& in);
           void Sell(ifstream& in);
        
           Stock* FindR(string name, StockListNode* node);
           Stock* InsertSortR(string name, StockListNode* &node);
           void ReportR(StockListNode *node);
    }
    
    I will also show all the code I have written so far on this. There are a total of three classes: Portfolio, Stock, and Transaction. The Portfolio contains a linked list of Stocks. Each Stock contains a linked list of all the Transactions on it. Note that there are also two kinds of transaction methods on Stock class... I don't get why there needs to be a recursive method there too (in other words, why two add transaction functions)?

    Code:
    // Portfolio.h
    
    #ifndef "Portfolio.h"
    #define	"Portfolio.h"
    
    using namespace std;
    
    class Portfolio
    {
    	public:
    
    		Portfolio();
    		~Portfolio();
    
    		void Buy(ifstream& in);
    		void Sell(ifstream& in);
    		Stock* FindR(string name, StockListNode* node);
    		Stock* InsertSortR(string name, StockListNode* &node);
    		void ReportR(StockListNode *node);
    
    	// don't know any of this part yet...
    	// private:
    	//	struct StockListNode;
    }
    
    Code:
    // Portfolio.cpp
    
    #include "Portfolio.h"
    
    using namespace std;
    
    Portfolio::Portfolio()
    {
    	// blank constructor
    }
    
    Portfolio::~Portfolio()
    {
    	// now what do I delete here?
    	// in this destructor?
    	// my guess: delete *StockListNode;
    	// probably some variant of that
    }
    
    void Portfolio::Buy(ifstream& in)
    {
    	// question for myself: why the hell do we use
    	// "ifstream& in" and not "ifstream in"...?
    	// seriously what the hell is the point of that?
    
    	// uses FindR - find the stock in the first place
    
    	// uses InsertSortR - insert a new stock with
    	// whatever information if FindR fails
    	// (no such stock is already in list?)
    
    	// FindR(stock);
    	// if !FindR(stock)
    	// {
    	//	InsertSortR(newstock);
    	// }
    	// stock->tickerSymbol = tickerSymbol;
    	// stock->price = price;
    	// // etc....
    }
    
    /*
    okay NOW i get how this mechanism works...
    buy: 	if stock not there, create it
    	if stock there, buy MORE OF IT
    sell:	if stock there, sell it
    	if stock not there, error
    */
    
    void Portfolio::Sell(ifstream& in)
    {
    	// FindR(stock);
    	// if !FindR(stock)
    	// {
    	//	cerr << "Stock not found."
    	// }
    }
    
    Stock* FindR(string name, StockListNode* node)
    {
    	// returns a POINTER to a Stock,
    	
    	// this is supposed to be RECURSIVE
    	// Mean Kong supplies an ITERATIVE
    	// version of this function, but it's up to
    	// me to make a recursive version of this
    
    	// question for myself: how do I implement
    	// this recursively?
    }
    
    Stock* InsertSortR(string name, StockListNode* &node)
    {
    	// question for myself: how do I implement
    	// this recursively?
    }
    
    void ReportR(StockListNode *node)
    {
    	// this involves the recursive
    	// "transverse a list"
    }
    
    // so there a total of three recursive things
    // to implement in this program... four
    // if you count the separate transverse
    // method in the later Transaction class
    
    
    Code:
    // Stock.h
    
    class Stock
    {
    	public:
    		void addTransaction(Transaction *transaction);
    		void addTransactionR(Transaction *transaction, TransactionListNode* &node);
    
    	private:
    		string tickerSymbol;
    		string date;
    		// int shares;
          		// does int shares go here?
    		// or in "Transaction"?
    		double currentPrice;
    
    	/* possible here...
    		struct TransactionListNode
    		{
    			Transaction whatever;
    			Transaction *next;
    		}
    	*/
    }
    
    Code:
    // Stock.cpp
    
    #include "Stock.h"
    
    using namespace std;
    
    Stock::Stock()
    {
    }
    
    Stock::~Stock()
    {
    }
    
    void Stock::addTransaction(Transaction *transaction)
    {
    }
    
    void Stock::addTransactionR(Transaction *transaction, TransactionListNode* &node)
    {
    }
    
    Code:
    // Transaction.h
    
    #ifndef "Transaction.h"
    #define "Transaction.h"
    
    class Transaction
    {
    	public:
    
    }
    
    Code:
    // Transaction.cpp
    
    #include "Transaction.h"
    
    using namespace std;
    
    
    Code:
    // main.cpp
    
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    // seriously, I am doing it this way...
    // that way we all know that every file in this program
    // uses namespace std
    
    int main(int argc, char* argv[])
    {
    	// open file, I guess here...
    	// filename = argv[1]
    	// if argc < 2, print out:
    	// Usage: main <inputfile>
    
    	return 0;
    }
    
    /* 
    	question to ask:
       	does the file handling still go on here in main?
    */
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice