¿Cuál es la diferencia técnica entre una lista enlazada y una secuencia?

It seems like they both do the same thing in the same way: Lazy operations in a specific but not-necessarily-indexed order, and cannot necessarily backtrack.

preguntado el 24 de agosto de 12 a las 06:08

Just to confirm, you mean stream as in en.wikipedia.org/wiki/Stream_(type_theory)? -

I had in mind more like en.wikipedia.org/wiki/Stream_(computing) but your link seems compatible. -

As you said 'lazy', I assume you strictly mea lazy streams and not data/byte streams. Is this correct? -

I meant any stream where pending elements are not necessarily loaded into memory, evaluated, or otherwise touched except to establish a pointer to an object representing that element, or where the stream is not run length encoded (that is, you don't know when you've reached the end of the stream until you look and can't find anything), like in your link -

On the high level of abstraction they are the same but technically they a quiet different. With great assumptions you can consider stream as single-linked list of bytes. But for me this sort of assumptions looks like imagining a spherical horse in vacuum - absolutely useless. -

4 Respuestas

Linked list is a specific way of representing sequences of data elements in memory, where each element is paired with a pointer of sorts to the next element in the sequence. Linked lists let you perform a range of operations on their subsequences: you can cut out or insert entire chains of elements, or delete elements from the middle at a very low cost.

Streams, on the other hand, are abstractions for accessing data in sequential order, without any specific requirements to their representation in memory. You can use a linked list to implement a stream, but you could also use another data structure for that, such as a plain array or a circular array buffer.

Respondido 24 ago 12, 06:08

Are you also thinking of the Stream class/type? - leppie

@leppie No, I'm thinking of streams as of "one way" read-only or write-only abstractions. - Serguéi Kalinichenko

I think this is a little like trying to compare apples and oranges.

A linked list is a data structure where each node points to another node. They're a useful structure as inserting and removing items from a linked list is just a matter of re-pointing a node, rather than with an array where you need to do allot more shuffling. See http://en.wikipedia.org/wiki/Linked_list para obtener más información.

A stream is an abstracted object used to represent a series of bytes. In most frameworks (Java, .NET etc) there are several concrete implementations of steams (memory stream, file stream etc) used to read the byte array from the relevant source (memory, file etc).

Respondido 24 ago 12, 06:08

Stream is not a class or type in the OP's context. - leppie

@leppie: Yes, but that's not what Neil's answer claimed. He pointed out that there are usually concrete implementations of an abstract interface that is called "Stream". Also, the original question doesn't mention anything about what language or environment it's set in, so I don't really see how you can be so certain. - Rupert Swarbrick

A linked list is a data structure where each element has a pointer to the next one, and possibly the same in the other direction. Circular linked lists even have a pointer from the last to the first element and vice versa. Those pointers (or references in languages that don't have pointers) are what defines the data structure. They imply a certain mode of operation, they don't force that, though. The LinkedList class in Java, for example, can be used like an array, although it won't be very effective then. It also can be used as (double-ended) queue or stack, depending on which functions you call.

A stream, on the other hand, is not defined as a data structure, but as a source or sink of elements. These elements could be bytes or characters, if you think of file streams, socket streams or reader/writer classes that wrap streams. The elements provided by a stream could also be more complex, e.g. tokens for a parser. In that case the stream likely uses some kind of queue internally, which could be implemented using a linked list or some array construction.

Just make sure to understand these two things are defined on different layers of abstraction. The linked list is defined on how it works internally, while the stream is defined on how it works externally.

Respondido 24 ago 12, 06:08

There is a common abstraction between a read-only singly-linked list and an input stream, which C++ formalises as InputIterator: you can read a value and you can move forwards. In many stream APIs you have to do both simultaneously, but given that API it's fairly easy to see how to separate them out with a wrapper that caches one value: C++ calls this class istream_iterator.

However, a singly-linked list has a property that a stream does not always have, which C++ formalises as ForwardIterator: you can copy the current position, move the copia forward, but still read the value at the location of the original. A generalized stream cannot do this, because the underlying I/O only has one "current position". With a linked list you can have multiple pointers to different nodes in the list, with no troubles.

Some streams can be marked and reset, rewound, seeked (sought?) etc, adding facilities that are somewhat like a C++ ForwardIterator, or even RandomAccessIterator.

I use C++ as an example not because it's particularly important, but because the C++ concept of an iterator is designed in part to provide an abstraction common to data structures and streams. Not all languages have such a common abstraction, but for another example in Python you can write for x in y: if y is a container data structure or if y is a file-like object, or in general if y is "iterable".

Respondido 24 ago 12, 09:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.