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

Snippets

  • submit to reddit

Recent Snippets

                    public int FindNthFibonacciNumber(int n)
        {
            if (n == 1 || n == 2) return 1;

            int nthfibonacciNumber = FindNthFibonacciNumber(n - 1) + FindNthFibonacciNumber(n - 2);
            return nthfibonacciNumber;
        }                
                            public class BinaryTreeNode
        {
            public BinaryTreeNode Left { get; set; }

            public BinaryTreeNode Right { get; set; }

            public int Data { get; set; }
        }

public class DepthFirstSearch
        {
            private Stack<BinaryTreeNode> _searchStack;
            private BinaryTreeNode _root;

            public DepthFirstSearch(BinaryTreeNode rootNode)
            {
                _root = rootNode;
                _searchStack = new Stack<BinaryTreeNode>();
            }

            public bool Search(int data)
            {
                BinaryTreeNode _current;
                _searchStack.Push(_root);
                while (_searchStack.Count != 0)
                {
                    _current = _searchStack.Pop();
                    if (_current.Data == data)
                    {
                        return true;
                    }
                    else
                    {
                        _searchStack.Push(_current.Right);
                        _searchStack.Push(_current.Left);
                    }
                }
                return false;
            }
        }                
                    class BinaryTreeNode
        {
            public BinaryTreeNode Left { get; set; }

            public BinaryTreeNode Right { get; set; }

            public int Data { get; set; }
        }


        public class BreadthFirstSearch
        {
            private Queue<BinaryTreeNode> _searchQueue;
            private BinaryTreeNode _root;

            public BreadthFirstSearch(BinaryTreeNode rootNode)
            {
                _searchQueue = new Queue<BinaryTreeNode>();
                _root = rootNode;
            }

            public bool Search(int data)
            {
                BinaryTreeNode _current = _root;
                _searchQueue.Enqueue(_root);

                while (_searchQueue.Count != 0)
                {
                    _current = _searchQueue.Dequeue();
                    if (__current.Data == data)
                    {
                        return true;
                    }
                    else
                    {
                        _searchQueue.Enqueue(_current.Left);
                        _searchQueue.Enqueue(_current.Right);
                    }
                }

                return false;
            }
        }                
                    /// <summary>
        /// 3 Stacks are Implemented in this class using 1 array
        /// </summary>
        public class StacksUsingSingleArray
        {
            private static int _arrSize = 99;
            private int[] _arr = new int[_arrSize];
            private int[] _stacksAvailableSlots = new int[3];
            private int _stackSize = 0;

            public StacksUsingSingleArray()
            {
                _stackSize = _arrSize / 3;
                _stacksAvailableSlots[0] = 0 * _stackSize;
                _stacksAvailableSlots[1] = 1 * _stackSize; //33
                _stacksAvailableSlots[2] = 2 * _stackSize; //66

            }

            /// <summary>
            /// Push item on stack
            /// </summary>
            /// <param name="stackNum">stack number (ie. 0/1/2)</param>
            /// <param name="data"></param>
            public void PushOnStack(int stackNum, int data)
            {
                _arr[_stacksAvailableSlots[stackNum]++] = data;
            }

            /// <summary>
            /// Pop item from stack
            /// </summary>
            /// <param name="stackNum">stack number (ie. 0/1/2)</param>
            public int PopStack(int stackNum)
            {
                return _arr[--_stacksAvailableSlots[stackNum]];
            }

            /// <summary>
            /// Gives count of items in a stack
            /// </summary>
            /// <param name="stackNum">stack number (ie. 0/1/2)</param>
            /// <returns></returns>
            public int StackCount(int stackNum)
            {
                return (_stacksAvailableSlots[stackNum] - (_stackSize * stackNum));
            }

            /// <summary>
            /// Printing contents of stack
            /// </summary>
            /// <param name="stackNum">stack number (ie. 0/1/2)</param>
            public void PrintStack(int stackNum)
            {
                for (int i = stackNum * _stackSize; i < _stacksAvailableSlots[stackNum]; i++)
                {
                    Console.WriteLine(_arr[i]);
                }
            }
        }                
                    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();
            }
        }                
                    LS_COLORS='no=00:fi=00:di=01;33:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:';
