473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.

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
0 1187

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

Similar topics

6
by: Ash Lux | last post by:
Does the C++ vector template class use pointer-based link lists? Could I get a link explaining how it works, regardless of what it uses? Thank You, Ash Lux
1
by: dmattis | last post by:
I am trying to create a linked list of 15 random numbers. The list is to be populated by inserting new nodes from the front. Can someone give me an example of how to do this? I'm not sure where...
12
by: onkar | last post by:
sometimes main accepts int main() and sometimes int main(int argc,char** argv) and sometimes int main(int argc,char **argv,char** env) still the code complies properly How is this possible ???...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
3
by: ericstein81 | last post by:
I have written a program and am having trouble with the bubblesort... i get very confused on it and am not too familiar with link lists in the first place. The link list runs succesfully when i dont...
2
by: Ron Eggler | last post by:
Hi, I would like to call a link in a frame every second but with a different GET parameter to recognize the time, how long a user has been on the page, i did this with following script: ...
4
by: valt | last post by:
public class DLLNode { String Item; int Time; DLLNode Next; DLLNode Prev; public DLLNode(String newItem,int newTime,DLLNode newNext,DLLNode newPrev) { Item = newItem;
5
by: johnnash | last post by:
i'm declaring a data structure for link list of integers in A.h #ifndef A_H (can anyone please explain how ifndef works as well..i just seem to see it in almost every program) #define A_H ...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.