Hey Guys,
One of my colleague asked me how can I keep a collection of Objects without using any Collection framework or Arrays and neither Maps (further more Maps are not Collections, since they never implement Collections interface)?
At first I was numb, since I am unable to think how is it possible to have a group of objects without using Collections or Arrays or Maps, then I just got an idea when we implemented the Linked Lists using Node traversals in C Language like this,
Using the same concept I proposed this solution, the solution is pretty much same as the concept of C, here what I did;
In this way we can make a Doubly Linked List, with the functionalities like add(), next(), prev(), find(int), get(int), getFromEnd(int), getFromStart(int) and so on.
One of my colleague asked me how can I keep a collection of Objects without using any Collection framework or Arrays and neither Maps (further more Maps are not Collections, since they never implement Collections interface)?
At first I was numb, since I am unable to think how is it possible to have a group of objects without using Collections or Arrays or Maps, then I just got an idea when we implemented the Linked Lists using Node traversals in C Language like this,
#include <stdlib.h> struct node { int x; struct node *next; }; int main() { /* This will be the unchanging first node */ struct node *root; /* Now root points to a node struct */ root = malloc( sizeof(struct node) ); /* The node root points to has its next pointer equal to a null pointer set */ root->next = 0; /* By using the -> operator, you can modify what the node, a pointer, (root in this case) points to. */ root->x = 5; }
//courtesy : Linked List in C
Using the same concept I proposed this solution, the solution is pretty much same as the concept of C, here what I did;
public class Data{
private int a;
private Data next;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public Data getNext() {
return next;
}
public void setNext(Data next) {
this.next = next;
}
@SuppressWarnings("unused")
public static void main(String[] args) {
Data d1 = new Data();
d1.setA(1);
Data d2 = new Data();
d2.setA(2);
d2.setNext(null);
d1.setNext(d2);
Data start = d1;
Data startCopy = start;
while(start!=null){
System.out.println(start.getA());
start = start.getNext();
}
}
}
In this way we can make a Doubly Linked List, with the functionalities like add(), next(), prev(), find(int), get(int), getFromEnd(int), getFromStart(int) and so on.
No comments:
Post a Comment