export LS_COLORS                
                     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;
}
?>
                
                     public class LinkedList
        {
            Node _head;
            Node _tail;

            public LinkedList()
            { }
            

            public void AddLast(int data)
            {
                Node _newNode = new Node(data);
                if (_head == null)
                {
                    _tail = _newNode;
                    _head = _tail;
                }
                else
                {
                    _tail.Next = _newNode;
                    _tail = _newNode;
                }
            }

            public void Clear()
            {
                _head = null;
            }

            public void AddFirst(int data)
            {
                Node _newNode = new Node(data);
                if (_head == null)
                {
                    _head = _newNode;
                    _tail = _head;
                }
                else
                {
                    _newNode.Next = _head;
                    _head = _newNode;
                }
            }

            public Node FindNode(int data)
            {
                Node _current = _head;
                while (_current != null)
                {
                    if (_current.Data == data)
                        return _current;
                    else
                        _current = _current.Next;
                }
                return _current;
            }


            public void PrintList()
            {
                Node _current = _head;

                if (_head == null)
                    Console.WriteLine("Empty Linked List");
                else
                {
                    while (_current != null)
                    {
                        Console.Write(_current.Data.ToString() + " ");
                        _current = _current.Next;
                    }
                    Console.WriteLine();
                }
            }

            public void InsertAfter(int data, int _newData)
            {
                Node _newNode = new Node(_newData);
                Node _node = FindNode(data);
                if (_node != null)
                {
                    _newNode.Next = _node.Next;
                    _node.Next = _newNode;
                }
                else
                {
                    Console.WriteLine("No such data:" + data);
                }
            }

            public void InsertBefore(int data, int _newData)
            {
                Node _newNode = new Node(_newData);
                Node _current = _head;
                Node _previous = _current;
                while (_current != null)
                {
                    if (_current.Data == data)
                    {
                        _newNode.Next = _current;
                        _previous.Next = _newNode;
                        break;
                    }
                    else
                    {
                        _previous = _current;
                        _current = _current.Next;
                    }
                }
            }

            public void Remove(int data)
            {
                Node _current = _head;

                while (_current.Next != null)
                {
                    if (_current.Next.Data == data)
                    {
                        _current.Next = _current.Next.Next;
                        break;
                    }
                    else
                        _current = _current.Next;
                }
            }

            public void RemoveDuplicatesUsingBuffer()
            {
                HashSet<int> _hashSet = new HashSet<int>();
                Node _current = _head;
                Node _previous = _current;
                while (_current != null)
                {
                    if (_hashSet.Contains(_current.Data))
                    {
                        _previous.Next = _previous.Next.Next;
                        _current = _previous.Next;
                    }
                    else
                    {
                        _hashSet.Add(_current.Data);
                        _previous = _current;
                        _current = _current.Next;
                    }
                }
            }

            public void RemoveDuplicates()
            {
                Node _current = _head;
                Node _previous = _current;
                Node _curr = _current;

                while (_current != null)
                {
                    _curr = _head;
                    bool isDuplicate = false;
                    while (_curr != _current)
                    {
                        if (_curr.Data == _current.Data)
                        {
                            isDuplicate = true;
                            break;
                        }
                        else
                            _curr = _curr.Next;
                    }

                    if (isDuplicate)
                    {
                        _previous.Next = _previous.Next.Next;
                        _current = _previous.Next;
                    }
                    else
                    {
                        _previous = _current;
                        _current = _current.Next;
                    }
                }
            }


            public void Reverse()
            {
                Stack<Node> _stack = new Stack<Node>();
                Node _current = _head;
                while (_current != null)
                {
                    _stack.Push(_current);
                    _current = _current.Next;
                }

                _head = _stack.Pop();
                _current = _head;
                while (_stack.Count > 0)
                {
                    _current.Next = _stack.Pop();
                    _current = _current.Next;
                }
                _current.Next = null;
            }

            public void FindNthElementFromLast(int n)
            {
                Node _p1 = _head;
                Node _p2 = _p1;

                for (int i = 0; i < (n - 1); i++)
                {
                    _p2 = _p2.Next;
                }

                while (_p2.Next != null)
                {
                    _p1 = _p1.Next;
                    _p2 = _p2.Next;
                }

                Console.WriteLine(_p1.Data.ToString());
            }

            public Node First
            {
                get
                {
                    return _head;
                }
            }

        }                
                    function getURLParameter(name){
  return decodeURI(
    (RegEXP(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
  );
}