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 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]
  );
}                
                    Func<string, string> RemoveDuplicate = delegate(string s)
            {
                BitArray _arr = new BitArray(256);
                StringBuilder _sb = new StringBuilder();
                s = s.ToLower();
                for (int i = 0; i < s.Length; i++)
                {
                    if (_arr[(int)s[i]])
                    {
                        continue;
                    }
                    else
                    {
                        _arr[(int)s[i]] = true;
                        _sb.Append(s[i]);
                    }
                }
                return _sb.ToString();
            };                
                    Func<string, string> ReverseString = delegate(string s)
            {
                char[] word = s.ToCharArray();
                for (int i = 0, j = (s.Length - 1); i < j; i++, j--)
                {
                    char _temp = word[i];
                    word[i] = s[j];
                    word[j] = _temp;
                }

                return new string(word);
            };                
                    
string strCheck = '';
bool checkString = String.IsNullOrEmpty(strText);
                
                    
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<string> query =
  Enumerable.Select (
    Enumerable.OrderBy (
      Enumerable.Where (
        names, n => n.Contains ("a")
      ), n => n.Length
    ), n => n.ToUpper()
  );