views:

184

answers:

9

A rough definition of a data structure is that it allows you to store data and apply a set of operations on that data while preserving consistency of data before and after the operation. However some people insist that a primitive variable like 'int' can also be considered as a data structure. I get that part where it allows you to store data but I guess the operation part is missing. Primitive variables don't have operations attached to them. So I feel that unless you have a set of operations defined and attached to it you cannot call it a data structure. 'int' doesn't have any operation attached to it, it can be operated upon with a set of generic operators.

Please advise if I got something wrong here.

+2  A: 

I don't think your definition of data structure is correct.

It seems to me that a struct (with no methods) is a valid data structure, but it has no real 'operations'. And that's not important. It's holding data.

To that end, and int holds data, an Object holds data. They are data structures (technically).

That said, I don't ever find myself saying "What datastructure shall I use? I know! an int!".

I would say you need to re-evaluate the meaning of "data structure".

Noon Silk
A: 

I would argue that "int" is a data structure - it has a defined representation and meaning. That is, depending on your system, it has a specific length, a specific set of operators available to it, and a specified representation (be it twos-compliment). It is designed to hold "integer numbers".

Practically, the distinction isn't particularly relevant.

Yann Ramin
A good comment, but I'd disagree that operators have anything to do with the definition of a data structure. You can of course simply store data and retrieve it later for reference without modifying or comparing it. Of course, without appropriate operations (such as set/get and so on), your data might lack the additional meaning or purpose it requires to support it's intended purpose.I agree though that for practical purposes, the distinction isn't really relevent! :-)
S.Robins
+2  A: 

Primitives do have operations attached to them; however, they may not be in the format of methods as you would expect in an object-oriented paradigm.

Assignment =, addition +, subtraction -, comparison ==, etc are all operations. Especially if you consider that you can explicitly define, override, or overload these operations for arbitrary classes (i.e.: data structures) in some languages (e.g. C++), then the primitive int, char, or what have you, are not very different.

Justin Johnson
A: 

Primitive variables don't have operations attached to them. So I feel that unless you have a set of operations defined and attached to it you cannot call it a data structure.

'int' doesn't have any operation attached to it, it can be operated upon with a set of generic operators.

Generic? Then how come 2+2 works, but "ninja" + List<float> doesn't? If the operator was generic, it'd work on anything. It doesn't. It only works on a few predefined types, such as integers.

Ints certainly have a set of operations defined on them. Arithmetic operations such as addition, subtraction, multiplication or division, for example. Most languages also have some kind of ToString()-like functionality defined on integers. But you can't do just anything with an int. For example, you can't pass an int to a function expecting a string. ints have a very specific set of operations defined on them. Those operations just aren't member methods. They come in the form of operators and non-member functions or member methods of other classes. But they are still operations that work on integers.

jalf
"you can't pass an int to a function expecting a string" <- you can if the language is typeless, like JavaScript or PHP for example.
Justin Johnson
When I see stack I have operations like push and pop. These are attached to stack, you cannot do push or pop on linkedlist. My doubt is that is it really necessary to have operators defined for a variable to be called as a data structure ?
Ravi Gupta
@Ravi All types have operators, primitive or otherwise. A type without any operators would be useless. Even DTO's (and even if you don't count the constructor), which typically have no other methods, still have member access operators. I'm not sure where you're going with this argument anymore.
Justin Johnson
@Ravi: That's just syntactic sugar. Does it really matter whether the syntax for popping off a stack is `st.pop()` or `pop(st)`? In both cases, pop is clearly an operation defined on the stack. In the same way, `+` is clearly an operation defined on integers, whether or not it is defined as a member method in your specific language. You need to look past the syntax, and think about the semantics. It doesn't matter what the function call looks like, as much as what the operation is defined to do, and what data type it is designed to work on.
jalf
@Justin Thanks for the response, I am just clearing up my head on this. I know that my definition may be seriously flawed, I am here to correct it :)
Ravi Gupta
"If the operator was generic, it'd work on anything." This is a flawed understanding of generic. Operators have clear meanings and restrictions. They are supposed to have clear properties as well, but these are sometimes violated. For example, a+b should equal b+a because + is commutative. Unfortunately, BASIC defined + for strings as concatenation and violated this principle.
Jon Reid
@Justin: I think it would be better to say that types don't really have (as in own or belong to) operators, but that operators are designed to act on types. I'm assuming you meant primitives in this case, but even with classes, the methods that alter a class state (self) are acting on without belonging to the underlying data structure being represented. That's Ravi's "syntactic sugar" so to speak. Perhaps I argue semantics, but I think it makes the distinction clearer between system behaviour and data structure.
S.Robins
@Ravi: Re: your stack comment above, the operations themselves are there to manipulate the represented data structure. If you remove object-orientation from the equation, stacks are classically implemented by defining a data structure (I.E.: an array), and supported using the Push()/Pop() methods. It could also be argued that the stack as a data structure doesn't work as intended without the supporting methods manipulating the data in a particular way, and yet, the same data structure (IE: array) could represent a queue... but this is a large departure from your original question I think.
S.Robins
@Jon Reid: I don't see how that contradicts anything I said. Operators have clear meanings and they work on specific data types. They don't work generically on "whatever you pass to them". And I don't see how BASIC matters i nthis discussion.
jalf
+4  A: 

