what is wrong is this link list?

This is a discussion on what is wrong is this link list? within the C++ Programming forums, part of the General Programming Boards category; /* can yoou please tell me what is wrong with this linklist? i think it is overiding during my read.But ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    4

    Talking what is wrong is this link list?

    /*
    can yoou please tell me what is wrong with this linklist? i think it is overiding during my read.But how to fix it?

    */

    // Created by: Pierre-Michel Jean-Louis
    // Date: Febuary 29th 2003
    // File name: exercise2.cc
    // Purpose: Reads linklist to a file
    // input: list.txt
    // output: onscreen

    # include <iostream.h>
    # include <fstream.h>
    # include <iomanip.h>
    # include <stdlib.h>
    # include <string.h>
    # include <ctype.h>
    # include <stddef.h>
    # include <math.h>

    struct nodetype;
    typedef int itemtype;
    typedef nodetype * ptrtype;


    struct nodetype
    {
    itemtype item;
    ptrtype link;
    };


    int main()
    {
    ptrtype head;
    ptrtype newp;
    ptrtype currentp;
    itemtype temp;
    ifstream file;
    file.open("list.txt");
    head = new nodetype;
    file >> head->item;
    if (file)
    {
    currentp = head;
    file >> temp;

    while (file)
    {
    newp = new nodetype;
    newp->item = temp;
    currentp->link = newp;
    file >> temp;
    }
    currentp->link = NULL;
    }
    else
    head = NULL;
    currentp = head;
    while(currentp != NULL)
    {
    cout << currentp->item <<endl;
    currentp = currentp->item;
    }




    return 0;
    }

  2. #2
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Re: what is wrong is this link list?

    Code:
    /*
    can yoou please tell me what is wrong with this linklist? i think it is overiding during my read.But how to fix it?
    
    */
    
    // Created by: 	Pierre-Michel Jean-Louis
    // Date:	Febuary 29th 2003
    // File name: 	exercise2.cc
    // Purpose:	Reads linklist to a file 
    // input:	list.txt
    // output:	onscreen
    
    # include <iostream.h>
    # include <fstream.h>
    
    struct nodetype;
    typedef int itemtype;
    typedef nodetype * ptrtype;
    
    
    struct nodetype
    {
    	itemtype item;
    	ptrtype link;
    };
    
    
    int main()
    {
    	ptrtype head, newp, currentp;
    	itemtype temp;
    	ifstream file;  
    
    	file.open("list.txt");
    
    	head = new nodetype;
    	file >> head->item;
    	if (file)
    	{
    		currentp = head;
    		file >> temp;
    		
    		while (file)
    		{
    			newp = new nodetype;
    			newp->item = temp;
    			currentp->link = newp;
    			file >> temp;
    		}
    		currentp->link = NULL;
    	}
    	else 
    		head = NULL;
    	currentp = head;
    	while(currentp != NULL)
    	{
    		cout << currentp->item <<endl;
    		currentp = currentp->item;
    	}
    		
    	
    
    	
    	return 0;
    }
    May I ask why you need all those headers? As far as I can tell, all you need is iostream and fstream... perhaps even just fstream, as it includes iostream.
    Last edited by carrja99; 02-28-2003 at 12:25 PM.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    you never closed the file

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    int main()
    {
      ptrtype head;
      itemtype temp;
      head = new nodetype;
      file >> head->item;
      if (file)
      {
         currentp = head;
         file >> temp;
    In the code snippet above neither temp nor head->item are initialized to a valid value before you try to write them to the file. You are writing junk to the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  4. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  5. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21