Queue implementation using SinglyLinkedList

Program


package com.wordpress.sreeharshasite;
public class NodeG<T> {
int data;
NodeG<T> nextNode;
public NodeG(int data)
{
this.data=data;
this.nextNode=null;
}
}


package com.wordpress.sreeharshasite;
public class QueueLinkedList<T> {
NodeG<T> headNode=null;
int size=0;
NodeG<T> currNode=null;
public void add(int data)
{
currNode=headNode;
NodeG<T> newNode=new NodeG<T>(data);
/*if there are no nodes in the list add the new node to the headNode*/
if(currNode==null)
{
currNode=newNode;
headNode=currNode;
size++;
}
else
{
/*if there is only head node in the list add the new node next to the headNode*/
if(currNode.nextNode==null)
{
currNode.nextNode=newNode;
headNode=currNode;
size++;
}
/*add the new node at last in the list*/
else
{
NodeG<T> prevNode=null;
while(currNode!=null)
{
prevNode=currNode;
currNode=currNode.nextNode;
}
prevNode.nextNode=newNode;
size++;
}
}
}
public void delete()
{
currNode=headNode;
/*if no nodes to delete return*/
if(currNode==null)
{
return;
}
else
{
/*if only one nodes to delete , delete the headNode*/
if(currNode.nextNode==null)
{
currNode=null;
size--;
}
/*delete the first node in the list*/
else
{
currNode=currNode.nextNode;
headNode=currNode;
size--;
}
}
}
public void display()
{
currNode=headNode;
while(currNode!=null)
{
System.out.print(currNode.data+" ");
currNode=currNode.nextNode;
}
System.out.println();
}
}


package com.wordpress.sreeharshasite;
public class QueueLinkedListDemo {
public static void main(String[] args) {
QueueLinkedList<Integer> qll=new QueueLinkedList<Integer>();
qll.add(10);
qll.add(20);
qll.add(30);
qll.add(40);
qll.add(50);
System.out.println("After Insertion");
qll.display();
qll.add(570);
System.out.println("After Insertion");
qll.display();
qll.delete();
System.out.println("After Delete");
qll.display();
qll.delete();
System.out.println("After Delete");
qll.display();
}
}

Output

After Insertion
10 20 30 40 50 
After Insertion
10 20 30 40 50 570 
After Delete
20 30 40 50 570 
After Delete
30 40 50 570
Tagged with: , , ,
Posted in Core Java, Data Structures

Stack Implementation using SinglyLinkedList

Program


package com.wordpress.sreeharshasite;
public class NodeG&amp;lt;T&amp;gt; {
int data;
NodeG&amp;lt;T&amp;gt; nextNode;
public NodeG(int data)
{
this.data=data;
this.nextNode=null;
}
}


package com.wordpress.sreeharshasite;
public class StackSingleLinkedList<T> {
int size=0;
NodeG<T> headNode=null;
public void push(int data)
{
NodeG<T> newHeadNode=new NodeG<T>(data);
NodeG<T> currNode=headNode;
/*scenario 1: if headNode is null */
if(currNode==null)
{
headNode=newHeadNode;
size++;
}
else
{
if(currNode!=null)
{
/*scenario 2: if headNode nextnode is null */
if(currNode.nextNode==null)
{
newHeadNode.nextNode=currNode;
currNode.nextNode=null;
currNode=currNode.nextNode;
headNode=newHeadNode;
size++;
}
/*scenario 3: if headNode nextnode is not null
* add the element at the head position */
else
{
newHeadNode.nextNode=currNode;
currNode=null;
headNode=newHeadNode;
size++;
}
}
}
}
public void pop()
{
NodeG<T> currNode=headNode;
/*scenario 1: if no nodes to delete return */
if(currNode==null)
{
return ;
}
else
{
/*scenario 3: delete the headNode */
if(currNode.nextNode!=null)
{
headNode=currNode.nextNode;
size--;
}
/*scenario 3: if only head node to delete */
else
{
currNode=null;
headNode=currNode;
size--;
}
}
}
/*display the headNode*/
public NodeG<T> peek()
{
if(headNode==null)
{
return null;
}
else
{
return headNode;
}
}
public void display()
{
NodeG<T> currNode=headNode;
while(currNode!=null)
{
System.out.print(currNode.data+" ");
currNode=currNode.nextNode;
}
System.out.println();
}
}


