Y
Y2J
I am working through this book on C++ programming, the author is
speaking of using linked lists. He gave and example which I found
confusing to say the least. So I rewrote the example in a way that I
could better understand the concept, he was trying to convey to me.
I ran my own example and it crashed and burn "what a surprise!" : (. I
ran the authors example out of the book and quess what, it crashed
also, : 0. I ran them both on my laptop that uses the blood shed C++
IDE and they both crashed, I ran them both on my desktop in MS Visual
Studio 6.0 and they both crashed.
So I kept trying to look over the code carefully and see what the hell
is going on here, but I lack the experience to understand the error
messages that are being returned to me. I need to understand this
concept as I know it is important to understand it before I can move
on.
Here is the code I re wrote:
#include <iostream>
using namespace std;
struct node
{
int data;
node *nextNode;
node *previousNode;
};
class linkedList
{
private:
node baseNode;
node *currentNode;
public:
void pushMethod(int item);
int popMethod();
};
void linkedList:ushMethod(int item)
{
node *temporaryNodeToBePushedOn = currentNode;
currentNode->nextNode = new node;
currentNode = currentNode->nextNode;
currentNode->previousNode = temporaryNodeToBePushedOn;
}
int linkedList:opMethod()
{
int temporaryNodeToBePoppedOff;
temporaryNodeToBePoppedOff = currentNode->data;
if(currentNode->previousNode == NULL)
{
return temporaryNodeToBePoppedOff;
}
currentNode = currentNode->previousNode;
delete currentNode->nextNode;
return temporaryNodeToBePoppedOff;
}
int main()
{
linkedList myLinkedList;
int data;
cout << "Enter your data, and enter -1 to exit \n";
while (data != -1)
{
cout << "Enter your next number: ";
cin >> data;
myLinkedList.pushMethod(data);
}
return 0;
} // End of double linked list example : )
*** Can anyone take the time to explain what is wrong here? ***
In the begining of the program I see this:
struct node
{
int data;
node *nextNode;
node *previousNode;
};
What doesn't make sense to me is that how can a structure be declared
with in a structure if the structure is first being declared. This
little piece of code appears as if the structure is being declared and
then two types of the same structure are being declared within it at
the same time. I thought that the structure is declared and then as a
data type and then it is used as a data type in declaring other things
such as pointers and variables. Anyways I must be retarded or
something. But the author gives like a five sentence explanation about
this then moves onto something completely different all together.
Peace, Jaret
speaking of using linked lists. He gave and example which I found
confusing to say the least. So I rewrote the example in a way that I
could better understand the concept, he was trying to convey to me.
I ran my own example and it crashed and burn "what a surprise!" : (. I
ran the authors example out of the book and quess what, it crashed
also, : 0. I ran them both on my laptop that uses the blood shed C++
IDE and they both crashed, I ran them both on my desktop in MS Visual
Studio 6.0 and they both crashed.
So I kept trying to look over the code carefully and see what the hell
is going on here, but I lack the experience to understand the error
messages that are being returned to me. I need to understand this
concept as I know it is important to understand it before I can move
on.
Here is the code I re wrote:
#include <iostream>
using namespace std;
struct node
{
int data;
node *nextNode;
node *previousNode;
};
class linkedList
{
private:
node baseNode;
node *currentNode;
public:
void pushMethod(int item);
int popMethod();
};
void linkedList:ushMethod(int item)
{
node *temporaryNodeToBePushedOn = currentNode;
currentNode->nextNode = new node;
currentNode = currentNode->nextNode;
currentNode->previousNode = temporaryNodeToBePushedOn;
}
int linkedList:opMethod()
{
int temporaryNodeToBePoppedOff;
temporaryNodeToBePoppedOff = currentNode->data;
if(currentNode->previousNode == NULL)
{
return temporaryNodeToBePoppedOff;
}
currentNode = currentNode->previousNode;
delete currentNode->nextNode;
return temporaryNodeToBePoppedOff;
}
int main()
{
linkedList myLinkedList;
int data;
cout << "Enter your data, and enter -1 to exit \n";
while (data != -1)
{
cout << "Enter your next number: ";
cin >> data;
myLinkedList.pushMethod(data);
}
return 0;
} // End of double linked list example : )
*** Can anyone take the time to explain what is wrong here? ***
In the begining of the program I see this:
struct node
{
int data;
node *nextNode;
node *previousNode;
};
What doesn't make sense to me is that how can a structure be declared
with in a structure if the structure is first being declared. This
little piece of code appears as if the structure is being declared and
then two types of the same structure are being declared within it at
the same time. I thought that the structure is declared and then as a
data type and then it is used as a data type in declaring other things
such as pointers and variables. Anyways I must be retarded or
something. But the author gives like a five sentence explanation about
this then moves onto something completely different all together.
Peace, Jaret