double linked list

Im working on a linked list project that is suppose to be a pokedex. I got it to start up, but it keeps crashing when i use case 1 which is the insert function. what am i doing wrong that is making the program crash? and also in the main function, im suppose to the current pointer to the first pokemon? im not sure how to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

/********************************************
			Pokemon Class      
 ********************************************/
class Pokemon {
	private:
		string name;					//name of the pokemon
		Pokemon *prev;					//pointer the points to previous pokemon
		Pokemon *next;					//pointer that points to next pokemon object
	
	public:
		Pokemon(string s); // set pokemon name; set prev and next to null
		Pokemon();
		string getName();				// return the name of the pokemon
		void setName(string);			// set new name to the pokemon
		Pokemon * getPrev();			// return prev pointer
		Pokemon * getNext();			// return next pointer
		void setPrev(Pokemon *ptr);		// pointer prev points to new pokemon
		void setNext(Pokemon *ptr);		// pointer next points to new pokemon
		void displayPokemon(); // display pokemon
		friend class PokemonList;

};
Pokemon::Pokemon(){

}

/********************************************
              Pokemon::Pokemon               
 Constructor that initalizes pokemon name
 	set prev poitner to null
 	set next pointer to null      
 ********************************************/
Pokemon::Pokemon(string s){
	name = s;
	Pokemon *prev = NULL;
	Pokemon *next = NULL;
}

string Pokemon::getName(){
	return name;
}

void Pokemon::setName(string){
	name = name;
}

Pokemon *Pokemon::getPrev(){
	return prev;
}

Pokemon *Pokemon::getNext(){
	return next;
}

void Pokemon::setPrev(Pokemon *ptr){
	prev = ptr;
}

void Pokemon::setNext(Pokemon *ptr){
	next = ptr;
}

/********************************************
              Pokemon::displayPokemon()              
 Display the address that prev holds, address of the current object,
 the address that next holds, and the pokemon name
	
	0x7fe9ba500030 || 0x7fe9ba500000 || 0x7fe9ba500030   Charizard
	0x7fe9ba500000 || 0x7fe9ba500030 || 0x7fe9ba500000   Pikachu

	tips: use (this) to display the address of the current pokemon object
 ********************************************/
void Pokemon::displayPokemon() {
	cout << &Pokemon::prev << this << &Pokemon::next << name;
}


/********************************************
			PokemonList Class      
 ********************************************/
class PokemonList{
	private:
		Pokemon *first;
		int totalPokemon;

	public:
		PokemonList();				//set first to null, set total to 0;
		int getTotal();				//return value of totalPokemon
		bool isEmpty();				//check if the list is empty
		Pokemon * getFirst();		//return the first pointer value
		void insert();				//insert a pokemon at the end of the list
		void displayList(Pokemon *current);			//display all pokemon in the list
		friend class Pokemon;
};
//make sure you provide header comments

PokemonList::PokemonList(){
	Pokemon *first = NULL;
	totalPokemon = 0;
}

int PokemonList::getTotal(){
	return totalPokemon;
}

bool PokemonList::isEmpty(){
	if(totalPokemon == 0)
		return true;
	else
		return false;
}

Pokemon *PokemonList::getFirst(){
	return first;
}

/********************************************
             PokemonList::insert()

Ask user to enter the name of pokemon

if the list is empty
	dynamically allocate first pointer with new pokemon
	assign first's next pointer to first address
	assigne first's prev pointer to first address
	The circle of life (LION KING THEME)

else 
	Dyanmically allocate a new pokemon
	Draw out the process of adding a node to the end of double linked-list
	Do crazy pointer manipulation base on the drawing
	(I will go over the process in class so please be there)

 ********************************************/
void PokemonList::insert() {
	string poke;
	Pokemon *temp1, *temp2;

	cout << "Pokemon Name: ";
	cin >> poke;


	if(totalPokemon == 0){
		temp1 = new Pokemon;
		temp1->prev = NULL;
		temp1->name = poke;
		temp1->next = first;
		first->prev = temp1;
		first = temp1;
	}
	else{
		temp2 = new Pokemon;
		temp1 = new Pokemon;
		while(temp2->next != NULL){
			temp2 = temp2->next;
		}
		temp2->next = temp1;
		temp1->prev = temp1;
		temp1->next = NULL;
	}

}

void PokemonList::displayList(Pokemon *current){
	while(current != NULL)
	{
		cout << current->next << endl;
		current = current->next;
	}
}

/********************************************
			Main function     
 ********************************************/
int main() {

	int choice;
	PokemonList pokeDex;			//create an empty list
	Pokemon *current;				// the current pokemon you look at

	do {
		cout << endl << "=====================================" << endl;
		cout << "PokeDex" << endl;
		cout << "=====================================" << endl;
		cout << "Select your option:" << endl;
		cout << "1) Insert new Pokemon" << endl;
		cout << "2) Next Pokemon" << endl;
		cout << "3) Prev Pokemon" << endl;
		cout << "4) List all Pokemons" <<endl;
		cout << "5) Quit" << endl;
		cout << "=====================================" << endl;

		//YOUR CODE GOES HERE
		//IF WE HAVE 1 POKEMON IN THE LIST
		//ASSIGNING CURRENT POINTER TO THE FIRST POKEMON
	
		if (pokeDex.getTotal() > 0) {
			cout << endl <<"Current Pokemon" <<endl;
			current->displayPokemon();
		}
		cout << endl << "Please select your choice: ";
		cin >> choice;

		switch(choice) {
			case 1:
				pokeDex.insert();
				break;

			case 2:
				//IF THE LIST IS NOT EMPTY
				if(pokeDex.isEmpty() == false){
					current = current->getNext(); //ASSIGN CURRENT POINTER TO THE NEXT POKEMON
				}
				else{
					cout << "The list is empty";
				}

				break;

			case 3:
				//IF THE LIST IS NOT EMPTY
				if(pokeDex.isEmpty() == false){
					current = current->getPrev();//ASSIGN CURRENT POINTER TO THE PREVIOUS POKEMON
				}
				else{
					cout << "The list is empty";
				}
				break;

			case 4:
				//IF THE LIST IS NOT EMPTY
				if(pokeDex.isEmpty() == false){
					pokeDex.displayList(current); //DISPLAY ALL POKEMON IN THE LIST
				}
				else{
					cout << "The list is empty";
				}
				break;

			case 5:
				cout << "Terminating the super secret Pokemon list" << endl;
				break;
			default:
				cout << "Invalid choice!!" << endl;
		}
	} while(choice != 5);


	return 0;
}
Last edited on
On line 104: first is a local variable (that shadows the respective member variable). The member first will not be initialized.

In insert(): totalPokemon will never be increased and hence remains 0.
Topic archived. No new replies allowed.