How to implement linked list in C/C++ with out using
struct node{
int data;
struct node *next;
}
just using two arrays, one for data, other for next node
How to implement linked list in C/C++ with out using
struct node{
int data;
struct node *next;
}
just using two arrays, one for data, other for next node
Sure you can. There is some discussion about the topic here: Is a Linked-List implementation without using pointers possible or not?
One implementation would be to create an array, and store the nodes in the array. Wikipedia has a good pseudo-code example: http://en.wikipedia.org/wiki/Linked_list (Linked lists using arrays of nodes section)
You define two arrays 1.Data Array 2.Index Array every data array will contain data value and corresponding index in Index array will contain next node to be pointed by the value element you need to keep track of the last value added because it gives the index of Index array where you can add an index of the next value pointed by that value node. Adding a data value is also easy just add a new value in the data array and fill the previous element value in Index array by the index of the data value in data element.
Data Array 2,5,3,1,2. Index Array 1,2,3,4,0 (0 means it is not pointing to any data element)
add element: 2,5,3,1,2,77. Index Array 1,2,3,4,5,0.
delete element: 2,5,0,1,2,77 Index Array 1,3,0,4,5,0.