Skip to main content

Data Structures using Java

package com.test.count;

/**
 * 
 * @author Kiran
 *
 */
public class LinkedList {

Node headNode;
/**
*  This method appends the Node at the end of the List
*  
* @param data
*/
public void appendNode(int data) {
Node currentNode = headNode;
if(currentNode == null) {
Node newNode = new Node(data);
headNode = newNode;
return;
}
while(currentNode.getNextNode()!=null) {
currentNode = currentNode.getNextNode();
}
currentNode.setNextNode(new Node(data));
}

/**
* This method prints all the Nodes/Values of the 
* LinkedList
* 
*/
public void printNodes() {
Node currentNode = headNode;
if(currentNode==null) {
System.out.println(" The Node is Null");
return;
} else {
System.out.print("   "+ currentNode.getData());
while(currentNode.getNextNode()!=null) {
currentNode = currentNode.getNextNode();
System.out.print("=>  "+ currentNode.getData());
}
}
}
/**
*  This method deletes the Node with the given Data
*  
* @param data
*/
public void deleteNode(int data) {
Node currentNode = headNode;
Node previousNode = headNode;
if(currentNode.getData()==data) {
headNode = currentNode.getNextNode(); 
return;
} else {
while(currentNode.getNextNode()!=null) {
previousNode = currentNode;
currentNode = currentNode.getNextNode();
if(currentNode.getData()==data) {
previousNode.setNextNode(currentNode.getNextNode());
}
}
//if the last element is the element we are looking for 
if(currentNode.getData()==data) {
currentNode = null;
}
}
}

/**
* This method finds the given element Index in the list
* If the Element is Not found, it will return -1
* 
* @return
*/
private int findNodeLocation(int data) {
int index = -1;
Node currentElement = headNode;
int counter =  0;
if(currentElement!=null) {
if(currentElement.getData()==data) {
System.out.println(" Found the Element, "
+ " which is at the Head of the LinkedList ");
return 0;
} else {
while(currentElement.getNextNode()!=null) {
currentElement = currentElement.getNextNode();
counter++;
if(currentElement.getData()==data) {
System.out.println(" The Element is Found at the Index "+ counter);
return counter;
}
}
//If the Element is found at the Last Element, return the Last Element's index
if(currentElement.getData()==data) {
return counter++;
}
}
}
return index;
}
/**
* 
* @param a
*/
public static void main(String a[]) {
LinkedList linkedList = new LinkedList();
linkedList.appendNode(2);
linkedList.appendNode(3);
linkedList.appendNode(5);
linkedList.appendNode(6);
linkedList.appendNode(8);
linkedList.appendNode(20);
linkedList.printNodes();
System.out.println("\n ****  After Removing the Elements from the LinkedList *******");
linkedList.deleteNode(6);
linkedList.printNodes();
int indexOfTheElement = linkedList.findNodeLocation(20);
System.out.println("\n The Index Of the Element Searching is "+ indexOfTheElement);
}

}



Comments

Popular posts from this blog

Java Productivity tools

Here are the list of Java Productivity tools that help in simplifying the daily life of a developer. Eclipse as an IDE simplifies the development life cycle which has a lots of plugins to support different programming languages and frameworks. Some of useful Eclipse plugins are - Sonarlint that helps to do static code analysis and give suggestions as we go along the development life cycle. JDGUI - Is the Eclipse Plugin that supports the decompilation of the Java application within eclipse, this is very handy when debugging and need to look at the out of the box code from a framework/library. JUnit is the unit testing framework that supports Unit Testing of Java Applications. Mockito is the framework that supports the Mock Unit Testing of the Java Application.

Running Multiple Operating Systems(Windows and Ubuntu Linux) on the same machine

VMWare Player is a freely downloadable VMWare. Download VMWare player software and install it on your windows OS download an image of the Ubuntu Linux Desktop version called Ubuntu from http://www.ubuntu.com/getubuntu/download that in iso image format. Then download VMWare configuration bundle that contains a list of files, extract those file to some folder like C:\OS\. Then edit the file" os.vmx file and give the path of the .iso image in that file in the line like below. ide1:0.fileName = C:\OS\ubuntu-8.10-desktop-i386.iso" Now open the file os.vmx file using the vmware player, that will open the Ubuntu OS. You will get a list of options in that select the option install Ubuntu without changing your current configuration of the system Now that will start the Ubuntu OS in a window inside your windows OS. Now you have a browser and all the applications inside the Ubuntu OS, you can start working on that. Double click on this window/expand it to show in full screen. To switch ...