示例#1
0
        public void linkedListDataTest()
        {
            LinkedListCell <int> llcell = new LinkedListCell <int>();

            llcell.Data = 5;
            Assert.AreEqual(llcell.Data, 5);
        }
示例#2
0
        /// <summary>
        /// Adds an edge from the given source to the given destination having the given value.
        /// If either node is not already in the graph, it is added. If either node is null, throws
        /// an ArgumentNullException. If the nodes are the same or if the graph already contains this
        /// edge, throws an ArgumentException
        /// </summary>
        /// <param name="source">The source node.</param>
        /// <param name="dest">The destination node.</param>
        /// <param name="value">The value associated with the edge.</param>
        public void AddEdge(TNode source, TNode dest, TEdgeData value)
        {
            if (source == null || dest == null)
            {
                throw new ArgumentNullException();
            }
            Tuple <TNode, TNode> temp = new Tuple <TNode, TNode>(source, dest);

            if (source.Equals(dest) || _dictionary.ContainsKey(temp))
            {
                throw new ArgumentException();
            }
            _dictionary.Add(temp, value);
            LinkedListCell <TNode> list;

            _adjacencyLists.TryGetValue(source, out list);
            LinkedListCell <TNode> i = new LinkedListCell <TNode>();

            i.Data = dest;
            i.Next = list;
            _adjacencyLists[source] = i;
            if (!_adjacencyLists.ContainsKey(dest))
            {
                _adjacencyLists.Add(dest, null);
            }
        }
