Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace T5.RemoveNegativeNumbers
- {
- class RemoveNegativeNumbers
- {
- static void Main()
- {
- Console.WriteLine("Write a program that removes from given sequence all negative numbers\n");
- 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 };
- Console.WriteLine("The original sequence of numbers is:\n");
- Console.WriteLine(String.Join(", ", numbersList));
- Console.WriteLine("\n");
- RemoveNegativeUtil.PositiveNumbersSubSet(numbersList);
- Console.WriteLine("The subsequence of the positive numbers is:\n");
- Console.WriteLine(String.Join(", ", numbersList));
- Console.WriteLine("\n");
- }
- }
- }
- *****************************
- using System;
- using System.Collections.Generic;
- namespace T5.RemoveNegativeNumbers
- {
- public class RemoveNegativeUtil
- {
- /// <summary>
- /// removes all negative numbers from given List<double> and returns the result as new List<double>.
- /// </summary>
- /// <param name="numbersList">The given list of numbers</param>
- /// <returns>List<double> positiveOnly contains the subsequence of positive numbers in the numbersList</returns>
- public static void PositiveNumbersSubSet(LinkedList<double> numbersList)
- {
- LinkedListNode<Double> listNode = numbersList.First;
- while (listNode!=null)
- {
- var nextNode = listNode.Next;
- if (listNode.Value < 0)
- {
- numbersList.Remove(listNode);
- }
- listNode=nextNode;
- }
- }
- }
- }
- **********************************
- using System;
- using System.Collections.Generic;
- namespace T5.RemoveNegativeNumbers
- {
- public class LinkedListWithInit<T> : LinkedList<T>
- {
- public void Add(T item)
- {
- ((ICollection<T>)this).Add(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement