//param        : NA
        //summary      : Sort the list alphabatically
        //return       : NA
        //return type  : NA
        public void SortByName()
        {
            // write your implementation here
            StockNode temp     = this.head;
            int       StockLen = 0;

            while (temp.Next != null)

            //Travering the list to find the total number of stocks as StockLen
            {
                StockLen++;
                temp = temp.Next;
            }

            //Creating local variables for run-time data

            decimal[] holdings     = new decimal[StockLen];
            decimal[] currentPrice = new decimal[StockLen];
            string[]  symbol       = new string[StockLen];
            string[]  name         = new string[StockLen];
            temp = this.head;
            int ctr = 0;

            while (temp.Next != null)
            {
                //Populating the incremental local variables

                holdings[ctr]     = temp.StockHolding.Holdings;
                currentPrice[ctr] = temp.StockHolding.CurrentPrice;
                symbol[ctr]       = String.Copy(temp.StockHolding.Symbol);
                name[ctr]         = String.Copy(temp.StockHolding.Name);
                temp = temp.Next;
                ctr++;
            }

            //Creating comparision logic with nested loops using CompareTo

            for (int i = 1; i < name.Length; i++)
            {
                for (int j = 0; j < name.Length - i; j++)
                {
                    if (name[j].CompareTo(name[j + 1]) > 0)
                    {
                        string nametemp = String.Copy(name[j]);
                        name[j]     = String.Copy(name[j + 1]);
                        name[j + 1] = String.Copy(nametemp);

                        string symboltemp = String.Copy(symbol[j]);
                        symbol[j]     = String.Copy(symbol[j + 1]);
                        symbol[j + 1] = String.Copy(symboltemp);

                        decimal holdingtemp = holdings[j];
                        holdings[j]     = holdings[j + 1];
                        holdings[j + 1] = holdingtemp;

                        decimal currentprice = currentPrice[j];
                        currentPrice[j]     = currentPrice[j + 1];
                        currentPrice[j + 1] = currentprice;
                    }
                }
            }

            //The existing list will be now updated with sorted array values

            temp = this.head;
            ctr  = 0;
            while (temp.Next != null)
            {
                temp.StockHolding.Holdings     = holdings[ctr];
                temp.StockHolding.CurrentPrice = currentPrice[ctr];
                temp.StockHolding.Name         = String.Copy(name[ctr]);
                temp.StockHolding.Symbol       = String.Copy(symbol[ctr]);
                temp = temp.Next;
                ctr++;
            }
        }
Exemple #2
0
 //Constructor for initialization
 public StockList()
 {
     this.head = null;
 }
Exemple #3
0
 //constructor to initialize the variables of object
 public StockNode(Stock stockHolding)
 {
     StockHolding = stockHolding;
     Next         = null;
 }
        // FOR STUDENTS

        //param        : NA
        //summary      : Sort the list by descending number of holdings
        //return       : NA
        //return type  : NA
        public void SortByValue()
        {
            //Write your implementation here
            //We created local variables and used the method of bubblesorting to sort the local in descending order

            StockNode temp     = this.head;
            int       StockLen = 0;

            //The below function traverses the list to find the total number of stocks, stored as StockLen
            while (temp.Next != null)

            {
                StockLen++;
                temp = temp.Next;
            }

            decimal[] holdings     = new decimal[StockLen];
            decimal[] currentPrice = new decimal[StockLen];
            string[]  symbol       = new string[StockLen];
            string[]  name         = new string[StockLen];

            //This fucntion stores the incremental local variables

            temp = this.head;
            int ctr = 0;

            while (temp.Next != null)

            {
                holdings[ctr]     = temp.StockHolding.Holdings;
                currentPrice[ctr] = temp.StockHolding.CurrentPrice;
                symbol[ctr]       = String.Copy(temp.StockHolding.Symbol);
                name[ctr]         = String.Copy(temp.StockHolding.Name);

                temp = temp.Next;
                ctr++;
            }

            //Now we build comparision logic with nested loops

            for (int i = 1; i < name.Length; i++)
            {
                for (int j = 0; j < name.Length - i; j++)
                {
                    if (holdings[j] < holdings[j + 1])

                    {
                        string nametemp = String.Copy(name[j]);
                        name[j]     = String.Copy(name[j + 1]);
                        name[j + 1] = String.Copy(nametemp);

                        string symboltemp = String.Copy(symbol[j]);
                        symbol[j]     = String.Copy(symbol[j + 1]);
                        symbol[j + 1] = String.Copy(symboltemp);

                        decimal holdingtemp = holdings[j];
                        holdings[j]     = holdings[j + 1];
                        holdings[j + 1] = holdingtemp;

                        decimal currentprice = currentPrice[j];
                        currentPrice[j]     = currentPrice[j + 1];
                        currentPrice[j + 1] = currentprice;
                    }
                }
            }

            //The existing list will be now updated with sorted array values

            temp = this.head;
            ctr  = 0;
            while (temp.Next != null)

            {
                temp.StockHolding.Holdings     = holdings[ctr];
                temp.StockHolding.CurrentPrice = currentPrice[ctr];
                temp.StockHolding.Name         = String.Copy(name[ctr]);
                temp.StockHolding.Symbol       = String.Copy(symbol[ctr]);

                temp = temp.Next;
                ctr++;
            }
        }
