//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) { StockList resultList = new StockList(); StockNode mergeNode = null; //Sort the first list by name and assign the list to the result list. if (this.Length() > 0) { this.SortByName(); resultList = this; } // Traverse through each node and add to the end of the node. if (listToMerge.Length() > 0) { mergeNode = listToMerge.head; while (mergeNode != null) { resultList.AddStock(mergeNode.StockHolding); mergeNode = mergeNode.Next; } } return(resultList); }
//param (StockList)listToMerge : second list to be merged //summary : merge two different list into a single result list //return : merged list //return type : StockList // use AddStock function and Count functions as helpers // 1. Sort lists by name // 2. determine count of elements in each list // 3. Pick as main list the larger list // 4. Loop through the other list and add stock using add stock function // 5. return new list public StockList MergeList(StockList listToMerge) { this.SortByName(); StockList resultList = this; StockNode currentNode = listToMerge.head; for (int i = 0; i < listToMerge.Length(); i++) //loop through list to merge { this.AddStock(currentNode.StockHolding); currentNode = currentNode.Next; } return(resultList); }
//param (StockList)listToMerge : second list to be merged //summary : merge two different list into a single result list //return : merged list //return type : StockList // use AddStock function and Count functions as helpers // 1. Sort lists by name // 2. determine count of elements in each list // 3. Pick as main list the larger list // 4. Loop through the other list and add stock using add stock function // 5. return new list public StockList MergeList(StockList listToMerge) { // write your implementation here StockList resultList = new StockList(); resultlist = this; // set resultlist to current list resultlist.SortbyName(); StockNode currentNode = listToMerge.head; for (int i = 0; i < listToMerge.Length() ) //loop through list to merge { this.AddStock(currentNode.Stock); currentNode = currentNode.Next; } return resultList; }