linked list...no errors, but not the right info

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

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
#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.

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
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;
May I ask, why you don't want a class?

Your error is that the pointer go points to garbage, because you don't modify it in the function that creates the list.
i haven't learned using classes and linked lists together. any help on correcting that error?
And just what is your C++ related question?
You do know, I hope, that C++ provides a linked list container so you don't have to write your own.
http://www.cplusplus.com/reference/stl/list/

Here are a few problems:
1) In the first listing, start1 is an uninitialized pointer.
In the second set of code, start1 is initialized at line 13.

2) In the function listsize, the pointer to the linknode is passed by value.
At line 13, you overlay whatever pointer value was passed.

3) In function listsize, t2 is allocated, and is never released. This will result in a memory leak each time listsize is called.


Last edited on
Topic archived. No new replies allowed.