Advertisement
ellapt

DSA2.5.RemoveNegativeNumbers

Jun 1st, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace T5.RemoveNegativeNumbers
  5. {
  6.     class RemoveNegativeNumbers
  7.     {
  8.         static void Main()
  9.         {
  10.             Console.WriteLine("Write a program that removes from given sequence all negative numbers\n");
  11.  
  12.             LinkedList<double> numbersList = new LinkedListWithInit<double> { 45.2, 4.87, 4, 4.45, -5, -5.34, -5, 256, 3.38, 3, 2, 4, -48.2, 4, 4, -8.2, 0 };
  13.             Console.WriteLine("The original sequence of numbers is:\n");
  14.             Console.WriteLine(String.Join(", ", numbersList));
  15.             Console.WriteLine("\n");
  16.            
  17.             RemoveNegativeUtil.PositiveNumbersSubSet(numbersList);
  18.  
  19.             Console.WriteLine("The subsequence of the positive numbers is:\n");
  20.             Console.WriteLine(String.Join(", ", numbersList));
  21.             Console.WriteLine("\n");
  22.         }
  23.     }
  24. }
  25. *****************************
  26. using System;
  27. using System.Collections.Generic;
  28.  
  29. namespace T5.RemoveNegativeNumbers
  30. {
  31.  
  32.     public class RemoveNegativeUtil
  33.     {
  34.         /// <summary>
  35.         /// removes all negative numbers from given List<double> and returns the result as new List<double>.
  36.         /// </summary>
  37.         /// <param name="numbersList">The given list of numbers</param>
  38.         /// <returns>List<double> positiveOnly contains the subsequence of positive numbers in the numbersList</returns>
  39.         public static void PositiveNumbersSubSet(LinkedList<double> numbersList)
  40.         {
  41.             LinkedListNode<Double> listNode = numbersList.First;
  42.  
  43.             while (listNode!=null)
  44.             {
  45.                 var nextNode = listNode.Next;
  46.                 if (listNode.Value < 0)
  47.                 {
  48.                     numbersList.Remove(listNode);
  49.                 }
  50.                 listNode=nextNode;
  51.             }
  52.         }
  53.     }
  54. }
  55. **********************************
  56. using System;
  57. using System.Collections.Generic;
  58.  
  59. namespace T5.RemoveNegativeNumbers
  60. {
  61.     public class LinkedListWithInit<T> : LinkedList<T>
  62.     {
  63.         public void Add(T item)
  64.         {
  65.             ((ICollection<T>)this).Add(item);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement