c++ linked pass out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>


typedef struct linknode{
    int data1;
    linknode *p;
} nodetype;

int startlist(nodetype *p1){
    int size1=0; int in1;

    nodetype *temp; 
    in1=random(1000);
    cout << "\n\tRandom Numbers:   ";
    cout << in1 << "  ";
    nodetype *firstone= new nodetype;
    firstone->data1=in1;
    p1=firstone;
    temp=firstone;

    for(;;){
    nodetype *new1 = new nodetype;
    (temp->p)=new1;
    temp=new1;
    
    in1=random(1000); 
    cout << in1 << "   ";
    new1->data1=in1;
     size1++;
    if(size1==10) break;
        }    
    temp->p=NULL;

return size1;
}

int addNode(nodetype *p1, int pos, int value){
    // if((pos>size) || (pos<1)) error return 1;    
    // else
    int i;
    nodetype *n=new nodetype;
    n->data1=value;
    nodetype *temp=p1;

    if(pos==1){  n->p=p1; 
            p1=n;   }

      else{
     for(i=0; i<pos-2; i++) temp=temp->p;
    n->p = temp->p; temp->p=n;
    }
return 0;
}    

    
int listsize(linknode *p1){ 
    int size=0;
    for(;;){      
        if(p1==NULL) break;
        p1=p1->p;
        size++;
        }
    cout << "\n\n\tList size = " << size;

return size;}  

int printlist(linknode *p1, int size1){
    cout << "\n\n\tList Content(s):  ";
    for(int i=0;i<size1; i++){  
     cout << p1->data1 << "   ";
      p1=p1->p;
        }
return 0;}

int printall(linknode *p1){
        for(;;){
        if(p1==NULL) break;
        cout << "\n\n\t";
        cout << p1->data1 << "   ";
        p1 = p1->p;    
      }
return 0;
}

int main(){
   clrscr();
    nodetype *head;
    startlist(head);
    printall(head);
    addNode(head,5, 10);
    listsize(head);
    printlist(head,2);
    cout << "\n\n\t";
    

   cout << "\n\tComplete";

       return 0;
} //MAIN 


i'm trying to get the random numbers i used in startlist(head) to pass out through the main and move onto printall, but once i print out printall(head) it just prints 0, then freezes.

any help is appreciated.
head points to garbage because you pass it by value to startlist
how would i fix that?
Initialize it, that's all.
head = new nodetype;

-Albatross
i entered that and it ran, but when i do that i get a printout of

51373 1
- forever, is there a reason the for loop doesn't stop? it would be this line of code

1
2
3
4
5
6
7
8
int printall(linknode *p1){
		for(;;){
		if(p1==NULL) break;
		cout << p1->data1 << "   ";
		p1 = p1->p;	
      }
return 0;
}
Eh... yes. That would run until you hit a p1 that is truly null. Try manually setting one of them to null and see what that does.

-Albatross
yeah i still doesn't work

this is the program output for the random numbers btw...is there anything u can pick up on that would make it do this? p1 should be the string of random numbers correct?

Random Numbers:   549  604   986   944   712   283   556   734   286   861   761
If you modify something in a function, pass it by reference (or return the value)
int startlist(nodetype *&p1);

To use what Albatross suggested, note that you already have a header so
1
2
3
4
5
6
7
8
9
int startlist(nodetype *p1){
//p1 is the firstone
    /*nodetype *firstone= new nodetype; 
    firstone->data1=in1;
    p1=firstone;*/
   p1->data=in1;
    temp=p1;
//...
}
Last edited on
Topic archived. No new replies allowed.