示例#3
0
        /// <summary>
        /// Inserts the given key and value into a new cell at the beginning of the linked list at the
        /// given table location.
        /// </summary>
        /// <param name="k">The key to insert.</param>
        /// <param name="v">The value to insert.</param>
        /// <param name="loc">The table location containing the linked list in which the given key and
        /// value are to be inserted.</param>
        private void Insert(TKey k, TValue v, int loc)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = new LinkedListCell <KeyValuePair <TKey, TValue> >();

            cell.Data = new KeyValuePair <TKey, TValue>(k, v);
            Insert(cell, loc);
            _values++;

            if (_values > _table.Length)
            {
                if (_currentIndex < _table.Length - 1) //before wrong
                {
                    LinkedListCell <KeyValuePair <TKey, TValue> >[] oldTable = new LinkedListCell <KeyValuePair <TKey, TValue> > [_table.Length];
                    oldTable = _table;
                    _table   = new LinkedListCell <KeyValuePair <TKey, TValue> > [_tableSizes[++_currentIndex]];
                    for (int i = 0; i < oldTable.Length; i++)
                    {
                        while (oldTable[i] != null)
                        {
                            LinkedListCell <KeyValuePair <TKey, TValue> > temp = oldTable[i];
                            oldTable[i] = oldTable[i].Next;

                            int location = GetLocation(temp.Data.Key);
                            Insert(temp, location);
                        }
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// builds the game board using _board dictionary
        /// and each row is a linked list
        /// </summary>
        private void CreateBoard()
        {
            _board = new Dictionary <int, LinkedListCell <BoardSquare> >();
            for (int i = 1; i <= 8; i++)
            {
                _board[i] = null;


                for (int j = 1; j <= 8; j++)
                {
                    BoardSquare create = new BoardSquare(i, j);
                    create.King = false;

                    if (i < 4 && j % 2 != i % 2)
                    {
                        create.Color = SquareColor.Red;
                        _redCount++;
                    }
                    else if (i > 5 && j % 2 != i % 2)
                    {
                        create.Color = SquareColor.Black;
                        _blackCount++;
                    }
                    else
                    {
                        create.Color = SquareColor.None;
                    }

                    LinkedListCell <BoardSquare> temp = new LinkedListCell <BoardSquare>();
                    temp.Data = create;
                    temp.Next = _board[i];
                    _board[i] = temp;
                } //close inner for
            }     //close outer for
        }         //close CreateBoard
示例#5
0
        public void LinkedListTest1()
        {
            // Arrange
            LinkedListCell <int> firstCell  = new LinkedListCell <int>(1);
            LinkedListCell <int> secondCell = new LinkedListCell <int>(2);
            LinkedListCell <int> thirdCell  = new LinkedListCell <int>(3);
            LinkedListCell <int> fourthCell = new LinkedListCell <int>(4);
            LinkedListCell <int> fifthCell  = new LinkedListCell <int>(5);

            firstCell.Next  = secondCell;
            secondCell.Next = thirdCell;
            thirdCell.Next  = fourthCell;
            fourthCell.Next = fifthCell;
            fifthCell.Next  = null;

            // Act
            int result      = 0;
            var currentCell = firstCell;

            while (currentCell != null)
            {
                result     += currentCell.Data;
                currentCell = currentCell.Next;
            }

            // Assert
            Assert.Equal(result, 15);
        }
示例#6
0
        /// <summary>
        /// Inserts a cell containing the given key and value into the beginning of the linked list at the given
        /// location of the hash table.
        /// </summary>
        /// <param name="k">The key to insert.</param>
        /// <param name="v">The value associated with the key.</param>
        /// <param name="loc">The table location at which to insert the key and value.</param>
        private void Insert(TKey k, TValue v, int loc)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = new LinkedListCell <KeyValuePair <TKey, TValue> >();

            cell.Data = new KeyValuePair <TKey, TValue>(k, v);
            Insert(cell, loc);
            _numKeys++;
            //if the num of keys exceeds the num of array locations
            if (_numKeys > _table.Length)
            {
                //if the index into the array of allowable table sizes is at a location prior to the last
                if (_currentTableIndex != _tableSizes.Length - 1)
                {
                    LinkedListCell <KeyValuePair <TKey, TValue> >[] tempTable = _table;
                    _currentTableIndex++;
                    _table = new LinkedListCell <KeyValuePair <TKey, TValue> > [_tableSizes[_currentTableIndex]];

                    //moving all keys and values from old table to new one
                    for (int i = 0; i < tempTable.Length; i++)
                    {
                        while (_table[i] != null)
                        {
                            //Save a reference to the first cell in the linked list cell at table location
                            LinkedListCell <KeyValuePair <TKey, TValue> > tempCell = _table[i];

                            //remove this cell from the linked list
                            _table[i] = _table[i].Next;

                            //use hash function, compute new array location of the key in this cell
                            Insert(tempCell, GetLocation(tempCell.Data.Key));
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Inserts the given key and value into a new cell at the beginning of the linked list at the
        /// given table location.
        /// </summary>
        /// <param name="k">The key to insert.</param>
        /// <param name="v">The value to insert.</param>
        /// <param name="loc">The table location containing the linked list in which the given key and
        /// value are to be inserted.</param>
        private void Insert(TKey k, TValue v, int loc)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = new LinkedListCell <KeyValuePair <TKey, TValue> >();

            cell.Data = new KeyValuePair <TKey, TValue>(k, v);
            Insert(cell, loc);
        }
        /// <summary>
        /// Inserts the given cell into the beginning of the linked list at the given location of the table.
        /// </summary>
        /// <param name="k"></param>
        /// <param name="v"></param>
        /// <param name="loc"></param>
        private void Insert(TKey k, TValue v, int loc)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > temp = new LinkedListCell <KeyValuePair <TKey, TValue> >();

            temp.Data = new KeyValuePair <TKey, TValue>(k, v);
            Insert(temp, loc);
        }
示例#9
0
        /// <summary>
        /// Read the selected file and parse it into a LinkedListCell
        /// </summary>
        /// <param name="s">The path of the file</param>
        /// <returns>returns a ListLinkedCell</returns>
        private LinkedListCell <PhoneCount> ReadFile(string s)
        {
            LinkedListCell <PhoneCount> cell = new LinkedListCell <PhoneCount>();

            using (StreamReader input = new StreamReader(uxOpenDialog.FileName))
            {
                while (!input.EndOfStream)
                {
                    string num = input.ReadLine().Trim();

                    if (cell == null)
                    {
                        PhoneCount phone = new PhoneCount(num, 1);
                        LinkedListCell <PhoneCount> newCell = new LinkedListCell <PhoneCount>();
                        newCell.Data = phone;
                        newCell.Next = cell;
                        cell         = newCell;
                    }
                    else if (CheckIt(cell, num) == true)
                    {
                        continue;
                    }
                    else
                    {
                        PhoneCount phone = new PhoneCount(num, 1);
                        LinkedListCell <PhoneCount> newCell = new LinkedListCell <PhoneCount>();
                        newCell.Data = phone;
                        newCell.Next = cell;
                        cell         = newCell;
                    }
                }
            }
            return(cell);
        }
示例#10
0
        /// <summary>
        /// Adds an edge from the given source to the given destination having the given value.
        /// If either node is not already in the graph, it is added. If either node is null, throws
        /// an ArgumentNullException. If the nodes are the same or if the graph already contains this
        /// edge, throws an ArgumentException
        /// </summary>
        /// <param name="source">The source node.</param>
        /// <param name="dest">The destination node.</param>
        /// <param name="value">The value associated with the edge.</param>
        public void AddEdge(TNode source, TNode dest, TEdgeData value)
        {
            Tuple <TNode, TNode> edge = new Tuple <TNode, TNode>(source, dest);

            //if the node items are null; maybe check source and dest directly
            if (source == null || dest == null)
            {
                throw new ArgumentNullException();
            }
            //if the nodes are the same or if the graph already contains...
            else if (source.Equals(dest) || _edges.ContainsKey(edge))
            {
                throw new ArgumentNullException();
            }
            //if the node is not in the _graph
            else
            {
                _edges.Add(edge, value);
                LinkedListCell <TNode> list = new LinkedListCell <TNode>();
                _adjacencyLists.TryGetValue(source, out list);
                LinkedListCell <TNode> newCell = new LinkedListCell <TNode>();
                newCell.Next            = list;
                newCell.Data            = dest;
                list                    = newCell;
                _adjacencyLists[source] = newCell;
                if (!_adjacencyLists.ContainsKey(dest))
                {
                    _adjacencyLists.Add(dest, null);
                }
            }
        }
示例#11
0
        public void linkedListNextTest()
        {
            LinkedListCell <int> cellOne = new LinkedListCell <int>();
            LinkedListCell <int> cellTwo = new LinkedListCell <int>();

            cellOne.Next = cellTwo;
            Assert.AreEqual(cellOne.Next, cellTwo);
        }
示例#12
0
 /// <summary>
 /// Advances the current position to the next position.
 /// </summary>
 /// <returns>Whether the resulting position is valid.</returns>
 public bool MoveNext()
 {
     if (_current == null)
     {
         return(false);
     }
     _current = _current.Next;
     return(_current != null);
 }
示例#13
0
        }//close CheckCapture

        /// <summary>
        /// returns if row of cells given by cell contains a piece in the
        /// targetColor in the targetCol. out is set to square in column
        /// </summary>
        /// <param name="cell">given board cell</param>
        /// <param name="targetCol">given column</param>
        /// <param name="targetColor">given color</param>
        /// <returns></returns>
        private bool CheckCapture(LinkedListCell <BoardSquare> cell, int targetCol,
                                  SquareColor targetColor)
        {
            while (cell.Data.Column != targetCol)
            {
                cell = cell.Next;
            }
            return(cell.Data.Color == targetColor);
        }//close CheckCapture
示例#14
0
            /// <summary>
            /// Constructs an enumerator for the outgoing edges from the given source node.
            /// </summary>
            /// <param name="graph">The graph.</param>
            /// <param name="source">The source node for the outgoing edges.</param>
            public AdjacencyListEnumerator(DirectedGraph <TNode, TEdgeData> graph, TNode source)
            {
                LinkedListCell <TNode> node = graph._adjacencyLists[source];

                _list.Next = node;
                _current   = _list;
                _graph     = graph;
                _source    = source;
            }
示例#15
0
        //Method to get the last cell. It returns the last cell, less than the x value

        private LinkedListCell <KeyValuePair <TKey, TValue> > GetLastCell(TKey x)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > temp = _header;

            while (temp.Next != null && temp.Next.Data.Key.CompareTo(x) < 0)
            {
                temp = temp.Next;
            }
            return(temp);
        }
示例#16
0
        /// <summary>
        /// Adds a new link to the list
        /// </summary>
        /// <param name="x"></param>
        public void Push(T x)
        {
            LinkedListCell <T> temp = new LinkedListCell <T>();

            temp.Data = x;
            temp.Next = _Stack1;

            _Count++;
            _Stack1 = temp;
        }
 /// <summary>
 /// Finds the cell with the given key in the given linked list.
 /// </summary>
 /// <param name="k">The key to look for.</param>
 /// <param name="list">The beginning of the linked list to search.</param>
 /// <returns>The cell containing k, or null if there is no such cell.</returns>
 private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list)
 {
     for (LinkedListCell <KeyValuePair <TKey, TValue> > p = list; p != null; p = p.Next)
     {
         if (k.Equals(p.Data.Key))
         {
             return(p);
         }
     }
     return(null);
 }
示例#18
0
 /// <summary>
 /// Finds the cell with the given key in the given linked list.
 /// </summary>
 /// <param name="k">The key to look for.</param>
 /// <param name="list">The beginning of the linked list to search.</param>
 /// <returns>The cell containing k, or null if there is no such cell.</returns>
 private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list)
 {
     while (list != null)
     {
         if (list.Data.Key.Equals(k))
         {
             return(list);
         }
         list = list.Next;
     }
     return(null);
 }
        /// <summary>
        /// Implements the same functionality as the TryGetValue method.
        /// </summary>
        /// <param name="k"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public bool TryGetValue(TKey k, out TValue v)
        {
            CheckKey(k);
            int loc = GetLocation(k);
            LinkedListCell <KeyValuePair <TKey, TValue> > temp = GetCell(k, _hashtable[loc]);

            if (temp == null)
            {
                v = default(TValue);
                return(false);
            }
            v = temp.Data.Value;
            return(true);
        }
        /// <summary>
        /// Adds keys and values
        /// </summary>
        /// <param name="k">key to add</param>
        /// <param name="v">values to add</param>
        public void Add(TKey k, TValue v)
        {
            int index = k.GetHashCode();
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _hashTable[GetLocation(k)]);

            if (cell == null)
            {
                Insert(k, v, GetLocation(k));
            }
            else
            {
                throw new ArgumentException();
            }
        }
