示例#1
0
 static void Main(string[] args)
 {
     GenericLinkedList<System.Object> gll = new GenericLinkedList<System.Object>();
     gll.Add(12);
     gll.Add("string");
     gll.Add(false);
     gll.DisplayNodes();
 }
 public void Test_GenericLinkedList_Method_Add_BadIndexEmptyList()
 {
     linkedList = new GenericLinkedList <int>();
     testBool   = linkedList.Add(zeroInt, 1);
     Assert.IsFalse(testBool);
     Assert.IsNull(linkedList.FirstNode);
     Assert.IsNull(linkedList.LastNode);
     testBool = linkedList.Add(zeroInt, -1);
     Assert.IsFalse(testBool);
     Assert.IsNull(linkedList.FirstNode);
     Assert.IsNull(linkedList.LastNode);
 }
 public void Test_GenericLinkedList_Method_Add_BadIndex()
 {
     linkedList = new GenericLinkedList <int>();
     testBool   = linkedList.Add(zeroInt, 0);
     Assert.IsTrue(testBool);
     testBool = linkedList.Add(oneInt, 1);
     Assert.IsTrue(testBool);
     testBool = linkedList.Add(negOneInt, 2);
     Assert.IsTrue(testBool);
     testBool = linkedList.Add(zeroInt, 4);
     Assert.IsFalse(testBool);
     testBool = linkedList.Add(zeroInt, -1);
     Assert.IsFalse(testBool);
 }
示例#4
0
        private static void ExecuteCommands(GenericLinkedList <int> linkedList)
        {
            var numberOfCommands = int.Parse(Console.ReadLine());

            while (numberOfCommands > 0)
            {
                var command = Console.ReadLine().Split();
                var number  = int.Parse(command[1]);

                switch (command[0])
                {
                case "Add":
                    linkedList.Add(number);
                    break;

                case "Remove":
                    linkedList.Remove(number);
                    break;

                default:
                    break;
                }
                numberOfCommands--;
            }
        }
 public void Test_GenericLinkedList_Method_Add_SuccessOnlyFirstIndex()
 {
     linkedList = new GenericLinkedList <int>();
     testBool   = linkedList.Add(zeroInt, 0);
     Assert.IsTrue(testBool);
     Assert.AreEqual(zeroInt, linkedList.FirstNode.Data);
     Assert.AreEqual(linkedList.FirstNode, linkedList.LastNode);
     testBool = linkedList.Add(oneInt, 0);
     Assert.IsTrue(testBool);
     Assert.AreEqual(oneInt, linkedList.FirstNode.Data);
     Assert.AreEqual(zeroInt, linkedList.LastNode.Data);
     Assert.AreEqual(linkedList.FirstNode, linkedList.LastNode.Previous);
     Assert.AreEqual(linkedList.LastNode, linkedList.FirstNode.Next);
     testBool = linkedList.Add(negOneInt, 0);
     Assert.IsTrue(testBool);
     Assert.AreEqual(negOneInt, linkedList.FirstNode.Data);
     Assert.AreEqual(oneInt, linkedList.FirstNode.Next.Data);
     Assert.AreEqual(zeroInt, linkedList.FirstNode.Next.Next.Data);
     Assert.AreEqual(zeroInt, linkedList.LastNode.Data);
     Assert.AreEqual(oneInt, linkedList.LastNode.Previous.Data);
     Assert.AreEqual(negOneInt, linkedList.LastNode.Previous.Previous.Data);
 }
示例#6
0
        static void Main(string[] args)
        {
            GenericLinkedList <int> linkedListOfNumbers = new GenericLinkedList <int>();

            for (int i = 0; i < 5; i++)
            {
                linkedListOfNumbers.Add(i);
            }

            foreach (var number in linkedListOfNumbers)
            {
                Console.WriteLine(number);
            }


            List <string> stringlist = new List <string>();
        }
示例#7
0
        static void Main(string[] args)
        {
            //Define a new linked list to use
            MyLinkedList myLinkedList = new MyLinkedList();

            //Add a bunch of stuff to it
            myLinkedList.Add("first");
            myLinkedList.Add("second");
            myLinkedList.Add("third");
            myLinkedList.Add("fourth");

            //Loop through with this differently looking for loop to output
            //In here, the first part is initalization: Setting x to the Head
            //the second part is the test: If x != null, keep going
            //The last part is: Set the current x to x's next porinter. (The next in the list)
            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Print out the 2nd one
            Node nodeINeed = myLinkedList.Retrive(2);
            Console.WriteLine(nodeINeed.Data);

            //Print out the 2nd one again in one statement
            Console.WriteLine(myLinkedList.Retrive(2).Data);

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Delete the 2nd element in the list
            myLinkedList.Delete(2);
            //Delete the new 2nd element in the list. Was 3rd before previous delete
            myLinkedList.Delete(2);

            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }

            //Couple of blank lines
            Console.WriteLine();
            Console.WriteLine();

            //Add two new ones to the list
            myLinkedList.Add("fifth");
            myLinkedList.Add("sixth");

            //Print the list one last time
            for (Node x = myLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }

            Console.WriteLine("***************************************");
            Console.WriteLine("***************************************");

            //A generic linked list that sends in the type that we would like to use
            //This one will behave exactly like the one used above since it is taking a
            //string.
            GenericLinkedList<string> myGenericLinkedList = new GenericLinkedList<string>();

            //Some other linked lists that can use the generic one. One of them is of type
            //integer, and the other is of type Object
            GenericLinkedList<int> myOtherGenericLinkedLIst = new GenericLinkedList<int>();
            GenericLinkedList<Object> myObjectGenericLinkedList = new GenericLinkedList<object>();

            //Use the generic string one to do the same work as above
            //Add a bunch of stuff to it
            myGenericLinkedList.Add("first");
            myGenericLinkedList.Add("second");
            myGenericLinkedList.Add("third");
            myGenericLinkedList.Add("fourth");

            //Loop through with this differently looking for loop to output
            //In here, the first part is initalization: Setting x to the Head
            //the second part is the test: If x != null, keep going
            //The last part is: Set the current x to x's next porinter. (The next in the list)
            for (GenericNode<string> x = myGenericLinkedList.Head; x != null; x = x.Next)
            {
                Console.WriteLine(x.Data);
            }
        }