Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with linked list input.

Posted on 1998-06-03
6
Medium Priority
?
225 Views
Last Modified: 2013-12-14
 I don't quite get linked list.  My book is clear on how to sort , add and subtract things from a list but I can't figure out how to create a list form the input stream.  Here is my try at it.

 #include <iostream.h>
struct node
      { int data;
        node* next;
      };
      typedef  node* ptrtype;

      int main ()  {

      cout << " enter numbers to be inserted ( 0 to end ) " << flush;
      int data;
      cin >> data;
      ptrtype cur;
      ptrtype head;

      for ( cur = head; cur!= NULL || 0; cur = cur->next) ;
                cout << cur->data<< endl;
   return 0;

Thanks for your help.

      }
0
Comment
Question by:chasa
  • 5
6 Comments
 
LVL 1

Accepted Solution

by:
ElmerFud earned 200 total points
ID: 1165186
I have the answer just a minute
0
 
LVL 1

Expert Comment

by:ElmerFud
ID: 1165187
#include <iostream.h>
struct node
{
    int data;
    node* next;
};
typedef  node* ptrtype;
#define NULL 0

int main()
{
    cout << " enter numbers to be inserted ( 0 to end ) " << flush;

    ptrtype cur = NULL; /*you should always set your pointers to NULL if you try to access before using the new operator, your program WILL crash*/
    ptrtype head = NULL;

    int data;
    cin >> data;
    while(data != 0)
    {
        if(head != NULL)
        {
            head = new node;
            head->data = data;
            head->next = NULL;
            cur = head;
        }
        else
        {
            cur->next = new node;
            cur->next->next = NULL;
            cur->next->data = data;
            cur = cur->next;
        }
        cin >> data;
    }

    cout << "These are the number you typed:\n";
    cur = head;
    while(cur != NULL)
    {
        cout << cur->data << '\n';
        cur = cur->next;
    }
    return 0;
}

Now I bet you didn't learn a damn thing.  Shame on you going to expert's exchange to get you homework done. :P
0
 
LVL 1

Expert Comment

by:ElmerFud
ID: 1165188
This is musch easier when you learn classes and recursion.  Your teacher doesn't sound like he's teaching you very well.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chasa
ID: 1165189
 I only asked a part of the homework question.  Now I can put together the other 5 functions.
  The program did crash in the else statement but I did fix it

 Thanks
0
 
LVL 1

Expert Comment

by:ElmerFud
ID: 1165190
Are you going to grade my answer and give me the points?
0
 
LVL 1

Expert Comment

by:ElmerFud
ID: 1165191
Oops never mind.  :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question