package com.wordpress.sreeharshasite;
public class StackSingleLinkedListDemo {
public static void main(String[] args) {
StackSingleLinkedList<Integer> sll=new StackSingleLinkedList<Integer>();
sll.push(10);
sll.push(20);
sll.push(30);
sll.push(40);
sll.push(50);
sll.push(10);
System.out.println("After Insertion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
sll.pop();
System.out.println("After Deletion");
sll.display();
NodeG<Integer> node=sll.peek();
System.out.println("Head Node value is:"+node.data);
}
}

Output

After Insertion
10 50 40 30 20 10 
After Deletion
50 40 30 20 10 
After Deletion
40 30 20 10 
After Deletion
30 20 10 
Head Node value is:30

 

 

Tagged with: , ,
Posted in Core Java, Data Structures

Happiness is in Creation not Consumption

Between Jan 2016 to April 2017 , i read everyday few inspiring articles/watching videos for motivation .For few hours i used to be inspired and then after all the motivation got reset .

With the past one year experience of just reading things without applying i learnt below things

  • Reading tons of articles/watching videos without applying is useless.
  • No impact if whatever read is not put in to action.
  • Instead read just one post and apply it , the impact will be huge
  • Create something everyday it might be a simple post / program or at-least 100 words.Eventually you will improve bit by bit.
  • Doing something is far greater than doing nothing
  • Identify real/virtual mentors whom you can resonate with.Only follow their blogs for consumption.
  • Books are greater resource for consumption.
  • Daily create something if you are lucky enough one among hundreds may become a masterpiece.
  • Happy creation.

images

 

Tagged with:
Posted in My Themes

Pattern programs

Pattern

1 * * * * * * *
1 2 * * * * * *
1 2 3 * * * * *
1 2 3 4 * * * *
1 2 3 4 5 * * *
1 2 3 4 5 6 * *
1 2 3 4 5 6 7 *
1 2 3 4 5 6 7 8

Program


package com.wordpress.sreeharshasite;
import java.util.Scanner;
public class PatternC {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=sc.nextInt();
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=rows;j++)
{
if(i>=j)
System.out.print(j+" ");
else
System.out.print("*"+" ");
}
System.out.println();
}
sc.close();
}
}

Output

Enter the number of rows
8
1 * * * * * * * 
1 2 * * * * * * 
1 2 3 * * * * * 
1 2 3 4 * * * * 
1 2 3 4 5 * * * 
1 2 3 4 5 6 * * 
1 2 3 4 5 6 7 * 
1 2 3 4 5 6 7 8
Tagged with: ,
Posted in Core Java, Pattern programs

Pattern programs

Pattern

1 
1 0 
1 0 1 
1 0 1 0 
1 0 1 0 1 
1 0 1 0 1 0 
1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0

Program


package com.wordpress.sreeharshasite;
import java.util.Scanner;
public class PatternB {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=sc.nextInt();
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==1)
System.out.print(1+" ");
else
System.out.print(0+" ");
}
System.out.println();
}
sc.close();
}
}

Output

Enter the number of rows
10
1 
1 0 
1 0 1 
1 0 1 0 
1 0 1 0 1 
1 0 1 0 1 0 
1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 
1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0

 

 

Tagged with: ,
Posted in Core Java, Pattern programs

Spring Auto-wiring Java configuration demo

Project Snapshot

upload(19)

Programs


package com.wordpress.sreeharshasite.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
@Configuration
public class ApplicationConfigurationAdapter
{
@Bean (name="myController")
public MyController myController()
{
return new MyController();
}
@Bean (name="employeeServiceImpl")
public EmployeeServiceImpl employeeServiceImpl()
{
return new EmployeeServiceImpl();
}
}


package com.wordpress.sreeharshasite.controller;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.wordpress.sreeharshasite.model.*;
public class MyController {
@Autowired
private EmployeeServiceImpl employeeServiceImpl;
public EmployeeServiceImpl getEmployeeServiceImpl() {
return employeeServiceImpl;
}
public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
this.employeeServiceImpl = employeeServiceImpl;
}
public List<Employee> listAllEmployees()
{
return employeeServiceImpl.listAllEmployees();
}
}


