Solved

Help with linked list input.

Posted on 1998-06-03
6
222 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 100 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

627 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