Re: Sorted Linked List

From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 05/24/04


Date: Mon, 24 May 2004 20:46:59 GMT

massimo wrote:
> Hey john,
>
> Thank you very much. I totally understand the checking/sorting algorithm.
>
> I debugged the program and it compiles fine. I still believe I have some
> problem with the display() and main(); I'm doing something wrong and I don't
> see it!!
> When it compiles it doesn't prompt me and if I enter the numbers, it just
> return "Enter a number" for as many numbers I entered.
> How do I space the number??
> Let's say I want to enter:
> 12 34 56 23 45 33 and the on enter I want the program to spit out the sorted
> list.
[snip]

My suggestion is to write a small main() function that reads in the
numbers then spits them out:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

typedef vector<int> Vector_Of_Ints;

int main(void)
{
     // Create a container for the numbers.
     Vector_Of_Ints numbers;

     int users_number;

     // Read in the numbers until EOF or error:
     while (cin >> users_number)
     {
         numbers.push_back(users_number);
     }

     // Now to sort them:
     sort(numbers.begin(), numbers.end());

     // After sorting, spit them out.
     for (unsigned int i = 0; i < numbers.size(); ++i)
     {
        cout << " " << numbers[i];
     }
     cout << endl;

     return EXIT_SUCCESS;
}

The purpose of this program is to work out how you want to
enter the numbers. Once you have that working, add in calls
to your linked list system.

My understanding is that when you enter a lot of numbers on
one line:
     12 34 56 23 45 33
They will be placed into a buffer until you press Enter.
At that point, whenever a "cin >> number" is performed,
the next number in the buffer is given to the program.

When I ran the program, I had to supply my operating
system with an EOF character (like CTRL-D). It read
every number on the line.

-- 
Thomas Matthews
C++ newsgroup welcome message:
          http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq:   http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
          http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
     http://www.josuttis.com  -- C++ STL Library book


Relevant Pages

  • Re: Order of cpp files matters?
    ... > compiles, links and runs fine. ... some linkers only perform a single pass. ... C++ Faq: http://www.parashift.com/c++-faq-lite C Faq: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c++ faq: ... Other sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library ...
    (comp.lang.cpp)
  • Re: Unexpected Result
    ... > and helped me to learn whilst developing my yacht race classes. ... Everything compiles without fault; however, ... > output file the results were the same. ... > like a buffer that flushes the initial input upon saturation. ...
    (comp.lang.cpp)
  • Re: Attention Windows Users
    ... knowledge of buffer overrun exploits then. ... convenient way to exploit a buffer overrun. ... > compiles to assembly. ... Just as there are plenty of compiled BASICs, ...
    (rec.aviation.piloting)
  • Re: CREATE
    ... So, in effect, 5 CONSTANT FIVE compiles the equivalent of: ... Yours requires the extra space for the calls to LIT and EXIT. ... Most implementations have only the head, code address, and the data space. ... VARIABLE BUFFER 8 ALLOT \ 10 element array called BUFFER. ...
    (comp.lang.forth)
  • Re: Need some help with I/O
    ... > My textboox has less than a paragraph on the fgets() function ... > compiles and runs ok, it even builds a text file, but when I enter data ... > int main ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (alt.comp.lang.learn.c-cpp)