示例#21
0
 /// <summary>
 /// Returns the first link in the list and sets the second link as the first
 /// </summary>
 /// <returns></returns>
 public T Pop()
 {
     if (_Count > 0)
     {
         T temp = Peek();
         _Stack1 = _Stack1.Next;
         _Count--;
         return(temp);
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
        /// <summary>
        /// This gets the cell which is contains a Key equivalent to k
        /// </summary>
        /// <param name="k">Key we're looking for</param>
        /// <param name="list">Cell we are using to search for k</param>
        /// <returns></returns>
        private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list)
        {
            LinkedListCell <KeyValuePair <TKey, TValue> > pointer = list;

            while (pointer != null)
            {
                if (pointer.Data.Key.Equals(k))
                {
                    return(pointer);
                }
                pointer = pointer.Next;
            }
            return(null);
        }
        /// <summary>
        /// Associates the given value with the given key. If k is null, throws an ArgumentNullException.
        /// If k is already in the table, throws an ArgumentException.
        /// </summary>
        /// <param name="k">The key to add.</param>
        /// <param name="v">The value to associate with k.</param>
        public void Add(TKey k, TValue v)
        {
            CheckKey(k);
            int loc = GetLocation(k);
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _table[loc]);

            if (cell == null)
            {
                Insert(k, v, loc);
            }
            else
            {
                throw new ArgumentException();
            }
        }
