Monday, December 11, 2017

Data Structure and Algorithms in Java


                               

Data Structures :


It is a container of data. It let us store data more effectively. Execution and manipulation of data become's more easy and fast with the help data structures. It provides a bunch of ways to store data.

Large projects contain complex requirements, those projects need to be faster and user-friendly. There are many requirements those can't be implemented without data structures. Data structures provide many algorithms to do so.

Topics

  • Stack
  • Queue
  • Linked List
  • Sorting
  • Hash tables 
  • Trees
  • Application Development

Stack :
Is the linear data structure which follows the order in which the operations 

are performed .
The major operations which performed in Stacks are
  • Push : Used to insert the data
  • Pop : Used to access the data
  • isEmpty : Used to check whether the stack is empty

There are two ways to implement stacks 
1. Array
2. LinkedList


Sample example on Stack using array:
package datastr;
public class InStack {
private int [] stack;
private int top;
private int size;
public InStack(){
top=-1;
size= 50;
stack= new int[50];
}
public InStack(int size){
top=-1;
this.size=size;
stack =new int[this.size];
}
public boolean push(int item){
if(!isFull()){
top++;
stack[top]=item;
return true;
}else{
return false;
}
}
public int pop(){
try {
return stack[top--];
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public boolean isFull(){
return(top== stack.length-1);
}
public boolean isEmpty(){
return(top== -1);
}
} ********************************************
package datastr;
public class Main {
public static void main(String[] args){
InStack inStack = new InStack();
if(!inStack.isFull()){
inStack.push(2);
inStack.push(4);
inStack.push(6);
inStack.push(9);
}
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
System.out.println(inStack.pop());
}
}

Queue :

Queue is an another type of data structure, the way of storing and retrieving the data from queue first in and first out.

The major operations performed on Queue are :


  • Enqueue : Adds the item to queue
  • Dequeue : Removes the item from queue
  • Front  : Item at first index 
  • Rear : Item at last index



0 comments:

Post a Comment