示例#1
0
 // creat a public default Constructor (can constructor be private???)
 public LinkedList()
 {
     // set default values
     // assign null to Node head(both data and next are null???)
     this.head = null;
     // assign 0 to count
     this.count = 0;
 }
示例#2
0
        // creat a method named Add which has object data type output and has two argument int index and object o
        public object Add(int index, object o)
        {
            // use if statement to control the user specified index won't smaller than 0
            if ( index < 0)
            {// if index < 0, throw a exception
                throw new ArgumentOutOfRangeException("Index " + index);
            }
            // use if statement to control the user specified index won't bigger than count
            if ( index > count)
            {// if index > count, assign the heap of count to index
                index = count;
            }

            // current point to the same heap that head point to
            Node current = this.head;
            // use if statement to deal with Empty is true or index == 0 is ture
            if (this.Empty || index == 0)
            {// when count is 0 and index is 0
                //creat a Node object and invoke Node class constructor. assign o to data and assign head to next.Then head point to this Node object
                // Node X = new Node();
                // X.data = o;
                // X.Next = head;
                // head = X;
                this.head = new Node(o, this.head);
            }
            // when count is not 0 and index == 0 is fasle
            else
            {// use for loop to process current = current.Next which measn change the heap that current point to until the loop stop
                for (int i = 0; i < index -1; i++ )
                {
                    current = current.Next;
                }
                // Node x = new Node(o, current.Next);
                // x.data = o;
                // x.Next = current.Next;
                // current.Next = x;
                current.Next = new Node(o, current.Next);

            }
            // count++
            count++;
            // return o
            return o;
        }
示例#3
0
 // this is a constructor with two argument which is declared above :data and next
 public Node(object data, Node next)
 {
     //???
     this.data = data;
     this.next = next;
 }