|
-
January 12th, 2007, 09:55 PM
#1
[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,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|