Linked list with multiple data types

I am trying to write a linked list that will store a type double(prices), a type string(product name), and an int(barcode). The basis for this is that I am making a concession stand simulation. I already have 2 classes one for a person (dynamic array) to allow a group of any size. Another class for them to go through a line (queue) and purchase tickets. My dilemma is that I want to be able to allow each person to buy any number of snacks (add, remove, total for each person and a group total). In terms of design I'm not sure which class should it be in, and the best way to implement. I would love just an explanation so I can try and code it myself(make my own mistakes) and figure it out. Thank you as always!
> I am trying to write a linked list that will store
> a type double(prices), a type string(product name), and an int(barcode).

Make it a class template a la std::list minus the allocator
https://en.cppreference.com/w/cpp/container/list

Or create a list type where everything is stored as a string
(use std::to_string, std::stoi and std::stod for conversions).
Last edited on
I made both of my classes template classes. My person class has template<class S> and my queue class template<class L>. So if I'm understanding you correctly and from the link I would have my struct take either type S or type L. That would allow me to add any data type in as a reference at that point? To complete the thought my struct would need (to be complete) node type for head, first and last. Then all my data variables either S or L?
I am still a little confused on how I would be able to use my pointer for each person(random access) and use the chaining of the linked list so I can also access the orders of which ever person I want? I hope I am explaining myself well enough sometimes I feel I am not
Are you looking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <list>

struct order_item
{
    std::string name ;
    int quantity = 0 ;
    double price = 0 ;
    int barcode = 0 ;

    // ...
};

struct person
{
    // other person stuff

    std::list<order_item> orders ;
};
the barbaric, simple, crude way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
enum types { int_type, double_type, string_type, myclass_type};

struct node
{
   types type;
   void * vp;
};


list<node> L;

node x;

x.type = int_type;

int *ip = new int;

x.vp = ip;

L.insert(x);

x.type = string_type;

string * sp = new string;
x.vp = sp;

L.insert(x);


this same concept can be cleaned up with a union or, with a significant effort, templates can be used to hide all the hands-on type switcheroos. And *every type* can be stored in a byte * (unsigned char or uint8_t etc) or vector thereof. Its all doing similar things under the hood (templates do it differently by crafting different actual object types instead of mushing bytes into a blob approach) -- a vector of bytes or templates are the way to go. Templates and polymorphism is really the only professional answer if you need to support more than a small # of types, but if you have not gotten to these ideas yet, its going to be challenging.

vector<unsigned char> blob(4*sizeof(int));
int *ip = (int*)&(blob[0]);

ip[0] = 5;
ip[1] = 13;
etc.
and a list of objects of vector<unsigned char> + type of the thing


Last edited on
JLBorges yes that is what I was hoping to see if I can do!! Jonnin I have yet to work with enum types, so to just get it working I will try JL's approach. When I learn of enum types or even a better (optimized), cleaner way I am going to try that as well. Thank you all, I always so this but, becoming a programmer has been eye opening in the sense that everyone is truly here for one another.
Happy Coding!
enums are just a way to name and group constants.

enum {x,y,z}; is the same as
const int x = 0;
const int y = 1;
const int z = 2;

Again, what I was showing you isn't better. It was the 'everything is really a pile of bytes and you can beat it to death' answer. And the reason is that I think for this question, the template / polymorphism answer is going to be fairly involved esp for a new coder.
Last edited on
Topic archived. No new replies allowed.