uhm guys... i need help
i'm doing a project today from the subject DATA STRUCTURES. It's about implementing the linked list operations (construction, empty, insert, delete). my linked list is linked list of movies
i'm using a node to store each elements in the linked list
the problem is... even though i entered a movie name and the year it was shown, my code displays an empty list :( I'm pretty sure i incremented the size after i stored the movie data
Now I posted my code fragment by fragment instead of my whole program because it's very long :/
Here's the code fragment for the class LList. the class inside it is the node. I named it Movie instead of Node, which my teacher usually do. I made everything public so that i have no problems in accesing datas and functions wherever i want to (i'll encapsulate it later)
Note: ignore the destructor (i'll work on it later)
Please tell me if something's wrong about this code fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class LList{
public:
int size;
class Movie{
public:
string name;
string year;
Movie *next;
};
Movie *first;
LList();
//~LList();
bool empty();
void insert1(string a, string b, int c);
void insert2(string a, string b);
void erase1(int c);
void erase2();
void dispMenu();
void dispList();
void askChoice();
};
|
please take note of the data int size... because I'm suspecting that the problem comes from there
next I'll show you my insert function. basically, this code inserts an element at the beginning of the list. this is just one of the cases under the insert operation. if i solve this problem i think i may be able to fix the other insert operation case (which is inserting an element after some element in the list). notice that i increment the size so that every time the user enters a movie data the list will expand.
Note: at the beginning of the program, i created a constructor the sets size to 0 and makes the pointer "first" points to null.
Please tell me if something's wrong about this code fragment
1 2 3 4 5 6
|
void LList::insert2(string a, string b){
Movie *newPtr = new Movie;
newPtr->name = a; newPtr->year = b; newPtr->next = first;
first = newPtr;
size++;
}
|
now the next code fragment is where i think the heart of the problem lies, the display function. what this function do is to display the list and its element and it displays an error message if the size is 0 or the list is empty
(by the way, i think i need to show you my empty function. it returns true if the list is empty; which is size is 0)
1 2 3 4 5
|
bool LList::empty(){
bool empty = true;
if(size == 0)
return empty;
}
|
now, here's my display function
i can't even debug the else condition because the empty message keeps on appearing
Note: the pointer "first" always points to the first node in the list
Please tell me if something's wrong about this code fragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void LList::dispList(){
if(empty()){
cout << "***There is no element in the list***" << endl;
}
else{
Movie *listPtr;
listPtr = first;
for(int counter=0; listPtr!=NULL; counter++){
cout << "Element Number: " << counter+1 << endl
<< listPtr->name << listPtr->year << endl
<< endl << endl;
}
}
}
|
i think i've given you all the code fragments that is involved in my problem. but if you wish to see some parts of my program, please feel free to comment :) you can debug some parts of the code fragments i've given to you if you want.. hehe.. but my priority is the displaying problem. i'll handle the others later :)
please help me. i'm halfway in my program and i ran out of options :( i need to finish this because this is important
thank you in advance