c++ calling link lists in main

i want to convert this main to just a series of random numbers that i can call in a function above the main.

this is some of the code in the main, which i use go to put in the functions above.


1
2
3
4
5
6
7
8
9
10
11
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;


i want to make it so i don't have to put the random(1000) in this function, and instead i can call the random numbers in the main

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
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;
}


this is the struct.

1
2
3
4
typedef struct linknode{
    int data1;
    linknode *p;
} nodetype;
Is this C or C++? You've got a lot of mixed language constructs there. You use iostreams, and then you do a typedef struct. struct is a type declaration in C++. It's a bit confusing.

Why use an infinite loop at line 13 rather than construct a proper for loop?

I recommend using more whitespace: proper indentation, spaces between operators, etc. And consider either putting underscores between words in your variable names (start_list, node_type, etc) or use CamelCase names (NodeType, startList, firstOne, etc). Legibility matters, especially when you ask others to read and comprehend your code.

I believe in year 2010, Standard C++ STL libraries has matured so much that we can safely remove the need to write our own linked list, doubly-linked list etc data structure. Unless our job is to work for companies that sell C++ STL then we can just use those STL containers direct isn't it ?

Yet I still see lots of code that go back way to the C days. Just a few thots.
this is c++

also on line 13 i used an infinite loop because it was a test to see if the size constraints worked in the loop. notice (size1==10)

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
int start_list(node_type *p1){
    int size1=0; int in1;
    node_type *temp; 
        in1=random(1000);
       cout << "\n\tRandom Numbers:   ";
       cout << in1 << "  ";
    node_type *firstone=new node_type;
        firstone->data1=in1;
    p1=firstone;
    temp=firstone;

      for(;;){
    node_type *new1 = new node_type;
    (temp->p)=new1;
    temp=new1;
    
        in1=random(1000); 
       cout << in1 << "   ";
    new1->data1=in1;
    size1++; 
      if(size1==10) break;
        }    
    temp->p=NULL;

return size1;
}


int main(){
   clrscr();
    link_node *start1;
    link_node *go; 
    node_type t1;
    start1=&t1;
    t1.data1=55;
    node_type *t2;
    t2=new node_type;
    t2->data1=66;
    t1.p=t2;
    t2->p=NULL;

       go=start1;
    start_list(go);


basically, what i'm trying to do is get a list of random numbers from the main, like i did with (t1.data=55,t2->data=66), but with random numbers.


hopefully this is easier for you to read. i was just showing you the struct for reference.
this is c++

I don't see any C++ code here other than the use of cout and new.

Consider making nodetype a class with a proper constructor and insert operator.
C++ is about data abstraction, which means hiding the implementation of the linked list.
data1 and linknode should be private members of the class. When you modify members outside of the class, you make things harder to understand and harder to debug. i.e. main should not know anything about members of nodetype.

To get you started:

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
class NODE
{  
private: 
    int         m_data1;
    NODE * m_next;
public:
    // Construct a node from a value
     NODE (int val)
     {  m_data1 = val;
         m_next = NULL;
     }
    // Insert a node after the current node
    void Insert (NODE * node)
    {  node->m_next = this->m_next;
        this->m_next = node;
    }
};

int main ()
{   NODE * root;
     NODE * temp;
     root = new NODE(55);
     temp = new NODE (66);
     root->Insert (temp);
     start_list (root);
}        


edit:
BTW: You already have an active thread on this problem here:
http://www.cplusplus.com/forum/general/28098/




Last edited on
Topic archived. No new replies allowed.