Exemplo n.º 1
0
        static void LinkedListMethods()
        {
            LinkedList Parts = new LinkedList();
            Node       Part;
            Node       PartToFind;

            //add to end
            Part            = new Node();
            Part.NodeNumber = 1;
            Part.NodeName   = "Air Filter";
            Part.NodePrice  = 8.75;
            Parts.Push(Part);

            Part            = new Node();
            Part.NodeNumber = 2;
            Part.NodeName   = "Clutch Disk";
            Part.NodePrice  = 47.15;
            Parts.Push(Part);

            Part            = new Node();
            Part.NodeNumber = 3;
            Part.NodeName   = "Brake Disk";
            Part.NodePrice  = 35.15;
            Parts.Push(Part);

            //duplicate
            Part            = new Node();
            Part.NodeNumber = 2;
            Part.NodeName   = "Clutch Disk";
            Part.NodePrice  = 47.15;
            Parts.Push(Part);

            Part            = new Node();
            Part.NodeNumber = 4;
            Part.NodeName   = "A/C Filter Drier";
            Part.NodePrice  = 55.55;
            Parts.Push(Part);

            //add to end
            //Part = new Node();
            //Part.NodeNumber = 1;
            //Part.NodeName = "Air Filter";
            //Part.NodePrice = 8.75;
            //Parts.Add(Part, false);

            //Part = new Node();
            //Part.NodeNumber = 2;
            //Part.NodeName = "Clutch Disk";
            //Part.NodePrice = 47.15;
            //Parts.Add(Part, false);

            //Part = new Node();
            //Part.NodeNumber = 3;
            //Part.NodeName = "Brake Disk";
            //Part.NodePrice = 35.15;
            //Parts.Add(Part, false);

            //Part = new Node();
            //Part.NodeNumber = 4;
            //Part.NodeName = "A/C Filter Drier";
            //Part.NodePrice = 55.55;
            //Parts.Add(Part, false);

            Console.WriteLine(" -=- Store Inventory -=-");
            Console.WriteLine("Number of Parts: {0}", Parts.Count);

            //var node = Parts.Retrieve(0);
            //Console.WriteLine("Retrieve position {0}: node number {1}", 0, node.NodeNumber);


            for (int i = 0; i < Parts.Count; i++)
            {
                Node part = Parts.Retrieve(i);
                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.NodeNumber);
                Console.WriteLine("Description: {0}", part.NodeName);
                Console.WriteLine("Unit Price:  {0:C}", part.NodePrice);
            }


            //delete duplicated node;
            Parts.DeleteDups(Parts);


            //Parts.TravesLinked();
            var popNode = Parts.Pop();

            Console.WriteLine("1st Pop node number {0}", popNode.NodeNumber);

            for (int i = 0; i < Parts.Count; i++)
            {
                Node part = Parts.Retrieve(i);
                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.NodeNumber);
                Console.WriteLine("Description: {0}", part.NodeName);
                Console.WriteLine("Unit Price:  {0:C}", part.NodePrice);
            }

            popNode = Parts.Pop();
            Console.WriteLine("2nd Pop node number {0}", popNode.NodeNumber);

            for (int i = 0; i < Parts.Count; i++)
            {
                Node part = Parts.Retrieve(i);
                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.NodeNumber);
                Console.WriteLine("Description: {0}", part.NodeName);
                Console.WriteLine("Unit Price:  {0:C}", part.NodePrice);
            }

            //Delete head
            Parts.Delete();

            //Delete at a position
            Parts.Delete(2);

            for (int i = 0; i < Parts.Count; i++)
            {
                Node part = Parts.Retrieve(i);
                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.NodeNumber);
                Console.WriteLine("Description: {0}", part.NodeName);
                Console.WriteLine("Unit Price:  {0:C}", part.NodePrice);
            }

            //find a node
            PartToFind            = new Node();
            PartToFind.NodeNumber = 3;
            PartToFind.NodeName   = "Brake Disk";
            PartToFind.NodePrice  = 35.15;

            Node found = Parts.Find(PartToFind);

            if (found != null)
            {
                Console.WriteLine(string.Format("Item was found withe Part #{0}", found.NodeNumber));
            }
            else
            {
                Console.WriteLine("\nItem not found\n");
            }

            Part            = new Node();
            Part.NodeNumber = 3;
            Part.NodeName   = "Brake Disk";
            Part.NodePrice  = 35.15;
            Parts.Delete(Part);
            for (int i = 0; i < Parts.Count; i++)
            {
                Node part = Parts.Retrieve(i);
                Console.WriteLine("\nCar Part Information");
                Console.WriteLine("Part #:      {0}", part.NodeNumber);
                Console.WriteLine("Description: {0}", part.NodeName);
                Console.WriteLine("Unit Price:  {0:C}", part.NodePrice);
            }

            Console.WriteLine(" -=- Store Inventory -=-");
            Console.WriteLine("Number of Parts: {0}", Parts.Count);
        }