linked list...no error, but not the right information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    linked list...no error, but not the right information

    here is the entire source code. there are no errors, but what it is outputting is not what i want.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    
    typedef struct linknode{
    	int data1;
    	char data2;
    	linknode *p;
    } nodetype;
    
    int listsize(linknode *p1){ 
    	int size=0;
    	nodetype t1;
    	p1=&t1;
    	t1.data1=55;
    	t1.data2='A';
    	cout << "\n\n\t" << t1.data1 << t1.data2;
    	nodetype *t2;
    	t2=new nodetype;
    		t1.p=t2;
    		t2->p = NULL;
    	(*t2).data1=66;
    	t2->data2='B';
    	cout << "\n\n\t" << t2->data1 << t2->data2;
    	for(;;){      
    		if(p1==NULL) break;
    		p1 = p1->p;
    		size++;
    		}
    	cout << "\n\n\tList size = " << size;
    
    return size;}  
    
    int printlist(linknode *p1, int size1){
    	for(int i=0;i<size1; i++){  
    	 cout << p1->data1 << p1->data2 << "   ";
    	 p1=p1->p;
    		}
    return 0;}
    
    int printall(linknode *p1){
    	
    	for(;;){
    		cout << p1->data1 << p1->data2 << "   ";
    		if((p1->p)==NULL) break;
    	return 0;
    		}
    
    return 0;
    }
    
    int main(){
       clrscr();
    	linknode *go,*copygo;
    	int i,j;
    	linknode *start1;
    	
    	go=start1;
    	copygo=go;
    	listsize(go);
    	getch();
    	go=copygo;
    	printlist(go,2);
    	getch();
    	printall(go);
    
    
       cout << "\n\tComplete";
    
           return 0;
    } //MAIN
    listsize prints out what i want it to, which is how big the actual list is, but the other 2 dont. printlist is supposed to print out the data, but it just prints out 0 and some other random crazy number and print all just prints 0.

    all of this code worked fine in the main, but im trying to transition them to functions to make the main cleaner, i don't want to use a class either. help is greatly appreciated.

    --

    here is the working code for the non-function work.

    Code:
    typedef struct linknode{
    	int data1;
    	char data2;
    	linknode *p;
    } nodetype;
    
    int main(){
       clrscr();
    	linknode *go,*copygo;
    	int i,j;
    		linknode *start1;
    	nodetype t1;
    		start1 = &t1;
    	t1.data1=55;
    	t1.data2='A';
    	cout << "\n\n\t" << t1.data1 << t1.data2;
    	
    	nodetype *t2;
    	t2=new nodetype;
    		t1.p=t2;
    		t2->p = NULL;
    	(*t2).data1=66;
    	t2->data2='B';
    	cout << "\n\n\t" << t2->data1 << t2->data2;
    
    
    	go=start1;
    	copygo=go;
    	int size=0; 
    	for(;;){
    		if(go==NULL) break;
    		go = go->p;
    		size++;
    		}
    	cout << "\n\n\tList size = " << size;
        
    	go=copygo;
    	cout << "\n\n\t This list content(s): ";
          for(i=0;i<size; i++){  // print list
    	 cout << go->data1 << go->data2 << "   ";
    	 go=go->p;
    		}
    
    	delete t2;
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Well, to start with, in main at line 60, the variable start1 is never initialized. At best it will be implicitly initialized to 0 (NULL); at worst, it'll have a random memory address.

    There's a lot of extraneous (I assume debug) code in your function listsize (line 15). You can probably delete lines 17 through 28 benignly.

    Your call to printlist should fail, because the value of copygo is the same as start1, which is to say, it's indeterminate.

    And as for your function printall, you have a return in the middle of the output loop, which limits any output to at most one item.

    Hopefully that helps.

    Cheers!

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      So you really need to be using <iostream.h> and <fstream.h>., etc?

      Those are non-ANSI C++ headers from before 1998- and they are buggy.

      You should be using <iostream> and <fstream>. Today all .h headers are C headers.

      Comment

      Working...