|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
The Code
Code:
#include<iostream>
using namespace std;
//Defines the node
struct node
{
//An integer
int value;
//A node pointer
node *next;
};
int main(void)
{
//Declares the first, unchanging node
node *root;
//Declares the "conductor" node which will traverse down the list
node *conductor;
//The first node, root, points to a new node
root = new node;
//The fith node along the line, has it's pointer, *next, set equal to a null pointer
root->next->next->next->next = 0;
//Sets the value of the nodes
root->value=1;
root->next->value=2;
root->next->next->value=3;
root->next->next->next->value=4;
//Conductor is set to point to root
conductor=root;
//Prints all the values in the list
while(conductor!=NULL)
{
cout<<conductor->value<<" ";
conductor=conductor->next;
}
cout<<"\n";
cin.get();
return 0;
}
Everything works fine untill the last cin.get(); when an error comes up on windows vista saying "linked_lists.exe has stopped working. Windows is checking for a solution to the problem." Thanks for your help Ratchet Last edited by WingedPanther; 07-15-2008 at 11:58 AM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||||
|
I would expect this line:
root->next->next->next->next = 0; to generate an error. root->next is not initialized, so there is no telling where it's pointing, and if root->next = 0 then root->next->next is invalid.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
Nothing seems to have worked I'm afraid. I don't think there is a problem with the code because the compiler hasn't spotted one, plus, I only get the error message at the last cin.get(); when the program is exiting. I think it's probably compatibility issues with Vista, I'm just trying to find out what.
Thanks for your replies by the way; I fixed the problem that WingedPanther pointed out; the code now reads: Code:
#include<iostream>
using namespace std;
//Defines the node
struct node
{
//An integer
int value;
//A node pointer
node *next;
};
int main(void)
{
//Declares the first, unchanging node
node *root;
//Declares the "conductor" node which will traverse down the list
node *conductor;
//The first node, root, points to a new node
root = new node;
//Sets the value of the nodes
root->value=1;
root->next->value=2;
root->next->next->value=3;
root->next->next->next->value=4;
//The fith node along the line, has it's pointer, *next, set equal to a null pointer
root->next->next->next->next = 0;
//Conductor is set to point to root
conductor=root;
//Prints all the values in the list
while(conductor!=NULL)
{
cout<<conductor->value<<" ";
conductor=conductor->next;
}
cout<<"\n";
cin.get();
return 0;
}
Last edited by WingedPanther; 07-16-2008 at 12:47 PM. Reason: add code tags |
|
|||
|
Code:
#include<iostream>
using namespace std;
//Defines the node
struct node
{
//An integer
int value;
//A node pointer
node *next;
};
int main(void)
{
//Declares the first, unchanging node
node *root;
//Declares the "conductor" node which will traverse down the list
node *conductor;
//The first node, root, points to a new node
root = new node;
//Sets the value of the nodes
root->value=1;
root->next = new node;
root->next->value=2;
root->next->next = new node;
root->next->next->value=3;
root->next->next->next = new node;
root->next->next->next->value=4;
//The fith node along the line, has it's pointer, *next, set equal to a null pointer
root->next->next->next->next = 0;
//Conductor is set to point to root
conductor=root;
//Prints all the values in the list
while ( conductor!=NULL )
{
cout<<conductor->value<<" ";
conductor=conductor->next;
}
cout<<"\n";
cin.get();
return 0;
}
|
| Sponsored Links |
|
|
|
|||
|
Code:
#include<iostream>
using namespace std;
//Defines the node
struct node
{
//An integer
int value;
//A node pointer
node *next;
};
int main(void)
{
//Declares the first, unchanging node
node *root;
//Declares the "conductor" node which will traverse down the list
node *conductor;
//The first node, root, points to a new node
root = new node;
//Sets the value of the nodes
root->value=1;
root->next = new node;
root->next->value=2;
root->next->next = new node;
root->next->next->value=3;
root->next->next->next = new node;
root->next->next->next->value=4;
//The fith node along the line, has it's pointer, *next, set equal to a null pointer
root->next->next->next->next = 0;
//Conductor is set to point to root
conductor=root;
//Prints all the values in the list
while ( conductor!=NULL )
{
cout<<conductor->value<<" ";
conductor=conductor->next;
}
cout<<"\n";
cin.get();
delete root->next->next->next;
delete root->next->next;
delete root->next;
delete root;
return 0;
}
|
|
|||
|
It's just good practice. Like malloc and free in C.
|
![]() |
| Tags |
| c++, linked, list, nodes, structure |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Starting C# with C# 2008 Express Edition | Jordan | CSharp Tutorials | 19 | 08-08-2008 01:48 PM |
| Whats the use of linked list? | the_transltr | C and C++ | 6 | 03-29-2008 10:24 PM |
| Linked list | borny86 | Pascal/Delphi | 9 | 12-06-2007 12:42 PM |
| adding new nodes in a linked list while looping | emda321 | C and C++ | 2 | 06-18-2007 03:27 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |