toolshed
02-21-2004, 02:21 PM
I am Java programmer that is trying to learn C++ to help me further my Linux skills...I having trouble compiling a simple linkedlist.
I need a pointer to another LinkedList in my node class, but it just not compile it, below is the code if anyone can help. I also attached the file.
LinkedListPW.cpp:16: error: syntax error before `*' token
#include <iostream>
#include <string>
#include "LinkedList.cpp"
using namespace std;
class NodePW {
public:
int count;
string s;
//pointer to the node that is directly next
NodePW* next;
//COMPILE ERROR BELOW COMMENT: here is the problem if I uncomment this line I through an error.It
//must not be able to see the LinkedListPW class below or something
LinkedListPW* nextList;
//THIS DECLARATION ABOVE IS THE COMPILE ERROR
NodePW(string s) {
this->s = s;
}
NodePW() {
next = NULL;
//UNCOMMENT: this line to for your compile
//nextList = new LinkedListPW();
count++;
}
~NodePW() {
if (next != NULL)
delete next;
}
void incCount() {
count++;
}
};
class LinkedListPW {
NodePW *head;
NodePW *tail;
NodePW *current;
int size;
public:
LinkedListPW() {
size = 0;
head = NULL;
tail = NULL;
}
~LinkedListPW() {
delete head;
}
void add(string s);
void del(string s);
void print();
NodePW* next();
void start();
bool hasNext();
bool contains(string s);
NodePW* get(string s);
NodePW* get(int index);
bool isEmpty();
int getSize();
};
void LinkedListPW::add(string s) {
NodePW *temp;
temp = new NodePW;
temp->s = s;
if (size == 0) {
head = temp;
tail = temp;
} else {
tail->next = temp;
tail = temp;
}
/* Our list is now one size bigger. */
size++;
//cout << "LinkedListPW::add(" << temp->s << ")." << endl;
}
bool LinkedListPW::isEmpty() {
if (this->size <= 0) {
return true;
}
return false;
}
int LinkedListPW::getSize() {
return this->size;
}
void LinkedListPW::del(string s) {
NodePW *temp = head, *prev = head;
for (int i=0; i<size; i++) {
if (temp->s == s) {
prev->next = temp->next;
if (head == temp)
head = temp->next;
temp->next = NULL;
delete temp;
i = size;
}
else {
prev = temp;
temp = temp->next;
}
}
size--;
//cout << "LinkedListPW::del(" << s << ")." << endl;
}
void LinkedListPW::print() {
NodePW *temp;
temp = head;
cout << "LinkedListPW::print() output: ";
do {
cout << "[" << temp->s << "] --> ";
temp = temp->next;
} while (temp != NULL);
cout << "NULL" << endl;
}
NodePW* LinkedListPW::next() {
if (current == NULL) {
return NULL;
}
else {
NodePW* temp = current;
current = current->next;
return temp;
}
}
bool LinkedListPW::contains(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return true;
}
temp = temp->next;
}
return false;
}
void LinkedListPW::start() {
current = head;
}
bool LinkedListPW::hasNext() {
if (current != NULL && size >= 0) {
return true;
}
else {
return false;
}
}
NodePW* LinkedListPW::get(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return temp;
}
temp = temp->next;
}
return NULL;
}
NodePW* LinkedListPW::get(int index) {
NodePW* temp;
this->start();
for (int i = 0; i <= index; i++) {
if(this->hasNext())
temp = this->next();
else
temp = NULL;
}
if (temp == NULL){
return NULL;
}
return temp;
}
int main(int argc, char *argv[]) {
LinkedListPW list;
list.add("zero");
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");;
list.add("six");
list.print();
//list.del("three");
//list.print();
list.start();
while(list.hasNext()) {
NodePW* node = list.next();
string s = node->s;
cout << s << endl;
}
if (list.contains("tdfdfwo")) {
cout << "Look What I found." <<endl;
}
else {
cout << "Sorry no dice." <<endl;
}
// NodePW* getNodePW = list.get("two");
NodePW* getNodePW = list.get(3);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(6);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(0);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(7);
if (getNodePW == NULL) {
cout << "NULL I AM!" << endl;
}
else{
cout << "GetNOde: " << getNodePW->s << endl;
}
cout << "Done, press enter." << endl;
char c;
cin >> c;
return 0;
}
I need a pointer to another LinkedList in my node class, but it just not compile it, below is the code if anyone can help. I also attached the file.
LinkedListPW.cpp:16: error: syntax error before `*' token
#include <iostream>
#include <string>
#include "LinkedList.cpp"
using namespace std;
class NodePW {
public:
int count;
string s;
//pointer to the node that is directly next
NodePW* next;
//COMPILE ERROR BELOW COMMENT: here is the problem if I uncomment this line I through an error.It
//must not be able to see the LinkedListPW class below or something
LinkedListPW* nextList;
//THIS DECLARATION ABOVE IS THE COMPILE ERROR
NodePW(string s) {
this->s = s;
}
NodePW() {
next = NULL;
//UNCOMMENT: this line to for your compile
//nextList = new LinkedListPW();
count++;
}
~NodePW() {
if (next != NULL)
delete next;
}
void incCount() {
count++;
}
};
class LinkedListPW {
NodePW *head;
NodePW *tail;
NodePW *current;
int size;
public:
LinkedListPW() {
size = 0;
head = NULL;
tail = NULL;
}
~LinkedListPW() {
delete head;
}
void add(string s);
void del(string s);
void print();
NodePW* next();
void start();
bool hasNext();
bool contains(string s);
NodePW* get(string s);
NodePW* get(int index);
bool isEmpty();
int getSize();
};
void LinkedListPW::add(string s) {
NodePW *temp;
temp = new NodePW;
temp->s = s;
if (size == 0) {
head = temp;
tail = temp;
} else {
tail->next = temp;
tail = temp;
}
/* Our list is now one size bigger. */
size++;
//cout << "LinkedListPW::add(" << temp->s << ")." << endl;
}
bool LinkedListPW::isEmpty() {
if (this->size <= 0) {
return true;
}
return false;
}
int LinkedListPW::getSize() {
return this->size;
}
void LinkedListPW::del(string s) {
NodePW *temp = head, *prev = head;
for (int i=0; i<size; i++) {
if (temp->s == s) {
prev->next = temp->next;
if (head == temp)
head = temp->next;
temp->next = NULL;
delete temp;
i = size;
}
else {
prev = temp;
temp = temp->next;
}
}
size--;
//cout << "LinkedListPW::del(" << s << ")." << endl;
}
void LinkedListPW::print() {
NodePW *temp;
temp = head;
cout << "LinkedListPW::print() output: ";
do {
cout << "[" << temp->s << "] --> ";
temp = temp->next;
} while (temp != NULL);
cout << "NULL" << endl;
}
NodePW* LinkedListPW::next() {
if (current == NULL) {
return NULL;
}
else {
NodePW* temp = current;
current = current->next;
return temp;
}
}
bool LinkedListPW::contains(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return true;
}
temp = temp->next;
}
return false;
}
void LinkedListPW::start() {
current = head;
}
bool LinkedListPW::hasNext() {
if (current != NULL && size >= 0) {
return true;
}
else {
return false;
}
}
NodePW* LinkedListPW::get(string s) {
NodePW* temp = head;
while (temp != NULL) {
string tempS = temp->s;
if(tempS == s){
return temp;
}
temp = temp->next;
}
return NULL;
}
NodePW* LinkedListPW::get(int index) {
NodePW* temp;
this->start();
for (int i = 0; i <= index; i++) {
if(this->hasNext())
temp = this->next();
else
temp = NULL;
}
if (temp == NULL){
return NULL;
}
return temp;
}
int main(int argc, char *argv[]) {
LinkedListPW list;
list.add("zero");
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");;
list.add("six");
list.print();
//list.del("three");
//list.print();
list.start();
while(list.hasNext()) {
NodePW* node = list.next();
string s = node->s;
cout << s << endl;
}
if (list.contains("tdfdfwo")) {
cout << "Look What I found." <<endl;
}
else {
cout << "Sorry no dice." <<endl;
}
// NodePW* getNodePW = list.get("two");
NodePW* getNodePW = list.get(3);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(6);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(0);
cout << "GetNOde: " << getNodePW->s << endl;
getNodePW = list.get(7);
if (getNodePW == NULL) {
cout << "NULL I AM!" << endl;
}
else{
cout << "GetNOde: " << getNodePW->s << endl;
}
cout << "Done, press enter." << endl;
char c;
cin >> c;
return 0;
}