Jump to content

C++ - Need help with a linked list program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Ratchet2246

Ratchet2246

    Newbie

  • Members
  • PipPip
  • 12 posts
The 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;
}
The Problem
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

Edited by WingedPanther, 15 July 2008 - 07:58 AM.
add code tags


#2
Oigen

Oigen

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
I don't know why that happens. Maybe you're asking something that's not compatible with Vista?

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Ratchet2246

Ratchet2246

    Newbie

  • Members
  • PipPip
  • 12 posts
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:
#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;
}

Edited by WingedPanther, 16 July 2008 - 08:47 AM.
add code tags


#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
#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;

   [COLOR="Blue"]root->next = new node;[/COLOR]

   root->next->value=2;

   [COLOR="Blue"]root->next->next = new node;[/COLOR]

   root->next->next->value=3;

   [COLOR="Blue"]root->next->next->next = new node;[/COLOR]

   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;

}
Don't forget to delete what you new.

#6
Ratchet2246

Ratchet2246

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks a lot this has cleared up everything.
By the way, how does this delete thing work, it's not something I'm familiar with.

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
#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();
   [COLOR="Blue"]delete root->next->next->next;
   delete root->next->next;
   delete root->next;
   delete root;[/COLOR]
   return 0;
}


#8
Ratchet2246

Ratchet2246

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks, I've done that, but why is it neccesary?

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
It's just good practice. Like malloc and free in C.

#10
Ratchet2246

Ratchet2246

    Newbie

  • Members
  • PipPip
  • 12 posts
Right, thanks.