Thread: Help with a linked list

  1. #1
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287

    Help with a linked list

    Here are the compiler errors:
    Code:
    1>------ Build started: Project: Linked List 2, Configuration: Debug Win32 ------
    1>  Linked List 2.cpp
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>          c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(61) : see reference to class template instantiation 'list<NODETYPE>' being compiled
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(47): warning C4183: 'LIST': missing return type; assumed to be a member function returning 'int'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(48): error C2523: 'list<NODETYPE>::~LIST' : destructor tag mismatch
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(67): error C2039: '{ctor}' : is not a member of 'List<NODETYPE>'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(90): error C2039: '{dtor}' : is not a member of 'List<NODETYPE>'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(104): error C2039: 'insertAtFront' : is not a member of 'List<NODETYPE>'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(118): error C2039: 'insertAtBack' : is not a member of 'List<NODETYPE>'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(121): error C2065: 'NODETYPLE' : undeclared identifier
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(121): error C2065: 'value' : undeclared identifier
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(138): error C2039: 'removeFromFront' : is not a member of 'List<NODETYPE>'
    1>c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(284): fatal error C1075: end of file found before the left brace '{' at 'c:\users\owner\documents\visual studio 2010\projects\linked list 2\linked list 2\linked list 2.cpp(142)' was matched
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Code:
    #include "stdafx.h"
    #ifndef LISTNODE_H
    #define LISTNODE_H
    
    #include <iostream>
    using std::cout;
    
    template< typename NODETYPE > class List;
    
    template< typename NODETYPE>
    
    class ListNode
    {
    	friend class List< NODETYPE>;
    
    public:
    	ListNode( const NODETYPE & );
    	NODETYPE getData() const;
    private:
    	NODETYPE data;
    	ListNode< NODETYPE > *nextPtr;
    };
    
    template< typename NODETYPE>
    ListNode< NODETYPE >::ListNode( const NODETYPE &info )
    	: data( info ), nextPtr( 0 )
    {
    
    }
    
    template< typename NODETYPE >
    NODETYPE ListNode< NODETYPE >::getData() const
    {
    	return data;
    }
    
    #endif
    
    template< typename NODETYPE >
    class list
    {
    public:
    	LIST();
    	~LIST();
    	void insertAtFront( const NODETYPE & );
    	void insertATBack( const NODETYPE & );
    	bool removeFromFront( NODETYPE & );
    	bool removeFromBack( NODETYPE & );
    	bool isEmpty() const;
    	void print() const;
    
    private:
    	ListNode< NODETYPE > *firstPtr;
    	ListNode< NODETYPE > *lastPtr;
    
    	ListNode< NODETYPE > *getNewNode( const NODETYPE & );
    };
    
    template< typename NODETYPE >
    List< NODETYPE >::List()
    	: firstPtr( 0 ), last Ptr( 0 )
    {
    }
    
    template< typename NODETYPE >
    List< NODETYPE >::~List()
    {
    	if ( !isEmpty() )
    	{
    		cout << "Destroying nodes ...\n";
    
    		ListNode< NODETYPE  > *currentPtr = firstPtr;
    		ListNode< NODETYPE > *tempPtr;
    
    		while ( CurrentPtr != 0 )
    		{
    			tempPtr = currentPtr;
    			cout << tempPtr->data << '\n';
    			currentPtr = currentPtr->nextPtr;
    			delete tempPtr;
    		}
    	}
    
    cout << "All nodes destroyed\n\n";
    
    }
    
    template< typename NODETYPE >
    void List< NODETYPE >::insertAtFront ( const NODETYPE &value )
    {
    	ListNode< NODETYPE > *newPtr = getNewNode( value );
    
    	if ( isEmpty() )
    		firstPtr = lastPtr = newPtr;
    	else
    	{
    		newPtr->nextPtr = firstPtr;
    		firstPtr = newPtr;
    	}
    }
    
    template< typename NODETYPE >
    void List< NODETYPE >::insertAtBack( const NODETYPE &VALUE)
    {
    	ListNode< NODETYPE > *newPtr = getNewNode( value );
    
    	if( isEmpty() )
    		firstPtr = lastPtr = newPtr;
    	else
    	{
    		lastPtr->nextPtr = newPtr;
    		lastPtr = newPtr;
    	}
    }
    
    template< typename NODETYPE >
    bool List< NODETYPE >::removeFromFront( NODETYPLE &value )
    {
    	if ( isEmpty() )
    		return false;
    	else
    	{
    		ListNOde< NODETYPE > *tempPtr = firstPtr;
    
    		if ( firstPtr == lastPtr )
    			firstPtr = lastPtr = 0;
    		else
    			firstPtr = firstPtr->nextPtr;
    
    		value = tempPtr->data;
    		delete tempPtr;
    		return true;
    	}
    }
    
    template< typename NODETYPE >
    bool List< NODETYPE >::removeFromBack( NODETYPE &value )
    {
    	if ( isEmpty() )
    		return false;
    	else
    	{
    		ListNode< NODETYPE > *tempPtr = lastPtr;
    
    		if(firstPtr == lastPtr )
    			firstPtr = lastPtr = 0;
    		else
    		{
    			ListNOde< NODETYPE > *tempPtr = lastPTr;
    
    			if ( firstPtr == LastPtr )
    				firstPtr = lastPtr = 0;
    			else
    			{
    				ListNode< NODETYPE > *currentPtr = firstPtr;
    
    				while ( currentPtr->nextPtr != lastPtr )
    					currentPtr = currentPtr->nextPtr;
    
    				lastPtr = currentPtr;
    				currentPtr->nextPtr = 0;
    			}
    
    			value = tempPtr->data;
    			delete tempPtr;
    			return true;
    		}
    	}
    
    	template< typename NODETYPE >
    	bool List< NODETYPE >::isEmpty() const
    	{
    		return firstPtr == 0;
    	}
    
    	template< typename NODETYPE >
    	ListNode< NODETYPLE > *lIST< nodetype >::getNewNode( const NODETYPE &value )
    	{
    		return new ListNOde< NODETYPE >(value );
    	}
    
    	template< typename NODETYPE >
    	void List< NODETYPE >::print() const
    	{
    		if ( isEmpty() )
    		{
    			cout << "The list is empty\n\n";
    			return;
    		}
    
    		ListNode< NODETYPE > *currentPtr = firstPtr;
    
    		cout << "The list is: ";
    
    		while ( currentPtr != 0 )
    		{
    			cout << currentPtr->data << ' ' ;
    			currentPtr = currentPtr->nextPtr;
    		}
    
    		cout << "\n\n";
    	}
    
    //#endif
    
    
    
    
    template< typename T >
    void testList( List < T > &listObject, const string &typeName )
    {
    	cout << "Testing a List of " << typeName << " values\n";
    	instructions();
    
    	int choice;
    	T value;
    
    	do
    	{
    		cout << "? ";
    		cin >> choice;
    
    		switch(choice)
    		{
    		case 1:
    			cout << "Enter " << typeName << ": ";
    			cin >> value;
    			listObject.insertAtFront( value );
    			listObject.print();
    			break;
    		case 2:
    			cout << "Enter " << typeName << ": ";
    			cin >> value;
    			listObject.insertAtBack( value );
    			listObject.print();
    			break;
    		case 3:
    			if ( listObject.removeFromFront( value ) )
    				cout << value << " removed from list\n";
    
    			listObject.print();
    			break;
    		case 4:
    			if ( listObject.removeFromBack( value ) )
    				cout << value << " removed from list\n";
    
    			listObject.print();
    			break;
    		}
    
    	} while (choice != 5 );
    
    	cout << "End list test\n\n";
    }
    
    void instructions()
    {
    	cout << "Enter one of the following:\n"
    		<< " 1 to insert at beginning of list\n"
    		<< " 2 to insert at end of list\n"
    		<< " 3 to delete from beginning of list\n"
    		<< " 4 to delete from end of list\n"
    		<< " 5 to end list processing\n";
    }
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	List< int > integerList;
    	testList( integerList, "integer" );
    
    	List< double > doubleList;
    	testList( doubleList, "double" );
    
    	return 0;
    }
    Anyone able to give me a few pointers?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Terrance
    linked list 2.cpp(47): warning C4183: 'LIST': missing return type; assumed to be a member function returning 'int'
    linked list 2.cpp(48): error C2523: 'list<NODETYPE>::~LIST' : destructor tag mismatch
    This is a simple series of typo errors: instead of class list you meant to write class List and instead of LIST you meant to write List.

    You have another typo error here:
    Code:
    template< typename NODETYPE >
    List< NODETYPE >::List()
        : firstPtr( 0 ), last Ptr( 0 )
    {
    }
    last Ptr should have been lastPtr. By the way, beginning with C++11, we prefer to write nullptr instead of 0 or NULL when making pointers be null pointers.

    You have another typo error here:
    Code:
    while ( CurrentPtr != 0 )
    CurrentPtr should have been currentPtr.

    I think I will stop here. Instead of writing the whole program and then compiling, you should have written piece by piece, compiling step by step and fixing these rather obvious typo errors as you go. As it is, you got a lump of error messages and freaked out when actually if you just calmly went through them one by one, you clearly have the skill to fix them yourself.

    EDIT:
    Okay, most of your mistakes really are trivial typo errors; I won't go through any more of them as they are really boring and dreadfully trivial to fix. Other mistakes to note:
    • You have a missing closing brace for removeFromBack. Actually, your IDE was nice enough to hint it to you by auto indenting the code correctly according to the missing brace, but incorrectly according to what we would expect if the brace was there.
    • You forgot to #include <string>, and then to use it as the unqualified string you need the using directive using namespace std or the using declaration using std::string. By the way, using directives and using declarations should not appear at namespace scope in a header file or before a header file is included. What this means is that your using std::cout; near the top is wrong because normally the entire List and ListNode function template implementations would be moved to a header file: you should move the using declaration to just before the testList function template, and likewise that is where you would place the using declaration for std::string. This does mean that your use of cout nearer the top must either be qualified with std:: or you need to place using declarations at function scope rather than namespace scope.
    • You forgot to forward declare the instructions function before the testList function template. Actually, it would be easier just to move the definition of instructions before that of testList.
    • You forgot the using declaration of std::cin
    • In removeFromBack, you declared tempPtr once, then shadowed it with a more local declaration of tempPtr without having used the previous one.
    • In removeFromBack, if firstPtr == lastPtr, you don't return a value even though removeFromBack is declared as returning a bool.
    Last edited by laserlight; 10-29-2016 at 08:36 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Great, I was up early this morning working on it. I got it to compile and work correctly! Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  2. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread