Problem 1
|
Array Implementation of Queue using Circular Array
public class MyQueue { int[] arr; int size, front = -1, rear = -1; public MyQueue(int capacity) { arr = new int[capacity]; size = capacity; } public bool IsEmpty() { if (front == -1 && rear == -1) return true; else return false; } public void Enqueue(int val) { if ((rear + 1) % size == front) Console.WriteLine("Overflow"); else { if (IsEmpty()) front = rear = 0; else rear = (rear + 1) % size; arr[rear] = val; } } public int Dequeue() { int result = -1; if (IsEmpty()) Console.WriteLine("Underflow"); else { result = arr[front]; if (front == rear) front = rear = -1; else front = (front + 1) % size; } return result; } public void PrintQueue() { if (rear < front) { for (int i = front; i < size; i++) Console.WriteLine(arr[i]); for (int i = 0; i <= rear; i++) Console.WriteLine(arr[i]); } else { for (int i = front; i <= rear; i++) Console.WriteLine(arr[i]); } }
|
Problem 2
|
/// <summary> /// Implement Queue using Stacks /// </summary> /// <typeparam name="T"></typeparam> public class MyQueue<T> { Stack<T> stack1 = new Stack<T>(); Stack<T> stack2 = new Stack<T>(); public void Push(T value) { stack1.Push(value); } public T Pop() { if (stack2.Count == 0) { while (stack1.Count > 0) { stack2.Push(stack1.Pop()); } } return stack2.Pop(); } public void Print() { while (stack2.Count > 0) { Console.WriteLine(stack2.Pop()); } while (stack1.Count > 0) { stack2.Push(stack1.Pop()); } } }
|
Problem 3
|
Monday, 8 February 2016
suman microsoft queue
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment