Stack - data structure using java(part-3)

! . first created first basic Employee class.

package stacks;

public class Employee {

String firstname;
String lastname;
int id;
public Employee(String firstname, String lastname, int id) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.id = id;
}
/**
* @return the firstname
*/
public String getFirstname() {
return firstname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @return the lastname
*/
public String getLastname() {
return lastname;
}
/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + id;
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (id != other.id)
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Employee [firstname=" + firstname + ", lastname=" + lastname + ", id=" + id + "]";
}


}


 2. then created linkedstack.java class to control the operations of linked list . 

package stacks;

import java.util.Iterator;
import java.util.LinkedList;
//we are implementing linked list in other class because we want to implement the stack_jdk to implement only stack properties
public class linkedstack {
private LinkedList<Employee> stack;
public linkedstack() {
stack = new LinkedList<Employee>();
}
public void push(Employee employee) {
stack.push(employee);
}
public Employee pop() {
return stack.pop();
}
public Employee peek() {
return stack.peek();
}
public boolean isEmpty() {
return stack.isEmpty();
}
public void printstack() {
Iterator<Employee> itr = stack.listIterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
public int size() {
 
return stack.size();
}
}


3 . main.java class

package stacks;

public class main {

public static void main( String[] args) {
 

Array_stack stack = new Array_stack(10);
stack.push(new Employee( "sundaram", "dubey", 77));
stack.push(new Employee("saurabh", "dubey", 56));
stack.push(new Employee("tom", "cruise", 11));
stack.printstack();
System.out.println(stack.size());
stack.pop();
stack.printstack();
System.out.println(stack.size());
System.out.println(stack.peek());
System.out.println(stack.size());
}

}

Comments

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions