By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <string.h>
  7.  
  8.  
  9. typedef struct linknode{
  10.     int data1;
  11.     char data2;
  12.     linknode *p;
  13. } nodetype;
  14.  
  15. int listsize(linknode *p1){ 
  16.     int size=0;
  17.     nodetype t1;
  18.     p1=&t1;
  19.     t1.data1=55;
  20.     t1.data2='A';
  21.     cout << "\n\n\t" << t1.data1 << t1.data2;
  22.     nodetype *t2;
  23.     t2=new nodetype;
  24.         t1.p=t2;
  25.         t2->p = NULL;
  26.     (*t2).data1=66;
  27.     t2->data2='B';
  28.     cout << "\n\n\t" << t2->data1 << t2->data2;
  29.     for(;;){      
  30.         if(p1==NULL) break;
  31.         p1 = p1->p;
  32.         size++;
  33.         }
  34.     cout << "\n\n\tList size = " << size;
  35.  
  36. return size;}  
  37.  
  38. int printlist(linknode *p1, int size1){
  39.     for(int i=0;i<size1; i++){  
  40.      cout << p1->data1 << p1->data2 << "   ";
  41.      p1=p1->p;
  42.         }
  43. return 0;}
  44.  
  45. int printall(linknode *p1){
  46.  
  47.     for(;;){
  48.         cout << p1->data1 << p1->data2 << "   ";
  49.         if((p1->p)==NULL) break;
  50.     return 0;
  51.         }
  52.  
  53. return 0;
  54. }
  55.  
  56. int main(){
  57.    clrscr();
  58.     linknode *go,*copygo;
  59.     int i,j;
  60.     linknode *start1;
  61.  
  62.     go=start1;
  63.     copygo=go;
  64.     listsize(go);
  65.     getch();
  66.     go=copygo;
  67.     printlist(go,2);
  68.     getch();
  69.     printall(go);
  70.  
  71.  
  72.    cout << "\n\tComplete";
  73.  
  74.        return 0;
  75. } //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.

Expand|Select|Wrap|Line Numbers
  1. typedef struct linknode{
  2.     int data1;
  3.     char data2;
  4.     linknode *p;
  5. } nodetype;
  6.  
  7. int main(){
  8.    clrscr();
  9.     linknode *go,*copygo;
  10.     int i,j;
  11.         linknode *start1;
  12.     nodetype t1;
  13.         start1 = &t1;
  14.     t1.data1=55;
  15.     t1.data2='A';
  16.     cout << "\n\n\t" << t1.data1 << t1.data2;
  17.  
  18.     nodetype *t2;
  19.     t2=new nodetype;
  20.         t1.p=t2;
  21.         t2->p = NULL;
  22.     (*t2).data1=66;
  23.     t2->data2='B';
  24.     cout << "\n\n\t" << t2->data1 << t2->data2;
  25.  
  26.  
  27.     go=start1;
  28.     copygo=go;
  29.     int size=0; 
  30.     for(;;){
  31.         if(go==NULL) break;
  32.         go = go->p;
  33.         size++;
  34.         }
  35.     cout << "\n\n\tList size = " << size;
  36.  
  37.     go=copygo;
  38.     cout << "\n\n\t This list content(s): ";
  39.       for(i=0;i<size; i++){  // print list
  40.      cout << go->data1 << go->data2 << "   ";
  41.      go=go->p;
  42.         }
  43.  
  44.     delete t2;
Sep 2 '10 #1
Share this Question
Share on Google+
2 Replies


Oralloy
Expert 100+
P: 983
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!
Sep 2 '10 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 2 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.