Exemplo n.º 1
0
        } // end method InsertAtBack

        // remove first node from List
        public object RemoveFromFront()
        {
            if (IsEmpty())
            {
                throw new EmptyListException(name);
            }

            object removeItem = firstNode.Data; // retrieve data

            // reset firstNode and lastNode references
            if (firstNode == lastNode)
            {
                firstNode = lastNode = null;
            }
            else
            {
                firstNode = firstNode.Next;
            }

            return(removeItem); // return removed data
        } // end method RemoveFromFront
Exemplo n.º 2
0
        } // end method IsEmpty

        // output List contents
        public void Display()
        {
            if (IsEmpty())
            {
                Console.WriteLine("Empty " + name);
            } // end if
            else
            {
                Console.Write("The " + name + " is: ");

                ListNode current = firstNode;

                // output current node data while not at end of list
                while (current != null)
                {
                    Console.Write(current.Data + " ");
                    current = current.Next;
                } // end while

                Console.WriteLine("\n");
            } // end else
        }     // end method Display
Exemplo n.º 3
0
        private string name; // string like "list" to display

        #endregion Fields

        #region Constructors

        // construct empty List with specified name
        public List( string listName )
        {
            name = listName;
             firstNode = lastNode = null;
        }
Exemplo n.º 4
0
 // constructor to create ListNode that refers to dataValue
 // and refers to next ListNode in List
 public ListNode( object dataValue, ListNode nextNode )
 {
     Data = dataValue;
      Next = nextNode;
 }
Exemplo n.º 5
0
        // remove first node from List
        public object RemoveFromFront()
        {
            if ( IsEmpty() )
            throw new EmptyListException( name );

             object removeItem = firstNode.Data; // retrieve data

             // reset firstNode and lastNode references
             if ( firstNode == lastNode )
            firstNode = lastNode = null;
             else
            firstNode = firstNode.Next;

             return removeItem; // return removed data
        }
Exemplo n.º 6
0
        // remove last node from List
        public object RemoveFromBack()
        {
            if ( IsEmpty() )
            throw new EmptyListException( name );

             object removeItem = lastNode.Data; // retrieve data

             // reset firstNode and lastNode references
             if ( firstNode == lastNode )
            firstNode = lastNode = null;
             else
             {
            ListNode current = firstNode;

            // loop while current node is not lastNode
            while ( current.Next != lastNode )
               current = current.Next; // move to next node

            // current is new lastNode
            lastNode = current;
            current.Next = null;
             } // end else

             return removeItem; // return removed data
        }
Exemplo n.º 7
0
 // Insert object at front of List. If List is empty,
 // firstNode and lastNode will refer to same object.
 // Otherwise, firstNode refers to new node.
 public void InsertAtFront( object insertItem )
 {
     if ( IsEmpty() )
     firstNode = lastNode = new ListNode( insertItem );
      else
     firstNode = new ListNode( insertItem, firstNode );
 }
Exemplo n.º 8
0
 // Insert object at end of List. If List is empty,
 // firstNode and lastNode will refer to same object.
 // Otherwise, lastNode's Next property refers to new node.
 public void InsertAtBack( object insertItem )
 {
     if ( IsEmpty() )
     firstNode = lastNode = new ListNode( insertItem );
      else
     lastNode = lastNode.Next = new ListNode( insertItem );
 }
Exemplo n.º 9
0
        private string name; // string like "list" to display

        // construct empty List with specified name
        public List(string listName)
        {
            name      = listName;
            firstNode = lastNode = null;
        } // end constructor
Exemplo n.º 10
0
        } // end default constructor

        // constructor to create ListNode that refers to dataValue
        // and refers to next ListNode in List
        public ListNode(object dataValue, ListNode nextNode)
        {
            Data = dataValue;
            Next = nextNode;
        } // end constructor
Exemplo n.º 11
0
 // constructor to create ListNode that refers to dataValue
 // and refers to next ListNode in List
 public ListNode(double dataValue, ListNode nextNode)
 {
     Data = dataValue;
     Next = nextNode;
 }