Having trouble with Linked List

This is a discussion on Having trouble with Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi I'm newbie on the linked list concept and I'm trying to write a program that can basically allow a ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    12

    Having trouble with Linked List

    Hi I'm newbie on the linked list concept and I'm trying to write a program that can basically allow a user to store information into a linked list with the basic functions: append, delete, display, etc.

    My header file:
    Code:
     //Specification file for the ScheduleList class
    #ifndef SCHEDULELIST_H
    #define SCHEDULELIST_H
    #include <iostream>
    
    class ScheduleList
    {
    	private:
    		struct ListNode
    		{
    			int number;
    			ListNode *next;
    		};
    
    		
    		ListNode *head;
    	public:
    		ScheduleList()
    		{ head = NULL;}
    		~ScheduleList();
    		void appendNode(int);
    		//void deleteNode();
    	//	void displayList();
    };
    #endif
    The Cpp file for it:
    Code:
     //Implementation file for the ScheduleList class
    #include "ScheduleList.h"
    #include <iostream>
    using namespace std;
    
    void  ScheduleList::appendNode(int num)
    {
    	ListNode *newNode, *nodeptr;
    	newNode = new ListNode;
    	newNode->number = num;
    	newNode->next=NULL;
    
    	if(!head)
    		head = newNode;
    	else
    	{
    		nodeptr = head;
    		while(nodeptr->next)
    			nodeptr=nodeptr->next;
    		nodeptr->next = newNode;
    	}
    
    
    }
    and my Main cpp file:
    Code:
    //Linked List Project
    //Notes - http://www.functionx.com/cpp/articles/linkedlist.htm
    #include <iostream>
    #include "ScheduleList.h"
    using namespace std;
    
    
    int main()
    {
    	ScheduleList student;
    	student.appendNode(2);
    
    	return 0;
    
    }
    Anyways, it's not much so far but that's because I'm trying to do little by little so that I can be sure that it is working.

    Whenever I try and compile, it keeps giving me these same 3 error messages:
    Code:
     main.obj : error LNK2001: unresolved external symbol "public: __thiscall ScheduleList::~ScheduleList(void)" (??1ScheduleList@@QAE@XZ)
    main.obj : error LNK2001: unresolved external symbol "public: void __thiscall ScheduleList::appendNode(int)" (?appendNode@ScheduleList@@QAEXH@Z)
    Debug/main.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    I have read through many forums, but I am still clueless as to what exactly the problem is.

    Any help is greatly appreciated. Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The linker is telling you that in the class definition you have declared a destructor, and a function called "appendNode", but there isn't code for either one of them.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    You need to add "ScheduleList.cpp" to the project

    Right-click in the edit window, and you should get "Add to project"

    Then rebuild everything
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    12
    Thanks a lot for the help. My problem was that I did not create the code for the destructor yet and when I did, it was working properly.

    I have another problem regarding the code for searching a text file. My program is able to add information, display, and save it to a text file, but I'm having trouble figuring out how to search the text file for a particular entry.

    The entries in my text file look like this:

    0843543 Intro to C++ HH117

    I'm trying to implement a function to search the text file by the first value (ie: 0843543). I tried using a strcmp but it would not find the match.

    The code for this was:
    Code:
     void SearchClass()
    {
    	char input[7];
    	char course[7];
    	int i;
    	cout<<"enter in course to search: ";
    	cin>>course;
    
    	fstream readfile;
    	readfile.open("C:/WINDOWS/Desktop/class.txt", ios::in);
    	readfile.getline(input, 7);
    
    	while(!readfile.eof())
    	{
    		
    		
    		if(strcmp(course, input)==0)
    		{cout<<"match";}
    		else
    		{cout<<"no match";}
    		readfile.getline(input, 7);
    
    	}
    	readfile.close();
    
    }
    Once it finds the match I need to be able to delete it or change it. Thanks again for all the help.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    I am a newbie , so I just guess this maybe error:

    your ScheduleList.h


    class ScheduleList
    {
    ......
    public:
    ScheduleList()
    { head = NULL;}
    ~ScheduleList();
    void appendNode(int);
    //this variable defined maybe like this :
    // void appendNode(int num),add 'num'.
    .....
    };#endif

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by toysoldier
    I am a newbie , so I just guess this maybe error:

    your ScheduleList.h
    That's what's known as a function prototype. When prototyping a function, be it in a class, or by itself, you are giving the basics of what the function is:
    Code:
    #include <iostream>
    
    //Prototype a few functions, so we can use them before we define them.
    void foo( int );
    int bar( void );
    
    int main( void )
    {
        //Because both prototypes have been given, we can now call these
        //functions, because we know what type of parameters, name, and
        //return values they have, if any.
        int x = 5;
    
        foo( x );
        x = bar( );
    
        return 0;
    }
    
    //Now we will define the functions temselves.
    void foo( int x )
    {
        std::cout << "Our argument has the value of " << x << ".\n";
    }
    
    int bar( void )
    {
        std::cout << "We will return 3, just for the heck of it.\n";
        return 3;
    }
    What's interesting about prototypes, is that we could have made the first
    prototype be this:
    Code:
    void foo( int somevariablename );
    And defined it as this:
    Code:
    void foo( int x )
    {
        std::cout << "Our argument has the value of " << x << ".\n";
    }
    And the code would both compile, as well as be perfectly legal. The name of
    the parameters may be omitted entirely, or called whatever you like (as long
    as it's a legal variable name), because the prototype just cares what the type
    of each argument is, not its name.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by hkmixxa
    Thanks a lot for the help. My problem was that I did not create the code for the destructor yet and when I did, it was working properly.

    I have another problem regarding the code for searching a text file. My program is able to add information, display, and save it to a text file, but I'm having trouble figuring out how to search the text file for a particular entry.

    The entries in my text file look like this:

    0843543 Intro to C++ HH117

    I'm trying to implement a function to search the text file by the first value (ie: 0843543). I tried using a strcmp but it would not find the match.

    ...

    Once it finds the match I need to be able to delete it or change it. Thanks again for all the help.
    You'll have to do it one of a few ways:
    1) read a "word" from the file and then use strcmp on it
    2) read a "line" from the file, strip off the first word, and then use strcmp on it

    That's how you'll get your comparison. strcmp compares the entire string, not just one word. So you will have to manually pull the word to compare. In this case, the "word" happens to be a number.

    You could I suppose read the first "word" as a number, and then compare it, but I'd go for one of the first two options.

    As for changing the item, that'll be a bit more difficult.

    1) If the "word" you are replacing is exactly the same size, you can open the file in binary mode for both reading and writing, and seek to the spot where the word begins, and then write out the new word on top of it.

    2) If not, you will have to reconstruct the entire file to change this word.
    a) read the file into memory beyond the point you want to change
    b) seek to the spot you want to change
    c) write the change, as well as every thing else you've read into memory
    d) truncate if it is too long.

    a) read the file into memory
    b) make the changes in memory
    c) write a new file
    d) get rid of the old one

    Something along those lines.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ARRRGGG!! READ THE ANNOUNCEMENTS! YOU HAVE BEEN TOLD! There's nothing quite like wasting your time on a problem "helping someone", when they have already been helped because it's is a duplicate post!

    Actually, I know you've been told, because you yourself in another post, stated that "you're posting too much, and you're 'sorry' BUT..."

    Stop making multiple posts on the same topic.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  2. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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