Linked List

This is a discussion on Linked List within the C++ Programming forums, part of the General Programming Boards category; Hello all, I have almost completed a program that will take 8 numbers from a user and store it in ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Linked List

    Hello all, I have almost completed a program that will take 8 numbers from a user and store it in a linked list. Once that is done It will ask for another entry and determine the location of this new entry and how many items are currently bigger than the new entry. I have (pretty much") 95% of it written out but am having problems with my header file (below). I just have to modify my main.cpp file to complete the program but am having problems in the header where I'm getting errors in the codeLL function. I cannot figure out why it keeps giving off this error. I thought I had written everything correctly. Please let me know if you have any suggestions or if you see something that Im just not seeing

    Thanks in advance

    --------------------Configuration: Linked List Main - Win32 Debug--------------------
    Compiling...
    Linked List Main.cpp
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(29) : warning C4183: 'deleteLL': member function definition looks like a ctor, but name does not match enclosing class
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : error C2063: 'copyLL' : not a function
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : error C2040: 'copyLL' : 'class LinkedList<DataType>::Node *(class LinkedList<DataType>::Node *)' differs in levels of indirection from 'int'
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
    Error executing cl.exe.

    Linked List Main.obj - 3 error(s), 1 warning(s)


    Code:
    #ifndef LINKEDLIST
    #define LINKEDLIST
    #include<iostream.h>
    
    template<typename DataType>
    class LinkedList
    {
    	private:
    		class Node
    		{
    		public:          // Makes it accessible (public) by class Node
    			DataType data;
    			Node *next;
    		};
    
    		Node *first;
    		int mySize;
    
    		LinkedList::deleteLL()
    		{
    			Node *tempP=first, *disposeP;
    			while (tempP!=0)
    			{
    				disposeP=tempP;
    				tempP=tempP->next;
    				delete disposeP;
    			}
    			return;
    		}
    
    		Node* LinkedList::copyLL(Node* source)
    		{
    			if (source==0)
    				return 0;
    			Node* newH=new Node;
    			Node *tempP=newH;
    			newH->data=source->data;
    			source=source->next;
    			while (source!=0)
    			{
    				tempP->next=newNode;
    				tempP->data=source->data
    					tempP=tempP->next;
    				source=source->next;
    			}
    			temp->next=0;
    			return newH;
    		}
    
    		public:
    			LinkedList::LinkedList()
    			{
    				mySize=0;
    				first=0;
    			}
    			LinkedList::~LinkedList()
    			{
    				deleteLL();
    			}
    
    			LinkedList::LinkedList( const LinkedList &source)
    			{
    				mySize=source.first;
    				first=copyLL(source.first);
    			}
    
    			LinkedList& LinkedList::operator=(const LinkedList& s)
    			{
    				if (this!=&s)
    				{
    					deleteLL();
    					mySize=s.mySize;
    					first=copyLL(s.first)
    				}
    				return *this;
    			}
    				bool LinkedList::empty()
    				{
    					return (first==0);
    				}
    				void LinkedList::insert(DataType item, unsigned pos)
    				{
    					if (pos>mySize+1)
    					{
    						cout<<"Illegal position to insert:"<<pos<<endl;
    						return;
    					}
    					mySize++
    						Node* newNode=newNode;
    					newNode->data=item;
    					Node* prev=first;
    					for (int i=1, i<pos, i++)
    						prev=prev->next;
    					newNode->next=prev->next;
    					prev->next=newNode;
    				}
    				void LinkedList::delete (dataType item)
    				{
    					mySize--;
    					Node* tempP=first;
    					Node* prev=0;
    					while (tempP!=0 && tempP->data!=item)
    					{
    						prev=tempP;
    						tempP=tempP->next;
    					}
    					if (tempP!=0 && tempP->data==item)
    					{
    						prev->next=tempP->next;
    						delete tempP;
    					}
    						else
    							cout<<"Item to delete not found"<<endl;
    						return;
    				}
    
    				int LinkedList::locate(DataType item)
    				{
    					int position=0;
    					Node* ptr=first;
    					while(ptr->data<item &&ptr!=0)
    					{
    						position++;
    						ptr=ptr->next;
    					}
    					return position
    				}
    
    						void LinkedList::traverse()
    						{
    							Node *temp=first;
    							while (tempP!=0)
    							{
    								Process(tempP->data);
    								tempP=tempP->next;
    							}
    							return;
    						}
    				};
    #endif


    --------------------------------------------------------------------------------

    Hi I've included a basic main which I am using to test the header file

    Code:
    #include "LinkedList.h"
    #include<string>
    using namespace std;
    main()
    {
    
    
                
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the deleteLL doesn't have a return type.

    Try removing the pharase LinkedList:: in one of the functions. The compiler may feel this is a definition without a declaration. If it works for one function, do it for all of them.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Linked List

    Hi, I gave deleteLL a return type and tried removing LinkedList from 2 functions but that still did not work. I am still getting the same 3 errors. Im just confused as to why its telling me that copyLL is not a function when deleteLL is almost the same and seems to be working.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you haven't tried compiling with the LinkedList:: phrase in as well as out, I'd do so. If you have done it both ways, try compiling after placing the node class external to the linked list class rather than nested in the linked list class.

    If you haven't posted that you've figured it out or if someone hasn't already posted a successful fix by the time I get home, I'll copy and paste to my compiler to see if I can figure it out.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Remove all LinkedList::
    Only need them when you implement the functions outside the class.

    Line 19:
    add void beforeb deleteLL()

    line 97:
    delete is a keyword, not a valid function name.. rename it
    the typename is DataTyp not dataType
    Last edited by erikj; 07-22-2004 at 03:45 AM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This compiles fine for me. Runtime errors still possible, though.
    Code:
    #ifndef LINKEDLIST_H
    #define LINKEDLIST_H
    #include<iostream>
     
    template<typename DataType>
    class LinkedList
    {
    private:
    class Node
    {
    public:		 // Makes it accessible (public) by class Node
    DataType data;
    Node *next;
    };
     
    Node *first;
    int mySize;
     
    void deleteLL()
    {
    Node *tempP=first, *disposeP;
    while (tempP!=0)
    {
    	disposeP=tempP;
    	tempP=tempP->next;
    	delete disposeP;
    }
    }
     
    Node* copyLL(Node* source)
    {
    if (source==0)
    	return 0;
    Node* newH=new Node;
    Node *tempP=newH;
    newH->data=source->data;
    source=source->next;
    while (source!=0)
    {
    	tempP->next=newNode;
    	tempP->data=source->data
    	 tempP=tempP->next;
    	source=source->next;
    }
    temp->next=0;
    return newH;
    }
     
    public:
    LinkedList()
    {
    	mySize=0;
    	first=0;
    }
    ~LinkedList()
    {
    	deleteLL();
    }
    };
    #endif; 
     
    //driver program
    #include <iostream>
    #include "linkedList.h"
    using namespace std;
     
    int main(int argc, char* argv[])
    {
    LinkedList<int> myLinkedList;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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