Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-2008, 08:46 AM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default C++ - Need help with a linked list program

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;
}
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

Last edited by WingedPanther; 07-15-2008 at 11:58 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-15-2008, 08:58 AM
Oigen Oigen is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 40
Rep Power: 2
Oigen is on a distinguished road
Default Re: C++ - Need help with a linked list program

I don't know why that happens. Maybe you're asking something that's not compatible with Vista?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-15-2008, 12:01 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,405
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: C++ - Need help with a linked list program

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 03:03 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default Re: C++ - Need help with a linked list program

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-15-2008, 03:11 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: C++ - Need help with a linked list program

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;
}
Don't forget to delete what you new.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-15-2008, 03:21 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default Re: C++ - Need help with a linked list program

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-15-2008, 03:25 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: C++ - Need help with a linked list program

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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-15-2008, 03:31 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default Re: C++ - Need help with a linked list program

Thanks, I've done that, but why is it neccesary?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-15-2008, 03:42 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: C++ - Need help with a linked list program

It's just good practice. Like malloc and free in C.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-15-2008, 03:45 PM
Ratchet2246 Ratchet2246 is offline
Newbie
 
Join Date: Jul 2008
Location: Moray, Scotland
Age: 14
Posts: 12
Rep Power: 0
Ratchet2246 is on a distinguished road
Default Re: C++ - Need help with a linked list program

Right, thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply

Tags
c++, linked, list, nodes, structure



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 09:44 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads