Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with linked list input.

Posted on 1998-06-03
6
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

722 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