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;
}

Recommended Answers

All 4 Replies

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.

You'll want to implement a variation of insertion sort while you're performing the insert operation.

Here is some information about the insertion sort algorithm.

Since this is a linked-list, it should be fairly easy to re-assign the link pointers to add a link to the list. The tricky part will be finding the correct links to insert between.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.