Exemple #5
0
 //param (Stock)stock : stock that is to be added
 //summary      : Add node at first position in list
 //                This is done by creating a new node 
 //                  and pointing it to the current list 
 //return       : NA
 //return type  : NA
 public void AddFirst(Stock stock)
 {
   StockNode nodeToAdd = new StockNode(stock);
   nodeToAdd.Next = head;
   head = nodeToAdd;
 }
Exemple #6
0
 // default constructor
 public StockNode()
 {
     StockHolding = null;
     Next         = null;
 }
Exemple #7
0
        //param        : NA
        //summary      : Sort the list alphabatically
        //return       : NA
        //return type  : NA
        //param        : NA

        public void SortByName()

        {


            // if there is nothing in the list, stop.
            if (this.IsEmpty())
            {
                return;
            }
            // do a double loop to compare every item in the list
            // with every other item in the list 
            // based on bubble sort implementation of DSA guide - c sharp code download
            // grab first stock in stock list and next stock in list
            // compare their names
            // if first stock name greater than second stock, leave first stock in its place
            // if first stock name less than second stock name, swap stocks
            else
            {
                StockNode nodeReference = null;

                StockNode currentNode = this.head;
                StockNode nextNode = currentNode.Next;

                String currStockName = currentNode.StockHoldings.Name;
                String nextStockName = nextNode.StockHoldings.Name;


                for (int i = 0; i < this.Length(); i++)
                {
                    for (int j = 0; j < this.Length(); j++)
                    {
                        // String.Compare (strA, str B)
                        //  Less than zero	strA precedes strB in the sort order.
                        // Zero strA occurs in the same position as strB in the sort order.
                        //Greater than zero strA follows strB in the sort order.
                        // String.Compare (currStockName, nextStockName) < 0  
                        // already in right sort order, move on to next node
                        // if  String.Compare (currStockName, nextStockName) < 0  
                        // not in right sort order, need to swap




                        if (String.Compare(currStockName, nextStockName) > 0)  // swap
                        {
                            nodeReference = this.Swap(currentNode.StockHolding);
                            currentNode = nodeReference;
                            currStockName = currentNode.StockHoldings.Name;
                            nextNode = currentNode.Next;
                            nextStockName = nextNode.StockHoldings.Name;


                            String currStockName = currentNode.StockHoldings.Name;
                            String nextStockName = nextNode.StockHoldings.Name;

                        }
                        else // no need to swap, just move on to next node
                        {
                            currentNode = nextNode;
                            currStockName = currentNode.StockHoldings.Name;
                            nextNode = currentNode.Next;
                            nextStockName = nextNode.StockHoldings.Name;
                            nodeReference = null; // not sure if I need this 

                        }

                    }
                }



            }
        //param   (StockList)listToMerge : second list to be merged
        //summary      : merge two different list into a single result list
        //return       : merged list
        //return type  : StockList
        public StockList MergeList(StockList listToMerge)
        {
            // The Stock node objects first, current and previous are used to store the
            // head node, active node and the prior node informations
            StockList resultList = new StockList();
            StockNode first = null, current = null, previous = null;
            StockNode priNode = this.head;
            StockNode secNode = listToMerge.head;

            // The below logic progressively iterates through both the lists and stores the nodes in
            // sorted order. For ideal nodes, the holdings attribute is summed
            // This while loop iterates until all the nodes in the primary list are encountered
            while (priNode != null)
            {
                if (secNode != null && priNode.StockHolding.Name.CompareTo(secNode.StockHolding.Name) == 0)
                {
                    decimal updateHoldings = priNode.StockHolding.Holdings + secNode.StockHolding.Holdings;
                    priNode.StockHolding.Holdings = updateHoldings;
                    current = priNode;
                    priNode = priNode.Next;
                    secNode = secNode.Next;
                }
                else if (secNode != null && priNode.StockHolding.Name.CompareTo(secNode.StockHolding.Name) > 0)
                {
                    current = secNode;
                    secNode = secNode.Next;
                }
                else
                {
                    current = priNode;
                    priNode = priNode.Next;
                }
                // 'first' is used to store the head node
                if (first == null)
                {
                    first = current;
                }
                // Except for the first iteration, 'previous' is used to store the prior node information
                if (previous != null)
                {
                    previous.Next = current;
                }
                previous = current;
            }
            // This while loop handles the uniterated nodes in the listToMerge
            while (secNode != null)
            {
                current = secNode;
                if (first == null)
                {
                    first = current;
                }
                if (previous != null)
                {
                    previous.Next = current;
                }
                secNode = secNode.Next;
            }
            resultList.head = first;
            return(resultList);
        }
Exemple #9
0
        //param        : NA
        //summary      : Sort the list alphabatically
        //return       : NA
        //return type  : NA
        //param        : NA

        public void SortByName() // Carole
        {
            // if there is nothing in the list, stop.
            if (this.IsEmpty())
            {
                return;
            }
            // do a double loop to compare every item in the list
            // with every other item in the list
            // based on bubble sort implementation of DSA guide - c sharp code download
            // grab first stock in stock list and next stock in list
            // compare their number of stocks
            // if first stock greater than second stock, leave first stock in its place
            // if first stock less than second stock, swap stocks
            else
            {
                StockNode currentNode  = this.head;
                StockNode nextNode     = currentNode.Next;
                StockNode previousNode = null;


                String currStockName = currentNode.StockHolding.Name;
                String nextStockName = nextNode.StockHolding.Name;
                String prevStockName = "";



                for (int i = 0; i < this.Length(); i++)
                {
                    currentNode  = this.head;
                    nextNode     = currentNode.Next;
                    previousNode = null;

                    currStockName = currentNode.StockHolding.Name;
                    nextStockName = nextNode.StockHolding.Name;
                    prevStockName = "";


                    for (int j = 0; j < this.Length(); j++)

                    {
                        if (String.Compare(currStockName, nextStockName) > 0)  // swap ascending

                        {
                            //    Console.WriteLine("Logic 1");
                            previousNode  = currentNode;
                            prevStockName = previousNode.StockHolding.Name;
                            //    Console.WriteLine("Swapping:" + currentNode.StockHolding.Name);


                            currentNode   = this.Swap(previousNode.StockHolding);
                            currStockName = currentNode.StockHolding.Name;

                            nextNode      = currentNode.Next;
                            nextStockName = nextNode.StockHolding.Name;
                        }

                        else   // no need to swap, just move on to next node
                        {
                            //  Console.WriteLine("Logic 2");


                            if ((currentNode.Next != null) && (currentNode.Next.Next != null))
                            {
                                //   Console.WriteLine("Logic 3");
                                currentNode   = currentNode.Next;
                                currStockName = currentNode.StockHolding.Name;
                                nextNode      = currentNode.Next;
                                nextStockName = nextNode.StockHolding.Name;
                            }
                        }
                    }
                }
            }
        }
Exemple #10
0
        // FOR STUDENTS


        //param        : NA
        //summary      : Sort the list by descending number of holdings
        //return       : NA
        //return type  : NA
        //param        : NA

        public void SortByValue() // Carole
        {
            // if there is nothing in the list, stop.
            if (this.IsEmpty())
            {
                return;
            }
            // do a double loop to compare every item in the list
            // with every other item in the list
            // based on bubble sort implementation of DSA guide - c sharp code download
            // grab first stock in stock list and next stock in list
            // compare their number of stocks
            // if first stock greater than second stock, leave first stock in its place
            // if first stock less than second stock, swap stocks
            else
            {
                StockNode currentNode  = this.head;
                StockNode nextNode     = currentNode.Next;
                StockNode previousNode = null;

                decimal currNumHoldings = currentNode.StockHolding.Holdings;
                decimal nextNumHoldings = nextNode.StockHolding.Holdings;
                decimal prevNumHoldings = 0;


                for (int i = 0; i < this.Length(); i++)
                {
                    currentNode  = this.head;
                    nextNode     = currentNode.Next;
                    previousNode = null;

                    currNumHoldings = currentNode.StockHolding.Holdings;
                    nextNumHoldings = nextNode.StockHolding.Holdings;
                    prevNumHoldings = 0;


                    for (int j = 0; j < this.Length(); j++)

                    {
                        //  Console.WriteLine("Loop i is:" + i);
                        //   Console.WriteLine("Loop j is:" + j);
                        //   Console.WriteLine("CurrentNumHoldings:" + currNumHoldings);
                        //   Console.WriteLine("NextNumHoldings:" + nextNumHoldings);


                        if (currNumHoldings < nextNumHoldings)  // swap
                        {
                            //   Console.WriteLine("Logic 1");
                            previousNode    = currentNode;
                            prevNumHoldings = previousNode.StockHolding.Holdings;
                            //   Console.WriteLine("Swapping:" + currentNode.StockHolding.Name);


                            currentNode     = this.Swap(previousNode.StockHolding);
                            currNumHoldings = currentNode.StockHolding.Holdings;

                            nextNode        = currentNode.Next;
                            nextNumHoldings = nextNode.StockHolding.Holdings;
                        }
                        else   // no need to swap, just move on to next node
                        {
                            //   Console.WriteLine("Logic 2");


                            if ((currentNode.Next != null) && (currentNode.Next.Next != null))
                            {
                                //    Console.WriteLine("Logic 3");
                                currentNode     = currentNode.Next;
                                currNumHoldings = currentNode.StockHolding.Holdings;
                                nextNode        = currentNode.Next;
                                nextNumHoldings = nextNode.StockHolding.Holdings;
                            }
                        }
                    }
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// swaps the node passed as argument with next node in list
        /// Sorting the list using the simple bubble sort algorithm requires repeatdely traversing
        ///   the list and pushing a node down the list until it falls in place
        ///     Pushing the node is essentially a swap operation, where we take the next node
        ///       and put it in the current position and move the current node to the next position on the list
        /// </summary>
        /// <param name="nodeOne">first node to be swapped</param>
        /// <returns>Reference to current node</returns>
        public StockNode Swap(Stock nodeOne)
        {
            StockNode prevNodeOne = null;
            StockNode currNodeOne = this.head;

            //   Console.WriteLine("Node passed is Current Node One Stock is:" + currNodeOne.StockHolding.Name);
            //   Console.WriteLine("Node Passed is Current Node One Stock value is:" + currNodeOne.StockHolding.Holdings);

            // traverse the list until we reach the node to swap
            while (currNodeOne != null && currNodeOne.StockHolding != nodeOne)
            {
                prevNodeOne = currNodeOne;
                currNodeOne = currNodeOne.Next;
                //      Console.WriteLine("Previous Node One Stock is:" + prevNodeOne.StockHolding.Name);
                //      Console.WriteLine("Previous Node One Stock value is:" + prevNodeOne.StockHolding.Holdings);
                //      Console.WriteLine("Current Node One Stock is:" + currNodeOne.StockHolding.Name);
                //      Console.WriteLine("Current Node One Stock value is:" + currNodeOne.StockHolding.Holdings);
            }

            // maintain references to the nodes to be swapped
            StockNode prevNodeTwo = currNodeOne;
            StockNode currNodeTwo = currNodeOne.Next;

            // handle corner cases, maybe we have reached the end of the list
            if (currNodeOne == null || currNodeTwo == null)
            {
                return(null);
            }

            // perhaps the insertion is at the top of the list
            if (prevNodeOne != null)
            {
                prevNodeOne.Next = currNodeTwo;
            }
            else
            {
                this.head = currNodeTwo;
            }

            if (prevNodeTwo != null)
            {
                prevNodeTwo.Next = currNodeOne;
            }
            else
            {
                this.head = currNodeOne;
            }

            // normal case, swap nodes
            StockNode temp = currNodeOne.Next;

            currNodeOne.Next = currNodeTwo.Next;
            currNodeTwo.Next = temp;

            //   Console.WriteLine("Node returned is Current Node Two Stock Returned is:" + currNodeTwo.StockHolding.Name);
            //   Console.WriteLine("Node Returned is Current Node Two Stock Returned value is:" + currNodeTwo.StockHolding.Holdings);



            return(currNodeTwo);
        }