List, Linked List is a List

Linked lists are probably the second most commonly used general purpose storage structure after arrays. The mechanism used in linked list named as versatile mechanism. It can replace and array as the basis for other storage structures such as stacks and queues.In fact, you can use a linked list in many cases in which access to individual items using an index. There are simple linked lists, double ended lists, sorted lists, doubly inked lists, and lists with iterators. But linked lists aren’t the solution to all data storage problems,but they are surprisingly versatile and conceptually simpler than some other popular structures such as trees. Trees means Binary trees, balanced binary trees….etc.

A simple way to create a Linked List within a linked list.

Main class where user input the data

public class Main {

public static void main(String[] args) {

VertList newList1 = new VertList();

List l1 = new List();

l1.InsertHead(1);

l1.InsertHead(22222);

l1.InsertHead(1111);

l1.InsertHead(111);

l1.InsertHead(22);

newList1.InsertLink(l1);

}

}

The class that store the values that the user pass

public class List {

Node head;

public List() { // Constructor

}

public List(Listnew x) { // Overloaded Constructor

head = null;

}

public void InsertHead(int x) { // Method that create inner Nodes

Node nodelink = new Node(x);

if (head == null) {

head = nodelink;

} else {

nodelink.next = head;

head = nodelink;

}

}

}

Create another list and add the previous list as a value(Node) to the list

public class VertList {

Listnew head;

public VertList() { //Overloaded Constructor.

head = null;

}

public void InsertLink(List l) { //Method that create the Lists

Listnew nodelink = new Listnew(l);

if (head == null) {

head = nodelink;

} else {

nodelink.nextNode = head;

head = nodelink;

}

}

}

Likewise we can create many lists inside another list

ex:

 insert new values:

The out put

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s