-2

So I have to make a program that stores input piece by piece, until -1 is input. An example of this input would be 1 1 1 0 3 5 3 -1. And -1 would not be recorded. Then it plays the list back to you, but bacwards, so output would be 3 5 3 0 1 1 1

To do this I need to make a list of objects, but I have no clue how to declare them, or how to add/remove items to them. how do I do this?

Flotolk
  • 313
  • 1
  • 11
  • 37

6 Answers6

2

You need a place to store values that can grow as values are read.

The simpler is probably std::vector, but also std::list or std::deque as well as whatever bidirectional container will do the game.

Then you need a loop to get the values an save them into the container (the push_back method has that purpose), and another loop getting the values from the container from the end and printing them.

This can be done using iterators or using indexes, depending on the container and on your own specific needs.

This may be a possibility:

#include <vector>
#include <iostream>

template<class V, class S>
void read_numbers(V& v, S& s, N end)
{
    N x=0;
    while(x << s)
    {
        if(x==end) return;
        v.push_back(x);
    }
    std::cerr << "error: bad reading" << std::endl;
}

template<class V, class S>
void write_numbers(const V& v, S& s)
{
    for(auto i=s.rbegin(); i!=s.rend(); ++i)
        std::cout << *i << ' ';
    std::cout << std::endl;
}

int main()
{
    std::vector<int> nums;
    read_numbers(nums, std::cin, -1); }
    write_numbers(nums, std::cout);
    return 0;
}
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
1

For a list of unknown size, you can declare a vector class:

  std::vector<int> myVector;

Then to add an element, use push_back:

  // assign some integer to myInt
   myVector.push_back (myInt);

http://www.cplusplus.com/reference/vector/vector/push_back/

LazerSharks
  • 3,089
  • 4
  • 42
  • 67
1

If you need to use a list, then you could use std::list. You can declare them like this:

std::list<int> myList;

You can read more about std::list here.

phantom
  • 3,292
  • 13
  • 21
  • Why not `std::vector`? Seems like the obvious choice. – juanchopanza Apr 25 '15 at 16:00
  • 1
    That's the most accurate answer, he said he needs a list, so a list it is. Although I'm pretty sure OP is expected to implement one himself, but he should have pointed it out. – Leeor Apr 25 '15 at 16:01
  • @Leeor What BS. Just because a container happens to be called `std::list` doesn't mean it is the thing OP is after. – juanchopanza Apr 25 '15 at 16:03
  • @juanchopanza, OP didn't say what he's after, and since he's learning c++, he probably isn't after *any* library implementation, so the question should be closed. But given the restrictions above, std list is just as good and utterly useless for him as the rest. – Leeor Apr 25 '15 at 16:16
0

You can use it for inspiration.

#include <vector>
#include <iostream>

int main(int argc, char * argv[]) {
    std::vector<int> numbers;
    int number;

    do {
        std::cin >> number;
        if (number != -1) {
            numbers.push_back(number);
        }
    } while(number != -1);

    for (std::vector<int>::reverse_iterator it = numbers.rbegin(); it != numbers.rend(); ++it) {
        std::cout << *it << " ";
    }

    std::cin.get();
    return 0;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Krab
  • 6,526
  • 6
  • 41
  • 78
0

As was suggested already, you could use std::vector. It resizes as you push more elements. If the requirement is strictly a linked list, then go for std::list.

As to the requirement of playing them in reverse order, just use reverse_iterator and go through the list printing them.

std::list<int> mylist;
// Insert the elements.
for (std::list<int>::reverse_iterator rIt = mylist.rbegin();
     rIt != mylist.rend(); ++rIt) {
   cout << *rIt;
}
KalyanS
  • 527
  • 3
  • 8
-1

There are basically two options:

  1. You can use a pointer int *data and manually allocate memory to it with new[]. You'll need to continually reallocate the array as you find that the array is larger than you expected.

  2. You can use one of the wonderful containers that c++ provides. I might suggest std::vector.

Here's an example of how you might implement this:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> data;

    while (true) {
        int number;
        std::cin >> number;
        if (!std::cin)
            break;
        if (number == -1)
            break;
        data.push_back(number);
    }

    for (int datum : data)
        std::cout << datum << " ";
    std::cout << "\n";
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • 1
    malloc / realloc ?? in C11 at A.D. 2015 ? – Emilio Garavaglia Apr 25 '15 at 15:58
  • @hyde: It's mostly a reflection of the lack of a realloc() alternative... – Bill Lynch Apr 25 '15 at 15:59
  • Well, realloc is theoretical optimization (in some situations) over just doing new allocation, and legacy C stuff IMO is not something a novice C++ programmer should learn, while still learning just programming in general (like seems to be the case here). Anyway, my downvote retracted. – hyde Apr 25 '15 at 16:07