CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Linked List Problems

    I''m new to c++ and I am having problems with linked lists. I feel as if I have a descent amount of the code correct, but my output as not the same as how it should be. I know I am not iterating through my list, but I can't really seem to figure out how to correct this. Here is the screenshot of the output its suppose to produce.

    Name:  Capture.JPG
Views: 763
Size:  32.9 KB

    this is my code so far.
    my LList.h

    Code:
        #ifndef LList_h
        #define LList_h
    
        #include <iostream>
        #include <cstddef>
        #include "node.h"
    
    
        class LList
        {
        public:
    	LList(void);			//constructor
    	LList(const LList &);	//copy constructor
    	~LList();			//destructor
    	void displayList();
    	LList *next;			//points to next node
    	void push_back(const string &str);
    	void push_front(const string &str);
    	friend ostream& operator<<(ostream& out, const LList& llist);
    	LList &operator=(const LList &l);		
    	
    
        private:
    	Node *_head;
    	Node *_tail;
    	LList *front;		//points to front of the list
    	string _str;
    	
        };
    
        inline LList::LList(void)
        {
    	cerr << "head = tail = 0 at 0024f8d0\n";
    
    	_head = 0;
    	_tail = 0;
    	front = 0;
    	
        }
    
    
    
        inline void LList::push_back(const string &_str)
        {
    	Node *p = new Node(_str);
    	if (_tail == 0)
    	{
    		_head = _tail = p;
    	}
    	else
    	{
    		_tail ->next(p);
    		_tail = p;
    	}
    	if (_head == 0)
    	{
    		_head = _tail = p;
    	}
    	else
    	{
    		_head ->next(p);
    		_head = p;
    	}
        }
    
        inline void LList::push_front(const string &_str)
        {
    	Node *p = new Node(_str);
    	if (_tail == 0)
    	{
    		_head = _tail = p;
    	}
    	else
    	{
    		_tail ->next(p);
    		_tail = p;
    	}
    	if (_head == 0)
    	{
    		_head = _tail = p;
    	}
    	else
    	{
    		_head ->next(p);
    		_head = p;
    	}
    
        }
    
        ostream &operator <<( ostream &out, const LList & llist )
        {
        for( LList *p = LList._head; p != 0; p = p -> next )
            out << p;
    
        return out;
        }
    
        LList & LList::operator=(const LList &l)
        {
        _head = 0;
    	_tail = 0;
    	
    	return *this;
        }
    
        inline LList::~LList()
        {
    	//delete _head;
    	//delete _tail;
     	
        }
    
    
        /*inline LList::~LList( )
        {
    	Node *p = new Node (_str);
    
    	if ( _head == 0)
    	{
    		_head = p;
    	}
    	else
    	{
    	Node *q;
    	
    		for (q = _head; q->next(); q = q -> next)
    	{
    		//loop until we have
    		//q pointing to the last node
    	}
    	q->next ( p);	//last node points to p
        }		//_head still points to the first node
    
        } */
    
        #endif
    and this is my main.cpp file

    Code:
    #include "LList.h"
    #include <iostream>
    
    using namespace std;
    
    
    
    int main( )
    {
        LList a;
    
        a.push_back(  "30" );
        a.push_front( "20" );
        a.push_back(  "40" );
        a.push_front( "10" );
        a.push_back(  "50" );
    
        cout << "list a:\n" << a << '\n';
    	
        return 0;
    
    }
    my errors and changes would only be made to my LList.h file. But what is happening is I only get the output to say list a: but my list is not there. any ideas?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Linked List Problems

    Quote Originally Posted by needhelp101 View Post
    I''m new to c++ and I am having problems with linked lists.
    1) Always plan on paper how a linked list works. Did you do that first? If you did that, there is little reason for the code not to work if you first write how a linked list works on paper by drawing boxes with links showing insertion, deletion, etc. and then write the code according to that plan.

    2) Did you debug your code using the debugger? I don't see any indication of that (you wrote code, you ran the code, but no mention of stepping through the code and debugging it yourself). If you debugged your code, where does your linked list implementation fail to not follow your plan from step 1) above?

    3) Why did you comment out the destructor? The linked list class is faulty if it doesn't clean up the memory.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Linked List Problems

    I ran my code, and it runs fine. But when I run my code, my output doesn't look like the screenshot output. My output just displays head = tail = 0 = '#', list a:

    I commented out the destructor because as soon as I ran my code, the program crashed.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Linked List Problems

    Quote Originally Posted by needhelp101 View Post
    I ran my code, and it runs fine. But when I run my code, my output doesn't look like the screenshot output.
    You know those are contradictory statements, right?

    As Paul asked, did you run the debugger to see what's happening? That's what any of us would do, but you didn't include all your code, so we can't.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Linked List Problems

    Er.. no!

    Code:
    inline LList::LList(void)
        {
    	cerr << "head = tail = 0 at 0024f8d0\n";
    
    	_head = 0;
    	_tail = 0;
    	front = 0;
    	
        }
    In your screen shot it shows the actual contents of head and tail and the actual memory address. On your system the memory address to show will almost certainly be different from that shown on the screen shot!

    I commented out the destructor because as soon as I ran my code, the program crashed.
    and doesn't that tell you something??? so why does your program crash? It's no good just commenting out some code because it causes problems - you MUST understand why this causes the problem by using the debugger and then fix it!!!

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Linked List Problems

    Quote Originally Posted by needhelp101 View Post
    I commented out the destructor because as soon as I ran my code, the program crashed.
    Well, the destructor crashing is a symptom of the other issues with your code that you need to fix.

    Other issues:
    Code:
        LList & LList::operator=(const LList &l)
        {
        _head = 0;
    	_tail = 0;
    	
    	return *this;
        }
    How does the code above set one linked list equal to another linked list? It is a bogus assignment operator, and bogus assignment operators should not be coded, since it affects the overall running of any code using LList.

    You need to code a proper assignment statement, one that actually makes one linked list equal to the other linked list.
    Code:
    inline LList::~LList( )
    {
      node *p = new Node (_str);
    Why are you allocating a new node in the destructor? You're supposed to point to the head node, and just traverse each node removing each one.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured