Hello all,
This will be a bit of a long post due to the coding, my apologies.
This is a homework assignment, I am soooo close to finishing it.
I just have a couple of pesky bugs I am pulling my, what little I have left, hair.
We are using the Deitel Visual C# and the LinkedListLibrary and the StackInheritance
are used from the book. My part of the program that is using these 2 sources is my problem.
First off for using both the StackInheritance class and then the System.Collections.Stack
I am to see how low it takes for it to insert 10000, 100000, 1000000 and 10000000 random integers. Then record how long it takes to insert/push these number of items(emptying the stack after each run).
The Program.cs is the part I am needing help with and am sending the complete program in 3 sections.
// Fig. 25.4: LinkedListLibrary.cs
// Class ListNode and class List declarations.
using System;
namespace LinkedListLibrary
{
// class to represent one node in a list
class ListNode
{
private object data; // stores data for this node
private ListNode next; // stores a reference to the next node
// constructor to create ListNode that refers to dataValue
// and is last node in list
public ListNode( object dataValue )
: this( dataValue, null )
{
} // 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
// property Next
public ListNode Next
{
get
{
return next;
} // end get
set
{
next = value;
} // end set
} // end property Next
// property Data
public object Data
{
get
{
return data;
} // end get
} // end property Data
} // end class ListNode
// class List declaration
public class List
{
private ListNode firstNode;
private ListNode lastNode;
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
// construct empty List with "list" as its name
public List()
: this( "list" )
{
} // end default constructor
// 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 );
} // end method InsertAtFront
// 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 );
} // 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
// 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
} // end method RemoveFromBack
// return true if List is empty
public bool IsEmpty()
{
return firstNode == null;
} // end method IsEmpty
// output List contents
public void Print()
{
if ( IsEmpty() )
{
Console.WriteLine( "Empty " + name );
return;
} // end if
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 method Print
} // end class List
// class EmptyListException declaration
public class EmptyListException : ApplicationException
{
public EmptyListException( string name )
: base( "The " + name + " is empty" )
{
} // end constructor
} // end class EmptyListException
} // end namespace LinkedListLibrary
/**************************************************************************
* (C) Copyright 1992-2006 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/