Re: Sorted Linked List
From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 05/24/04
- Next message: Jeff Relf: "Re: Editors"
- Previous message: Pmb: "Re: Editors"
- In reply to: massimo: "Re: Sorted Linked List"
- Next in thread: massimo: "Re: Sorted Linked List - Final working version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jeff Relf: "Re: Editors"
- Previous message: Pmb: "Re: Editors"
- In reply to: massimo: "Re: Sorted Linked List"
- Next in thread: massimo: "Re: Sorted Linked List - Final working version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|