DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
public class QueueUsing2Stacks
{
private Stack _stack1 = new Stack();
private Stack _stack2 = new Stack();
public QueueUsing2Stacks() { }
public void Enqueue(int data)
{
//Pop ALL existing elements from stack1, push them onto stack2
int _stack1Count = _stack1.Count;
for (int i = 0; i < _stack1Count; i++)
{
_stack2.Push(_stack1.Pop());
}
//Push new data onto stack1
_stack1.Push(data);
int _stack2Count = _stack2.Count;
//Pop ALL elements from stack2 and Push them back onto stack1
for (int i = 0; i < _stack2Count; i++)
{
_stack1.Push(_stack2.Pop());
}
}
public int Dequeue()
{
if (_stack1.Count == 0)
throw new Exception("Queue is Empty");
return _stack1.Pop();
}
}
public class Node
{
public int Data { get; set; }
public Node Next { get; set; }
public Node(int data)
{
this.Data = data;
}
}
public class Queue
{
private Node _head;
private Node _tail;
private int _count = 0;
public Queue() { }
public void Enqueue(int data)
{
Node _newNode = new Node(data);
if (_head == null)
{
_head = _newNode;
_tail = _head;
}
else
{
_tail.Next = _newNode;
_tail = _tail.Next;
}
_count++;
}
public int Dequeue()
{
if (_head == null)
{
throw new Exception("Queue is Empty");
}
int _result = _head.Data;
_head = _head.Next;
return _result;
}
public int Count
{
get
{
return this._count;
}
}
}
<?php
function getRemoteIPAddress(){
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
return $ip;
}
/* If your visitor comes from proxy server you have use another function
to get a real IP address: */
function getRealIPAddress(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
?>