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

c++ calling link lists in main..serious help :/

P: 25
i understand the concept of linked lists and how they work, but i cannot seem to grasp how to convert random numbers in the main to. if anyone can help fix my code up that would be really helpful.

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.     linknode *p;
  12. } nodetype;
  13.  
  14. int startlist(nodetype *p1){
  15.     int size1=0; int in1;
  16.  
  17.     nodetype *temp; 
  18.     in1=random(1000);
  19.     cout << "\n\tRandom Numbers:   ";
  20.     cout << in1 << "  ";
  21.     nodetype *firstone= new nodetype;
  22.     firstone->data1=in1;
  23.     p1=firstone;
  24.     temp=firstone;
  25.  
  26.     for(;;){
  27.     nodetype *new1 = new nodetype;
  28.     (temp->p)=new1;
  29.     temp=new1;
  30.  
  31.     in1=random(1000); 
  32.     cout << in1 << "   ";
  33.     new1->data1=in1;
  34.      size1++;
  35.     if(size1==10) break;
  36.         }    
  37.     temp->p=NULL;
  38.  
  39. return size1;
  40. }
  41.  
  42. int addNode(nodetype *p1, int pos, int value){
  43.     // if((pos>size) || (pos<1)) error return 1;    
  44.     // else
  45.     nodetype *n=new nodetype;
  46.     n->data1=value;
  47.     nodetype *temp=p1;
  48.  
  49.     if(pos==1){  n->p=p1; 
  50.             p1=n;   }
  51.  
  52.       else{
  53.     // for(int i=0; pos-2;) temp=temp->p;
  54.     n->p = temp->p; temp->p=n;
  55.     }
  56. return 0;
  57. }    
  58.  
  59. int listsize(linknode *p1){ 
  60.     int size=0;
  61.     for(;;){      
  62.         if(p1==NULL) break;
  63.         p1=p1->p;
  64.         size++;
  65.         }
  66.     cout << "\n\n\tList size = " << size;
  67.  
  68. return size;}  
  69.  
  70. int printlist(linknode *p1, int size1){
  71.     cout << "\n\n\tList Content(s):  ";
  72.     for(int i=0;i<size1; i++){  
  73.      cout << p1->data1 << "   ";
  74.       p1=p1->p;
  75.         }
  76. return 0;}
  77.  
  78. int printall(linknode *p1){
  79.     for(;;){
  80.         if(p1==NULL) break;
  81.         cout << p1->data1 << "   ";
  82.         p1 = p1->p;
  83.         }
  84.  
  85. return 0;
  86. }
  87.  
  88. int main(){
  89.    clrscr();
  90.     linknode *start1;
  91.     linknode *go;
  92.     nodetype t1;
  93.     start1=&t1;
  94.     t1.data1=55;
  95.     nodetype *t2;
  96.     t2=new nodetype;
  97.     t2->data1=66;
  98.     t1.p=t2;
  99.     t2->p=NULL;
  100.     go=start1;
  101.  
  102.     startlist(go);
  103.     addNode(go,10,5);
  104.     listsize(go);
  105.     printlist(go,2);
  106.     cout << "\n\n\t";
  107.     printall(go);
  108.  
  109.     delete t2;
  110.  
  111.    cout << "\n\tComplete";
  112.  
  113.        return 0;
  114. } //MAIN
here is my entire source code for this program. its pretty messed up, but it works. i've been building from 1 program to the next. the original program was to just use t1 to print the integers and now i want to replace those few numbers with random numbers, or at least add them and use the function addnode, which would add another node in the program. i also don't know how to construct the for loop that i commented out. i need to have it -2 from the pos.

thanks alot in advance.
Sep 14 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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