Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a linkedlist store different data types?

My question is very general. I just started studying data structures and I came by linked-lists. I understood they are a sequence of nodes. Each node can store some data and it knows the next node in the list.

So one node has an object O and a pointer to the next object called object B which in turn has another pointer .. until we get to the node which has a pointer to null.

But say I am storing integers in a node in a Linked-list that is pointing to another node holding a string. First, is it allowable? And second, how would this be ever useful?

Also, what are the most common operations done on a linkedlist? getSize(), remove(), insert(), getElement(), concatenate()

If I were to store a million mobile number, would it be efficient to use a linkedlist? If not, where would the optimal use of a linkedlist appear?

Since a LinkedList is stored randomly in memory(using pointers from one node to another) not like an array adjacent, would this make it harder in NON- auto garbage collection languages like C++/C in terms of memory allocation and free-ing?

like image 716
Ralph Avatar asked Mar 22 '14 17:03

Ralph


People also ask

What data type is the data in a linked list?

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers.

Can linked list store heterogeneous data?

NOTE:- Without using Generics, Java LinkedList supports Heterogeneous elements. However, it is not recommended to use Collections without Generics.

Can we store different data types in array?

Yes we can store different/mixed types in a single array by using following two methods: Method 1: using Object array because all types in . net inherit from object type Ex: object[] array=new object[2];array[0]=102;array[1]="csharp";Method 2: Alternatively we can use ArrayList class present in System.


1 Answers

But say I am storing integers in a node in a Linked-list that is pointing to another node holding a string. First, is it allowable? And second, how would this be ever useful?

Yes, it's allowed as long as the list is declared as List<Object> or List<Serializable>, which both String and Integer extend/implement.

In this particular case, it wouldn't be very useful. But consider a List<Vehicle>. It could store instances of Car, Bike, Truck or any other kind of Vehicle.

what are the most common operations done on a linkedlist?

Those that are documented in the javadoc. I would say that adding and iterating are probably the most common. But I haven't make any statistical measurement.

If I were to store a million mobile number, would it be efficient to use a linkedlist?

It depends on the operation you need to do on the list. If you just need to add at the beginning or the end, it will be O(1). Iterating is not a problem. Finding the index of a phone would be O(n). Accessing at a given index in the list would also be O(n).

In general, an ArrayList is much faster than a LinkedList for almost every use-case. The only use-case where a LinkedList is faster is when it always inserts at the biginning, or when it removes/inserts elements while iterating using its Iterator.

would this make it harder in NON- auto garbage collection languages like C++/C in terms of memory allocation and free-ing?

I'm not experiences enough in those languages to answer, but yes, since you have to manage the memory, it's harder.

like image 88
JB Nizet Avatar answered Oct 11 '22 13:10

JB Nizet