Link to home
Start Free TrialLog in
Avatar of chasa
chasa

asked on

Help with linked list input.

 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.

      }
ASKER CERTIFIED SOLUTION
Avatar of ElmerFud
ElmerFud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ElmerFud
ElmerFud

#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
This is musch easier when you learn classes and recursion.  Your teacher doesn't sound like he's teaching you very well.
Avatar of chasa

ASKER

 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
Are you going to grade my answer and give me the points?
Oops never mind.  :)