Now make a program so that it stores the values in increasing order, i.e., each time a value is added it is placed into its proper position in the list.

Here is my code, I cant get it to put them in increasing order.

#include <iostream>

using namespace std;

struct ListNode
{
    int data;
    ListNode *next;
    ListNode(int d, ListNode *next1 = NULL)
    {
        data = d;
        next = next1;
    }
};

int main()
{
    ListNode *numberList = NULL;
    int size, number, check;
    int yes = 0;

    cout << "How many intergers are going to be entered ";//asks for how many numers
    cin >> size;

    for (int count = 0; count < size; count++)
    {
        cout << "Enter an integer " << count + 1 << ": ";//enter a numbrr
        cin >> number;
        numberList = new ListNode(number, numberList);
    }

    cout << "What integer do you want to check from the list ";//enter numebr from list
    cin >> check;

    cout << "The list contains\n";//tells the list
    ListNode *ptr = numberList;
    while (ptr != NULL)
    {
        if (ptr->data == check)
        {
            yes = 1;
        }
        cout << ptr->data;
        cout << "  ";
        ptr = ptr->next;
    }

    if (yes == 1)
    {
        cout << "\n\n" << check << " is in the list.\n";//if the entered number is in list
    }
    else
    {
        cout << "\n\n" << check << " is not in the list.\n";//if entered number is not in list
    }

    return 0;
}

Your ListNode is ok but you still need a list :).So here's an example of how to create one and add a member node to it.

#include <iostream>

struct ListNode  {
   ListNode* next;
   int data;
   ListNode (int data_=0) : data(data_), next(NULL) {}
   ~ListNode () {}
 };

struct List  {
   ListNode* head;  //a list should have a point at which you can begin operations
   ListNode** last; //remember the previous allocated node
   int Size;        //you need a member for counting the nodes 

   List () : Size(0), head(&ListNode())  {
      last=&head;       //last's initial position is same as head's
      head->next=head;  //close the loop
    }

   void Insert (ListNode node)  {            
      (*last)->next=new ListNode(node);  //link the new node to the last
      (*last)->next->next=head;   //link the new node to the next which is the head
      last=&(*last)->next;   //make 'last' point the new allocated node
      Size++;   
      //now you have a circular array of nodes
    }

   void Print ()  {
      ListNode* fwditer=head->next;
      while (fwditer!=head)  {
         std::cout << fwditer->data;
         fwditer=fwditer->next; 
       }
    }
 };

int main ()  { 
   List l;
   l.Insert (ListNode (5));   
   l.Print();
 }

I'm curious why you do not use std::list . My only guess is that this must be a homework requirement. If you were using a std::list you could just insert and then sort. Given that, you might want to try to write an insert function that adds a single item to the list and a sort function that will re-order the list properly. Separating the two will allow you to insert multiple elements and then sort at the end.

This article has been dead for over six months. Start a new discussion instead.