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

Design Patterns using Java

According to Gang of Four(GOF) any software is classified in to one of the three categories. I read so many books about design patterns which provide a lot of information about Design Patterns in a language neutral way or related to a particular programming language. I am trying to complement the great books by providing the precise and concise information that is required in the day to day programming of a Java Developer. Any software can be classified into one of the three categories -Framework, Toolkit, Application. Framework - Framework defines a set of steps to create an application. Framework provides design reuse. Toolkit - Toolkit provides some utility functions to an existing application. Toolkit provides code reuse. Application - Application is some thing that is specific to the project, and is not useful outside the context of the current application. Gang of Four divided the Design Patterns in to 3 types based on their usage. There are 3 types of Gang of Fo