here is the entire source code. there are no errors, but what it is outputting is not what i want.
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:
#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
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;
Comment