Skip to main content

How to design a LinkedList ?

To design a Linked List we normally need to design three classes one class for Node that contains a data and link to the next node.

Second class contain a big picture as a combination of nodes where one can view the number of nodes  as well as design functions for it.

Third class contains operations or the methods that we want to perform on the linked lists.This class contains main method and is driving whole mechanism.



Basic Structure of Node:-

Data represent the value and next represent the link to other nodes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.problems.list;

public class Node {
 int data;
 Node next;
 


 public Node()
 {
  data=0;
  next=null;
 }
 
 public Node(int data)
 {
  this.data=data;
  next=null;
 }
 
 public Node(int data,Node node)
 {
  this.data=data;
  this.next=node;
 }
 
 public int getData() {
  return data;
 }

 public void setData(int data) {
  this.data = data;
 }

 public Node getNext() {
  return next;
 }

 public void setNext(Node next) {
  this.next = next;
 }

 @Override
 public String toString() {
  return "Node [data=" + data + ", next=" + next + "]";
 }
 
}





Basic Structure of Linked List:-

Length represent the current size of list and head represent the first node.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.problems.list;

public class LinkedList {

 public LinkedList() {
  length = 0;
 }
 
 Node head;
 int length;
 public void addNodeStart(LinkedList list,int data)
 {
  Node temp=new Node(data);
  if(list.head==null)
  {
   System.out.println("head is null");
   head=temp;
   length++;
  }
  else{
  
  temp.next=list.head;
  list.head=temp;
  
  }
 }
 
 public void printList(Node head)
 {
  Node temp =head;
  System.out.println("List Data");
  while(temp!=null)
  {
   System.out.print(+temp.data+" ");
   temp=temp.next;
  }
  
 }
}
Basic Functions:-

Here we are creating one list then passing the head(first node) to function for creating the node.In second method we are printing the list.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.problems.list;

public class LinkedListOperations {

 public static void main(String[] args)
 {
  LinkedList list=new LinkedList();
  list.addNodeStart(list, 5);
  list.addNodeStart(list, 15);
  list.addNodeStart(list, 25);
  list.printList(list.head);
 }
}

Comments

.

Popular posts from this blog

Shortest Path from source to Vertex :- Dijkstra Algorithm

Shortest Path from source to Vertex :- Dijkstra Algorithm:- Dijkstra  Algorithms is an algorithm use to find the shortest path from source vertex to a given vertex. package Graph ; import java.util.HashMap ; abstract public class DirectedGraph { Vertex [] vertexlist = new Vertex [ 10 ]; HashMap < Character , HashMap < Character , Integer >> edgelist = new HashMap <>(); Vertex vertex ; //count of vertex and edge static int vertexcount = 0 ; int edgecount = 0 ; /* * This function takes a label and insert in the vertex list as well as edge list since it is new vertex it will add * null to its adjoining vertices */ int addVertex ( char label ) { vertex = new Vertex ( label ); vertexlist [ vertexcount ]= vertex ; System . out . println ( vertexlist [ vertexcount ]. label ); edgelist . put ( vertex . label , null ); vertexcount ++; return vertexcount ; } int addEdge ( char label , char []

How to build a project in eclipse with MAVEN build tool?

How to build a project in eclipse with MAVEN build tool? Step 1:- Install maven and set the path in my computer. Once path is set for java and maven you will get a screen With version installed in your system. Step 2:- Write a command mvn archetype:generate to build a project of your choice.This will give you a option to select a project from list. Step 3:- As soon as this operation will complete maven give you choice to choose project.Search for maven-archetype-webapp this will build a web project with basic structure. Step4:- Follow the below procedure to give name , version ,package ,artifact and group id of your choice. Step 5:- You will get screen with build success.Congrats your project is build in directory. Step 6: Go in the directory to check for the folders automatically created by maven. You will get pom file and src folder and the package folder. Step:-7  Move to the directry having pom.xml and run mvn ecl