示例#24
0
 /// <summary>
 /// Load file 1 and put its content into _file1
 /// </summary>
 /// <param name="sender">The sender object</param>
 /// <param name="e">Event argument</param>
 private void uxLoadFile1_Click(object sender, EventArgs e)
 {
     if (uxOpenDialog.ShowDialog() == DialogResult.OK)
     {
         try
         {
             _file1             = ReadFile(uxOpenDialog.FileName);
             uxFile1Status.Text = "File 1 loaded.";
         }
         catch (Exception ex)
         {
             MessageBox.Show("Errors:" + ex);
         }
     }
 }
        /// <summary>
        /// Tries to get the value associated with the given key. If k is null, throws an ArgumentNullException.
        /// </summary>
        /// <param name="k">The key to look for.</param>
        /// <param name="v">The value associated with k, or the default value for this type if k is not in the
        /// dictionary.</param>
        /// <returns>Whether the was found.</returns>
        public bool TryGetValue(TKey k, out TValue v)
        {
            CheckKey(k);
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _table[GetLocation(k)]);

            if (cell == null)
            {
                v = default(TValue);
                return(false);
            }
            else
            {
                v = cell.Data.Value;
                return(true);
            }
        }
示例#26
0
 /// <summary>
 /// Checks if there is a number in the cell already
 /// </summary>
 /// <param name="lc">LinkedListCell to check</param>
 /// <param name="s">String to check against</param>
 /// <returns>if matches then true</returns>
 public bool CheckIt(LinkedListCell <PhoneCount> lc, string s)
 {
     while (lc.Next != null)
     {
         if (lc.Data.PhoneNumber == s)
         {
             lc.Data.Count++;
             return(true);
         }
         else
         {
             lc = lc.Next;
         }
     }
     return(false);
 }
示例#27
0
        //If the next cell is non-null, and conatins the given key, set the out parameter to the associated value, and return true.
        //Otherwise, Set the out parameter to the default value, and return false.


        public bool TryGetValue(TKey k, out TValue v)
        {
            CheckNull(k);
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetLastCell(k);

            if (cell.Next != null && cell.Next.Data.Key.CompareTo(k) == 0)
            {
                v = cell.Next.Data.Value;
                return(true);
            }
            else
            {
                v = default(TValue);
                return(false);
            }
        }
示例#28
0
        //Essentially the opposite of TRygetValue
        // If the last key is not null, and contains the given key
        //   throw an argumentExceptionargument.
        //      Otherwise, set its data to a new kayvaluepair, and insert a new cell
        public void Add(TKey k, TValue v)
        {
            CheckNull(k);
            LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetLastCell(k);

            if (cell.Next != null && cell.Next.Data.Key.CompareTo(k) == 0)
            {
                throw new ArgumentException();
            }
            else
            {
                LinkedListCell <KeyValuePair <TKey, TValue> > newCell = new LinkedListCell <KeyValuePair <TKey, TValue> >();
                KeyValuePair <TKey, TValue> newPair = new KeyValuePair <TKey, TValue>(k, v);
                newCell.Data = newPair;
                newCell.Next = cell.Next;
                cell.Next    = newCell;
            }
        }
示例#29
0
 /// <summary>
 /// Load file 2 and put its content into _file2
 /// </summary>
 /// <param name="sender">The sender object</param>
 /// <param name="e">Event argument</param>
 private void uxLoadFile2_Click(object sender, EventArgs e)
 {
     if (uxOpenDialog.ShowDialog() == DialogResult.OK)
     {
         try
         {
             _file2             = ReadFile(uxOpenDialog.FileName);
             uxFile2Status.Text = "File 2 loaded.";
             if (uxFile1Status.Text != "No file loaded." && uxFile2Status.Text != "No file loaded.")
             {
                 uxFind.Enabled = true;
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("Errors:" + ex);
         }
     }
 }
示例#30
0
        }//close GetRow

        /// <summary>
        /// finds and returns the BoardSquare that corresponds to
        /// given row and column.If square has same color as the
        /// turn, square is stored in SelectedPiece
        /// </summary>
        /// <param name="row">row parameter</param>
        /// <param name="col">column parameter</param>
        /// <returns></returns>
        private BoardSquare SelectSquare(int row, int col)
        {
            LinkedListCell <BoardSquare> temp = _board[row];

            while (temp.Next != null && temp.Data.Column != col)
            {
                temp = temp.Next;
            }
            if (temp.Data.Color == Turn && temp.Data.Color != SquareColor.None)
            {
                if (SelectedPiece != null)
                {
                    SelectedPiece.Selected = false;
                }
                SelectedPiece          = temp.Data;
                SelectedPiece.Selected = true;
            }
            return(temp.Data);
        }//close SelectSquare