[RESOLVED] How do I use a linked list with a class?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] How do I use a linked list with a class?

  1. #1
    Join Date
    Jan 2007
    Posts
    10

    [RESOLVED] How do I use a linked list with a class?

    I would like to know how to use a linked list with a class. I have built a small test of using linked lists in the code below: (a lot of this code is from stuff I found online in tutorials, so much of it is not mine):
    Code:
    //includes needed
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    #include <string>
    
    //functions
    void add_node_at_end(void);
    void display_list(void);
    void delete_start_node(void);
    void delete_end_node(void);
    void move_current_on (void);
    void move_current_back (void);
    
    using namespace std;
    
    struct node
     {  char name[20];    // Name of up to 20 letters
        int date;          // year the CD came out
        char artist[20];     // Artist's name
        node *nxt;// Pointer to next node
     };
    
    node *start_ptr = NULL;
    node *current;           // Used to move along the list
    
    //varibles
    int option = 0; //used to pick item from list
    string sName; //used to save/load file
    
    //for reading and writing files
    FILE* myFile;
    
    void main()
     {
             start_ptr = NULL;
        do
           {
    		   //menu
            system ("cls");
            display_list();
            cout << endl;
            cout << "Please select an option : " << endl;
            cout << "0. Exit the program." << endl;
            cout << "1. Add a CD to the end of the list." << endl;
            cout << "2. Delete the first CD from the list." << endl;
            cout << "3. Delete the last CD from the list." << endl;
            cout << "4. Move the current pointer forward one CD." << endl;
    		cout << "5. Move the current pointer back one CD." << endl;
    		cout << endl << " >> ";
            cin >> option;
    
    		//determines where to go
            switch (option)
               {
                 case 1 : add_node_at_end(); break;
                 case 2 : delete_start_node(); break;
                 case 3 : delete_end_node(); break;
                 case 4 : move_current_on(); break;
    			 case 5 : move_current_back();
               }
            getch();
           }
        while (option != 0); //continues loop till user selects 0"exit"
     }
    
    void add_node_at_end()//adds a CD to the list
     {  node *temp, *temp2;   // Temporary pointers
    
        cout << "Please use an _ for spaces between names\n";
    
        // Reserve space for new node and fill it with data
        temp = new node;
        cout << "Please enter the name of the CD: ";
        cin >> temp->name;
        cout << "Please enter the year it was made : ";
        cin >> temp->date;
        cout << "Please enter the name of the artist : ";
        cin >> temp->artist;
        temp->nxt = NULL;
    
        // Set up link to this node
        if (start_ptr == NULL)
          { start_ptr = temp;
            current = start_ptr;
          }
        else
          { temp2 = start_ptr;
            // We know this is not NULL - list not empty!
            while (temp2->nxt != NULL)
              {  temp2 = temp2->nxt;
                 // Move to next link in chain
              }
            temp2->nxt = temp;
          }
     }
    
    void display_list()//displays the list
     {  node *temp;
        temp = start_ptr;
        cout << endl;
        if (temp == NULL)
          cout << "The list is empty!" << endl;
        else
          { while (temp != NULL)
              {  // Display details for what temp points to
                 cout << "Name : " << temp->name << " ";
                 cout << "Year : " << temp->date << " ";
                 cout << "Artist : " << temp->artist;
                 if (temp == current)
                   cout << " <-- Current CD";
                 cout << endl;
                 temp = temp->nxt;
    
              }
            cout << "End of list!" << endl;
          }
     }
    
    void delete_start_node()//deletes the first CD
      { node *temp;
        temp = start_ptr;
        start_ptr = start_ptr->nxt;
        delete temp;
      }
    
    void delete_end_node()//deletes the last CD
      { node *temp1, *temp2;
        if (start_ptr == NULL)
             cout << "The list is empty!" << endl;
        else
           { temp1 = start_ptr;
             if (temp1->nxt == NULL)
                { delete temp1;
                  start_ptr = NULL;
                }
             else
                { while (temp1->nxt != NULL)
                   { temp2 = temp1;
                     temp1 = temp1->nxt;
                   }
                  delete temp1;
                  temp2->nxt = NULL;
                }
           }
      }
    
    void move_current_on ()//move cursor toward the begining
      { if (current->nxt == NULL)
         cout << "You are at the end of the list." << endl;
        else
         current = current->nxt;
      }
    
    void move_current_back ()//move cursor toward the end
      { if (current == start_ptr)
         cout << "You are at the start of the list" << endl;
        else
         { node *previous;     // Declare the pointer
           previous = start_ptr;
    
           while (previous->nxt != current)
             { previous = previous->nxt;
             }
           current = previous;
         }
      }
    This works, however this is with a struct, and like I stated I'd like to use a linked list with a class. I tried modifing this code to be compatable with a class. the results gave me some errors. first the code:
    main.cpp:
    Code:
    //includes needed
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include "CD.h"
    
    using namespace std;
    
    node *start_ptr = NULL;
    node *current;           // Used to move along the list
    
    //varibles
    int option = 0; //used to pick item from list
    string sName; //used to save/load file
    
    //for reading and writing files
    FILE* myFile;
    
    void main()
     {
    	 node n;
    
             start_ptr = NULL;
        do
           {
            system ("cls");
            display_list();
            cout << endl;
            cout << "Please select an option : " << endl;
            cout << "0. Exit the program." << endl;
            cout << "1. Add a CD to the end of the list." << endl;
            cout << "2. Delete the first CD from the list." << endl;
            cout << "3. Delete the last CD from the list." << endl;
            cout << "4. Move the current pointer forward one CD." << endl;
    		cout << "5. Move the current pointer back one CD." << endl;
    		cout << endl << " >> ";
            cin >> option;
    
            switch (option)
               {
                 case 1 : n.add_node_at_end(); break;
                 case 2 : n.delete_start_node(); break;
                 case 3 : n.delete_end_node(); break;
                 case 4 : n.move_current_on(); break;
    			 case 5 : n.move_current_back();
               }
            getch();
           }
        while (option != 0);
     }
    CD.h:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    class node
     { 
    	public:
    	void add_node_at_end();
    	void display_list();
    	void delete_start_node();
    	void delete_end_node();
    	void move_current_on ();
    	void move_current_back ();
    
    	private:
    	char name[20];    // Name of up to 20 letters
        int date;          // year the CD came out
        char artist[20];     // Artist's name
        node *nxt;// Pointer to next node
     };
    
    
    void node::add_node_at_end()
     {  node *temp, *temp2;   // Temporary pointers
    
        cout << "Please use an _ for spaces between names\n";
    
        // Reserve space for new node and fill it with data
        temp = new node;
        cout << "Please enter the name of the CD: ";
        cin >> temp->name;
        cout << "Please enter the year it was made : ";
        cin >> temp->date;
        cout << "Please enter the name of the artist : ";
        cin >> temp->artist;
        temp->nxt = NULL;
    
        // Set up link to this node
        if (start_ptr == NULL)
          { start_ptr = temp;
            current = start_ptr;
          }
        else
          { temp2 = start_ptr;
            // We know this is not NULL - list not empty!
            while (temp2->nxt != NULL)
              {  temp2 = temp2->nxt;
                 // Move to next link in chain
              }
            temp2->nxt = temp;
          }
     }
    
    void node::display_list()
     {  node *temp;
        temp = start_ptr;
        cout << endl;
        if (temp == NULL)
          cout << "The list is empty!" << endl;
        else
          { while (temp != NULL)
              {  // Display details for what temp points to
                 cout << "Name : " << temp->name << " ";
                 cout << "Year : " << temp->date << " ";
                 cout << "Artist : " << temp->artist;
                 if (temp == current)
                   cout << " <-- Current CD";
                 cout << endl;
                 temp = temp->nxt;
    
              }
            cout << "End of list!" << endl;
          }
     }
    
    void node::delete_start_node()
      { node *temp;
        temp = start_ptr;
        start_ptr = start_ptr->nxt;
        delete temp;
      }
    
    void node::delete_end_node()
      { node *temp1, *temp2;
        if (start_ptr == NULL)
             cout << "The list is empty!" << endl;
        else
           { temp1 = start_ptr;
             if (temp1->nxt == NULL)
                { delete temp1;
                  start_ptr = NULL;
                }
             else
                { while (temp1->nxt != NULL)
                   { temp2 = temp1;
                     temp1 = temp1->nxt;
                   }
                  delete temp1;
                  temp2->nxt = NULL;
                }
           }
      }
    
    void node::move_current_on ()
      { if (current->nxt == NULL)
         cout << "You are at the end of the list." << endl;
        else
         current = current->nxt;
      }
    
    void node::move_current_back ()
      { if (current == start_ptr)
         cout << "You are at the start of the list" << endl;
        else
         { node *previous;     // Declare the pointer
           previous = start_ptr;
    
           while (previous->nxt != current)
             { previous = previous->nxt;
             }
           current = previous;
         }
      }
    the errors I receive:
    1. cd.h(40) : error C2065: 'start_ptr' : undeclared identifier
    2. cd.h(42) : error C2065: 'current' : undeclared identifier
    3. cd.h(80) : error C2227: left of '->nxt' must point to class/struct/union/generic type
    4. cd.h(106) : error C2227: left of '->nxt' must point to class/struct/union/generic type
    5. cd.h(109) : error C2227: left of '->nxt' must point to class/struct/union/generic type
    6. cd.h(119) : fatal error C1903: unable to recover from previous error(s); stopping compilation

    I am deeply confused by this. I'm fairly new to classes and pointers, so am I forgetting something obvious? If not, how can I use a linked list with a class?

    EDIT: I am writing it in a win32 console application

    Thank you very much in advance,

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: How do I use a linked list with a class?

    start_ptr is defined in main.cpp but referenced in cd.h. The compiler doesn't know what it is in the cd.h file. Generally .h files should store definitions and actual code should be in your cpp files. Move add_node_at_end function to main.cpp and you should be okay.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How do I use a linked list with a class?

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jan 2003
    Posts
    615

    Re: How do I use a linked list with a class?

    How about adding this to cd.h file (before class definition).
    Code:
    extern node *start_ptr;

  5. #5
    Join Date
    Jan 2007
    Posts
    10

    Re: How do I use a linked list with a class?

    Thank you.

  6. #6
    Join Date
    May 2009
    Posts
    1

    Re: [RESOLVED] How do I use a linked list with a class?

    How can I delete random element from this list? Please, provide me with the complete program version. Thanks in advance.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] How do I use a linked list with a class?

    Quote Originally Posted by HD295 View Post
    How can I delete random element from this list? Please, provide me with the complete program version. Thanks in advance.
    There's a standard library coming with C++. It provides lots of different containers including a linked list based one.

    And, to the question of how you can delete a random element from a list. You traverse the list in order to find it and then replace/delete it. The standard library provides methods for that.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center