Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Question 2.3
            //
            // Implement an algorithm to delete a node in the middle of singly
            // linked list, given only access to that node.
            // EXAMPLE
            // Input: the node c from the linked list a->b->c->d->e
            // Result: nothing is returned, but the new li nked list looks like a->b->d->e

            Random rand = new Random();
            Node seth = new Node(500);
            for (int x = 0; x <= 10; x++)
            {
                seth.appendToTail(rand.Next(0, 10));
            }

            Console.WriteLine("LinkedList: ");
            print_node(seth);

            // remove the 3rd node
            Node to_be_deleted = seth.next.next;

            remove_node(ref to_be_deleted);

            Console.WriteLine("After removing 3rd node: ");
            print_node(seth);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public void appendToTail(int d)
        {
            Node end = new Node(d);
            Node n = this;
            while (n.next != null)
            {
                n = n.next;
            }

            n.next = end;
        }
Ejemplo n.º 3
0
 public static void print_node(Node head)
 {
     Console.WriteLine("node level 1: " + head.data);
     int level = 2;
     Node runner = head;
     while (runner.next != null)
     {
         runner = runner.next;
         Console.WriteLine("node level " + level + ": " + runner.data);
         level++;
     }
 }
Ejemplo n.º 4
0
        public static void remove_node(ref Node to_delete)
        {
            Node replace;
            if (to_delete.next == null)
            {
                to_delete = null;
                return;
            }

            replace = to_delete.next;
            to_delete.data = replace.data;
            to_delete.next = replace.next;
        }