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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    #1

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

    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.

    Code:
    #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
        nodetype *n=new nodetype;
        n->data1=value;
        nodetype *temp=p1;
    
        if(pos==1){  n->p=p1; 
                p1=n;   }
    
          else{
        // for(int i=0; pos-2;) 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 << p1->data1 << "   ";
            p1 = p1->p;
            }
    
    return 0;
    }
    
    int main(){
       clrscr();
        linknode *start1;
        linknode *go;
        nodetype t1;
        start1=&t1;
        t1.data1=55;
        nodetype *t2;
        t2=new nodetype;
        t2->data1=66;
        t1.p=t2;
        t2->p=NULL;
        go=start1;
    
        startlist(go);
        addNode(go,10,5);
        listsize(go);
        printlist(go,2);
        cout << "\n\n\t";
        printall(go);
    
        delete t2;
    
       cout << "\n\tComplete";
    
           return 0;
    } //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.
Working...