Linked List Problems

This is a discussion on Linked List Problems within the C++ Programming forums, part of the General Programming Boards category; Your still not checking for memory errors when you use 'new'. Code: if (remainingMemory > MAX_MEMORY) if Remaining_Memory is Greater ...

  1. #16
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Your still not checking for memory errors when you use 'new'.
    Code:
    if (remainingMemory > MAX_MEMORY)
    if Remaining_Memory is Greater Than Max_Memory then return TRUE.
    Is that right?

    BobCatPod::addSong() will only add a songnode to the head of the list.
    It will not append a song to the list. so if you call it twice then you will have a memory leak.

    Is there any way you can use std::list<>. Then all you would have to do is work with the songs. all the list management will be handled for you.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I don't know how does std::list<> work?

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    here is the assignment http://www.cs.txstate.edu/~ch04/webt.../f08/p7f08.pdf and we were given the driver. the song.h and song.cpp were our last assignment and I got that one right...I just don't understand how to implement these lists
    I really appreciate your help

  4. #19
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by cannsyl View Post
    I don't know how does std::list<> work?
    its easy.

    http://www.cprogramming.com/tutorial/stl/stllist.html
    You will need to remove some stuff from your BobCatPod class.
    like *head
    struct SongNode
    bool addSong (Song s);
    int removeSong (Song s);

    the std::list<> will handle this for you
    Just remember that with a std::list you will use an Iterator to access the data stored in the list. Read the tutorial. it will explain it all
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    so this sounds great, it allows me to add a song, but how does it fit in
    Code:
    bool BobCatPod::addSong(Song t)
    {
          if (isfull ())
         {
                    return false;
         }
         SongNode *newSong = new SongNode;     //a new node
         
        
         
         // Allocate a new node and stor num there.
         
         newSong->s = t; 
         head = newSong;
         newSong->next = NULL;
         
        
         return true;
         usedMemory++;
    }
    I mean I would do
    Code:
    std::list<Song> s;
    then
    Code:
    BobCatPod::BobCatPod()
    
         int totalMemory = 0;
         int usedMemory = 0;
         totalMemory - usedMemory = remainingMemory;
    
    bool BobCatPod::isfull ()
    {
         if (remainingMemory > MAX_MEMORY)
         {
                    return true;
         }
    
    }
    then would I
    Code:
    bool BobCatPod::addSong(Song t)
    {
          if (isfull ())
         {
                    return false;
         }
    s.push_front(t);
         
        
         return true;
         usedMemory++;
    }
    ?? I am not in data structures yet this is only my second c++ class.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    I have not yet taken a good look at this whole thread, but at a glance one advantage of Nor's suggestion is that eliminates the need to perform dynamic memory allocation yourself. The std::list doubly linked list handles that for you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    // ...I got that one right...I just don't understand how to implement these lists

    Wasn't it Stroustrup who spoke of blowing off ones own leg, if careless? Start by looking up tutorials/implementations of linked lists. Post your progress/regress here. And be specific.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    yeah it looks like it could be a solution, but we haven't gotten that far and our professor insists that we use different files for our functions and it is really confusing me...I read the tutorial...Im just not sure how to implement it with the .h and .cpp?

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    ok it doesn't like
    Code:
    std::list<Song> s;
    But Song is my class and s is an instance of Song so why doesn't this work?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by cannsyl
    ok it doesn't like
    Clearly, the compiler just hates you. You are doomed and should commit ritual suicide immediately

    Quote Originally Posted by cannsyl
    But Song is my class and s is an instance of Song so why doesn't this work?
    Morbid jokes aside, you should always clearly state what is the error message. Just saying "it doesn't like <such and such>" is bad practice when asking for help.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> ok it doesn't like

    Why wouldn't it? It's valid C++, right?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  12. #27
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    it says expected constructor, destructor, or type conversion before '<' token at std::list<Song> s;
    I think my compiler does hate me, but suicide won't help me finish this assignment

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by cannsyl
    it says expected constructor, destructor, or type conversion before '<' token at std::list<Song> s;
    Check that you have included the <list> standard header.

    Quote Originally Posted by cannsyl
    I think my compiler does hate me, but suicide won't help me finish this assignment
    Heh. More likely, your compiler just hates the code that you give it to compile.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    you mean this
    Code:
    //BobCatPod.cpp
    #include "song.h"
    #include "BobCatPod.h"
    
    std::list<Song> s;
    i included the header files for the class Song for which I need the list
    do I need to add std::list<> to my header file? somehow?

  15. #30
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    This should get you started. I've started using the list for you. all you need to do is fix the logic errors and fill in the blanks.
    Code:
    //bobcatpod.h
    #ifndef BOBCATPOD_H
    #define BOBCATPOD_H
    #include "song.h"
    /*ADD*/ 
    #include <list>
    
    class BobCatPod
    {
    	private:
    /*REMOVE*/
    	//struct SongNode
    	//{
    	//	Song s;                  //value in this node
    	//	SongNode *next;      //To point to the next node
    	//};        
    	//SongNode *head;
    /*ADD*/
    	std::list<Song> SongList;
    	std::list<Song>::iterator Iter;
    	public:
    	//Constructor
    	BobCatPod();
    	//Destructor
    	~BobCatPod();
    
    	//Linked list operations
    /*REMOVE*/
    	//void appendNode (Song s);
    	int addSong (Song s);
    	int removeSong (Song s);
    	bool isfull ();
    	void showSongList ();
    	int getTotalMemory();
    	int getRemainingMemory();
    	friend ostream& operator<<(ostream & os, Song & s);
    };
    #endif
    Code:
    //BobCatPod.cpp
    #include "song.h"
    #include "BobCatPod.h"		//<<<<<< forgot this
    BobCatPod::BobCatPod() //Moved here from line 24 of the header file
    { 
    }
    
    BobCatPod::~BobCatPod()
    {
    	SongList.clear();
    }
    
    /*REMOVE*/
    //void BobCatPod::appendNode (Song num){}
    int BobCatPod::addSong(Song s){
    	if( this->isfull() )
    		return 0;
    	SongList.push_back(s);
    }
    
    int BobCatPod::removeSong(Song s){
    
    	return 0;
    }
    bool BobCatPod::isfull ()
    {	 //logic errer here
         //if (remainingMemory > MAX_MEMORY)
         //{
         //           return true;
         //}
    
    	return false;
    }
    
    int BobCatPod::getTotalMemory()
    {
        //int totalMemory;	//???? you have not assigined anything to these.
        //int usedMemory;
        //totalMemory - usedMemory = remainingMemory
        //return remainingMemory;
    
    	//Undeclared Identifers. 
    	return 0;
    }
    
    int BobCatPod::getRemainingMemory(){
    	//this should be what you have above here.
    	return 0;
    }
    
    void BobCatPod::showSongList(){
    	//I'm guessing this will be a console application.
    	Iter = SongList.begin();
    	while(Iter != SongList.end())
    	{
    	//TODO Write Display Code.
    		std::cout << Iter->getArtist() << " "
    			      << Iter->getTitle()  << std::endl;
    		//Move to the next object in your list.
    		Iter++;
    	}
    }
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 02:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21