To say that something is structured implies that there is a form or formatting that defines HOW the data is structured. Note this has nothing to do with how the data is actually stored. You could for example create a data structure that exists entirely within a single Integer, yet represents a number of different values.

A data structure is an arbitrary construct used to describe how to store data in a system. It may be as simple as a single primitive, or as complex as a class. So the answer is largely subjective. It's "yes" if you decide to use a primitive as such, that a simple primitive may be considered a primitive data structure, because it describes HOW you wish to store an element of data. The answer is also "no", because it describes an element of a structure and not necessarily the whole structure in itself.

As for how this relates to operations, strictly speaking a data structure has nothing to do with behaviour, it is simply a storage mechanism. Preserving consistency of data is really a behavioural thing. Yes, your compiler probably spits out errors if you try to shoe-horn a 32-bit value into a Byte, but that's symptomatic of the behaviour of the system (Ie: compilation) acting on the data structure of your application, of which your primitives are an element.

S.Robins
+1 And welcome to SO
Justin Johnson
A: 

I don't think, (ref: Wikipedia entry) that Data Structure includes the definition of permissible operations (on operators). Of course, we could cite the C++ class as a counter-example in which we can define overloaded operators. At the same time, we define a structure as just a composite/user defined datatype and do not declare any permissible operations on them. We allow the compiler to figure that out.

Amit
+1  A: 

Your definition of a data structure isn't quite correct. A data structure doesn't necessarily have any attached behaviors or operations. An ADT or Abstract Data Type is what you are describing as a data structure. An ADT includes the data and the behaviors or operations that work on that data. An int by itself is not an ADT, but I suppose you could call it a data structure. If you encapsulate an int and its operations then you have an ADT which is what I think you are trying to describe as a data structure. Classes provide a mechanism for implementation of ADTs in modern languages.

wikipedia has a good description of abstract data types.

rayd09
A: 

'int' doesn't have any operation attached to it, it can be operated upon with a set of generic operators.

Operations are intrinsically linked with the things that they operate on; there's no such thing as generic operations.

This is true in the mathematical sense (< works for in set of integers, but has no meaning for complex numbers), and also in the computer scientific sense (evaluating a + b requires that a and b are or can be converted to compatible types on which the + operation is defined).

Seth
A: 

Of course, it depends what you mean by "data structure." Others have focused on whether your definition is correct and raise good questions. But what if we say, "Let's ignore the term for now, and focus on what you described?" In other words, what if look at

  • A piece of data that has a designated interpretation of its value
  • A set of operations on that data

Then certainly, int qualifies. (If there were no operations on int, we'd all be stuck!)

For a more mathematical approach to programming that begins with these questions, and takes them to what some have called "an algebra of computation," see Elements of Programming by Alex Stepanov and Paul McJones.

Jon Reid