Tarik
BAN USERThis is something I did. Tested and working fine.
using System;
using System.Net;
using System.IO;
using System.Collections;
class Program
{
// Caller (block 1)
static void Main()
{
QueueStack s = new QueueStack();
s.Push("First");
s.Push("Second");
s.Push("Third");
s.Pop();
}
}
class QueueStack
{
private Queue queue;
public QueueStack()
{
this.queue = new Queue();
}
public void Push(object something)
{
this.queue.Enqueue(something);
Console.WriteLine(something + " pushed");
}
public object Pop()
{
if(this.queue.Count > 0)
{
int totalItem = this.queue.Count - 1;
Queue newQueue = new Queue();
object poppedItem = null;
for(int i= 0;i < totalItem ; i++)
{
newQueue.Enqueue(this.queue.Dequeue());
}
poppedItem = this.queue.Dequeue();
Console.WriteLine(poppedItem + " popped");
this.queue = newQueue;
return poppedItem;
}
else
{
Console.WriteLine("The stack is empty");
return null;
}
}
}
I've found a nice answer for this question:
import java.util.*;
public class CountTest {
public static void main(String... args) {
Integer[] array1 = {9, 4, 6, 2, 10, 10};
Integer[] array2 = {14, 3, 6, 9, 10, 15, 17, 9};
Set hashSet = new HashSet(Arrays.asList(array1));
Set commonElements = new HashSet();
for (int i = 0; i < array2.length; i++) {
if (hashSet.contains(array2[i])) {
commonElements.add(array2[i]);
}
}
System.out.println("Common elements " + commonElements);
}
}
Check my answer to see how I implemented the same logic using C#.
- Tarik January 06, 2013