package com.wordpress.sreeharshasite.model;
public class Employee {
private int empId;
private String empName;
private String city;
private String designation;
public Employee()
{
}
public Employee(int empId,String empName,String city,String designation)
{
this.empId=empId;
this.empName=empName;
this.city=city;
this.designation=designation;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package com.wordpress.sreeharshasite.services;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public interface EmployeeService {
List<Employee> listAllEmployees();
}


package com.wordpress.sreeharshasite.services;
import java.util.ArrayList;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public class EmployeeServiceImpl implements EmployeeService {
public List<Employee> listAllEmployees() {
Employee emp1=new Employee(100,"John","Delhi","Associate");
Employee emp2=new Employee(101,"Ravi","Hyderabad","SeniorAssociate");
Employee emp3=new Employee(102,"Rajesh","Delhi","JuniorAssociate");
Employee emp4=new Employee(103,"Ramesh","Delhi","AssociateDirector");
List<Employee> employeeList=new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
return employeeList;
}
}


package com.wordpress.sreeharshasite.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.wordpress.sreeharshasite.configuration.ApplicationConfigurationAdapter;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.model.Employee;
public class DIDemo {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfigurationAdapter.class);
MyController controller = (MyController) ctx.getBean("myController");
List<Employee> employees = controller.getEmployeeServiceImpl().listAllEmployees();
for(Employee employee : employees){
System.out.println("Employee Details");
System.out.print(employee.getEmpId()+" ");
System.out.print(employee.getEmpName()+" ");
System.out.print(employee.getCity()+" ");
System.out.print(employee.getDesignation()+" ");
System.out.println();
}
}
}


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordpress.sreeharshasite.spring</groupId>
<artifactId>SpringDIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDIDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Output

Jul 21, 2017 6:56:11 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e9f23b4: startup date [Fri Jul 21 06:56:11 IST 2017]; root of context hierarchy
Employee Details
100 John Delhi Associate 
Employee Details
101 Ravi Hyderabad SeniorAssociate 
Employee Details
102 Rajesh Delhi JuniorAssociate 
Employee Details
103 Ramesh Delhi AssociateDirector
Tagged with: , ,
Posted in Frameworks, Spring

Spring – Dependency Injection Auto Wiring Demo

Project Snapshot

upload(18)

Program


package com.wordpress.sreeharshasite.model;
public class Employee {
private int empId;
private String empName;
private String city;
private String designation;
public Employee()
{
}
public Employee(int empId,String empName,String city,String designation)
{
this.empId=empId;
this.empName=empName;
this.city=city;
this.designation=designation;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package com.wordpress.sreeharshasite.services;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public interface EmployeeService {
List<Employee> listAllEmployees();
}


package com.wordpress.sreeharshasite.services;
import java.util.ArrayList;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public class EmployeeServiceImpl implements EmployeeService {
public List<Employee> listAllEmployees() {
Employee emp1=new Employee(100,"John","Delhi","Associate");
Employee emp2=new Employee(101,"Ravi","Hyderabad","SeniorAssociate");
Employee emp3=new Employee(102,"Rajesh","Delhi","JuniorAssociate");
Employee emp4=new Employee(103,"Ramesh","Delhi","AssociateDirector");
List<Employee> employeeList=new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
return employeeList;
}
}


package com.wordpress.sreeharshasite.controller;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
import java.util.List;
import com.wordpress.sreeharshasite.model.*;
public class MyController {
@Autowired
private EmployeeServiceImpl employeeServiceImpl;
public EmployeeServiceImpl getEmployeeServiceImpl() {
return employeeServiceImpl;
}
public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
this.employeeServiceImpl = employeeServiceImpl;
}
public List<Employee> listAllEmployees()
{
return employeeServiceImpl.listAllEmployees();
}
}


package com.wordpress.sreeharshasite.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.model.Employee;
public class DIDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);
MyController controller = (MyController) ctx.getBean("myController");
List<Employee> employees = controller.getEmployeeServiceImpl().listAllEmployees();
for(Employee employee : employees){
System.out.println("Employee Details");
System.out.print(employee.getEmpId()+" ");
System.out.print(employee.getEmpName()+" ");
System.out.print(employee.getCity()+" ");
System.out.print(employee.getDesignation()+" ");
System.out.println();
}
}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="myController" class="com.wordpress.sreeharshasite.controller.MyController" autowire="byName"/>
<bean id="employeeServiceImpl" class="com.wordpress.sreeharshasite.services.EmployeeServiceImpl"/>
</beans>


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordpress.sreeharshasite.spring</groupId>
<artifactId>SpringDIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDIDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Output

Jul 20, 2017 5:02:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Jul 20 17:02:41 IST 2017]; root of context hierarchy
Jul 20, 2017 5:02:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Employee Details
100 John Delhi Associate 
Employee Details
101 Ravi Hyderabad SeniorAssociate 
Employee Details
102 Rajesh Delhi JuniorAssociate 
Employee Details
103 Ramesh Delhi AssociateDirector
Tagged with: , ,
Posted in Frameworks, Spring
Follow My Diary on WordPress.com
Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

%d bloggers like this: