Solved

Help with linked list input.

Posted on 1998-06-03
6
214 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 100 total points
Comment Utility
I have the answer just a minute
0
 
LVL 1

Expert Comment

by:ElmerFud
Comment Utility
#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
Comment Utility
This is musch easier when you learn classes and recursion.  Your teacher doesn't sound like he's teaching you very well.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:chasa
Comment Utility
 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
Comment Utility
Are you going to grade my answer and give me the points?
0
 
LVL 1

Expert Comment

by:ElmerFud
Comment Utility
Oops never mind.  :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now