Tuesday, August 21, 2012

Queue Operations Using LinkedList in Java



import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;


public class QueueOperations {

public static void main(String args[]){

Queue que=new LinkedList();

//Insert elements into Queue
que.offer(12);
que.offer(25);
que.offer(75);
que.offer(9);

System.out.println("Queue"+que);

//Queue Operations
System.out.println("Queue Peek the elmenet  "+que.peek());
System.out.println("Queue Remove Element "+que.poll());

System.out.println("isEmpty :?"+que.isEmpty());
}
}

Output:
Queue[12, 25, 75, 9]
Queue Peek the elmenet  12
Queue Remove Element 12
isEmpty :?false

Stack Operations Using Java Programming


import java.util.Iterator;
import java.util.Stack;

public class StackOperations {

public static void main(String args[]){
Stack st=new Stack();

//Push the elments into stack
st.push(12);
st.push(9);
st.push(15);
st.push(76);
st.push(55);

//Elements in stack
System.out.println("Stack"+st);

//Pop the Elements
System.out.println("Top of the Stack:"+st.peek());
System.out.println("Pop the element from stack "+st.pop());

//Display elements using iterator
Iterator it=st.iterator();
while(it.hasNext())
System.out.println(it.next());

//Check stack is Empty or not
System.out.println("Stack is empty ?"+st.isEmpty());

}
}

Output:
Stack[12, 9, 15, 76, 55]
Top of the Stack:55
Pop the element from stack 55
12
9
15
76
Stack is empty ?false