Exemple #1
0
        public static MySingleLLNode AddAscOrder(MySingleLLNode list, int data)
        {
            MySingleLLNode node = new MySingleLLNode(data);
            MySingleLLNode head = list;

            if (list == null)
            {
                return(node);
            }
            else if (list.Data >= data)
            {
                return(AddFirst(list, data));
            }

            // find the first element which is grater than Data
            while (head.Next != null && head.Next.Data < data)
            {
                head = head.Next;
            }

            node.Next = head.Next;
            head.Next = node;

            return(list);
        }
Exemple #2
0
        public static MySingleLLNode AddFirst(MySingleLLNode list, int data)
        {
            MySingleLLNode node = new MySingleLLNode(data);

            node.Next = list;
            list      = node;
            return(list);
        }
Exemple #3
0
        public static MySingleLLNode AddLast(MySingleLLNode list, int data)
        {
            MySingleLLNode node = new MySingleLLNode(data);
            MySingleLLNode head = list;

            while (head != null && head.Next != null)
            {
                head = head.Next;
            }

            if (head == null)
            {
                return(node);
            }
            else
            {
                head.Next = node;
            }

            return(list);
        }
Exemple #4
0
        public static void PrintList(MySingleLLNode head)
        {
            if (head == null)
            {
                throw new NullReferenceException("List is not initialized");
            }

            StringBuilder sb = new StringBuilder();

            while (head != null)
            {
                sb.Append(head.Data);
                if (head.Next != null)
                {
                    sb.Append(", ");
                }
                head = head.Next;
            }

            Console.WriteLine(sb.ToString());
        }
Exemple #5
0
        public static bool DeleteMe(ref MySingleLLNode list, int data)
        {
            MySingleLLNode head = list;

            // is the List empty?
            if (head == null)
            {
                throw new NullReferenceException("List not initialized");
            }

            // Is head.Data == data ? => DeleteFirst.
            if (head.Data == data)
            {
                list = list.Next;
                return(true);
            }

            // if not, iterate until you get the node N where N.Next.Data == data OR N.Next == Null
            while (head.Next != null && head.Next.Data != data)
            {
                head = head.Next;
            }

            // N.Next == Null => No element found. return False.
            //if (head.Next == null)
            //    return false;
            if (head.Next != null && head.Next.Data == data)
            {
                head.Next = head.Next.Next;
                return(true);
            }

            return(false);
            // Otherwise => Found N.
            // Set N.Next = N.Next.Next
        }
Exemple #6
0
        static void MainNEW(string[] args)
        {
            IntBuffer buff = new IntBuffer(8);
            Producer  p    = new Producer(buff);
            Consumer  c    = new Consumer(buff);

            Thread producer = new Thread(new ThreadStart(p.Run));
            Thread consumer = new Thread(new ThreadStart(c.Run));

            producer.Start();
            consumer.Start();



            string aaa = "aa";
            string bbb = "bb";

            Console.WriteLine("aa={0}, bb={1}", aaa, bbb);
            Permutation.SwapUs <string>(ref aaa, ref bbb);
            Console.WriteLine("aa={0}, bb={1}", aaa, bbb);

            int[] arr = new int[] { 2, 3 };
            Console.WriteLine("arr[0]={0}, arr[1]={1}", arr[0], arr[1]);
            Permutation.SwapUs <int>(ref arr[0], ref arr[1]);
            Console.WriteLine("arr[0]={0}, arr[1]={1}", arr[0], arr[1]);


            // find first non repeating char
            string ss = "IKiNAKAP";

            Console.WriteLine("first distinct char is = '{0}'", FindFirstNonRepeatingChar2(ss));
            ////

            // In place string update.
            ss = "Battle of Vowels: Indiana Jones";
            Console.WriteLine(RemoveSelectedChars(ss, "aeiou"));

            // In place string reversal
            ss = "This is a test string.";
            Console.WriteLine(InPlaceStringReversal(ss));

            // Int to String
            ss = IntToString(284);
            ss = IntToString(-4484);
            ss = IntToString(0);
            ss = IntToString(+23);
            ss = IntToString(+0);

            // string to int
            int z = StringToInt("-4484");

            z = StringToInt("284");
            z = StringToInt("0");

            FindDupWithoutExtraBuffer("HELLO");

            // Permutation n!
            Permutation permutation = new Permutation();
            string      inStr       = "ABCD";

            // permutation.setper(inStr.ToCharArray());
            permutation.SetPermutation(inStr);

            permutation.SetCombi(inStr);



            // phone number combinations
            Console.WriteLine("phone number combinations");
            Console.WriteLine("------------------------------");
            //Console.WriteLine(PhoneNumberChars.GetChar(0, 0));
            //Console.WriteLine(PhoneNumberChars.GetChar(1, 0));
            //Console.WriteLine(PhoneNumberChars.GetChar(0, 1));
            //Console.WriteLine(PhoneNumberChars.GetChar(1, 1));
            //Console.WriteLine(PhoneNumberChars.GetChar(2, 3));
            //Console.WriteLine(PhoneNumberChars.GetChar(9, 1));
            //Console.WriteLine(PhoneNumberChars.GetChar(9, 0));

            permutation.SetPhoneCharConfig(new byte[] { 7, 4, 2, 3 });



            string[] xxx = new string[2];
            string[] yyy = new string[3];
            yyy[2] = "hello";
            xxx    = yyy;
            Console.WriteLine(xxx[2]);
            ///
            string aa = "barista";
            string bb = "far";

            char[] final = aa.ToCharArray();
            if (bb.Length <= aa.Length)
            {
                Array.Copy(bb.ToCharArray(), 0, final, 0, bb.Length);
            }
            Console.WriteLine(final);
            //
            //int[,] arr = new int[2, 3]; // [col, row]
            //arr[1, 2] = 20;

            Collections();
            string x = null;

            Console.WriteLine("reverse of empty = {0}", ReverseMe(string.Empty));
            Console.WriteLine("reverse of null = {0}", ReverseMe(x));
            x = "seetaphal";
            Console.WriteLine("reverse of x = {0}", ReverseMe(x));

            x = "this is seetaphal";
            Console.WriteLine("reverse of x = {0}", ReverseMe(x));
            Console.Read();

            MySingleLLNode head = MySingleLinkedList.CreateList(2);

            MySingleLinkedList.AddLast(head, 5);
            head = MySingleLinkedList.AddFirst(head, 1);
            head = MySingleLinkedList.AddAscOrder(head, 3);
            head = MySingleLinkedList.AddAscOrder(head, 7);
            head = MySingleLinkedList.AddAscOrder(head, 1);
            head = MySingleLinkedList.AddAscOrder(head, 6);
            head = MySingleLinkedList.AddAscOrder(head, 4);

            MySingleLinkedList.PrintList(head);

            MySingleLinkedList.DeleteMe(ref head, 1);  //delete First
            MySingleLinkedList.PrintList(head);
            MySingleLinkedList.DeleteMe(ref head, 7);  //delete last
            MySingleLinkedList.PrintList(head);
            MySingleLinkedList.DeleteMe(ref head, 59); //delete Non existent
            MySingleLinkedList.PrintList(head);
            MySingleLinkedList.DeleteMe(ref head, 5);  //delete 5
            MySingleLinkedList.PrintList(head);
            //MySingleLinkedList.DeleteMe(null, 1); //delete from Null

            MySingleLinkedList.PrintList(head);

            Console.Read();
        }