Advertisement
Guest User

Node

a guest
Jan 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace nodeTest
  8. {
  9.     class Program
  10.     {
  11.         public static void printList(Node<int> ls)
  12.         {
  13.             Node<int> pos1 = ls;
  14.             while (pos1 != null)
  15.             {
  16.                 Console.WriteLine(pos1.GetValue());
  17.                 pos1 = pos1.GetNext();
  18.             }
  19.         }
  20.         public static Node<int> qu1(Node<int> list1, int max)
  21.         {
  22.             Node<int> newList = new Node<int>(1);
  23.             Node<int> newPos = newList;
  24.             for(int i=2;i< max; i++)
  25.             {
  26.                 Node<int> pos1 = list1;
  27.                 bool found = false;
  28.                 while (pos1 != null)
  29.                 {
  30.                     if (pos1.GetValue() == i)
  31.                     {
  32.                         found = true;
  33.                     }
  34.                     pos1 = pos1.GetNext();
  35.                 }
  36.                 if (!found)
  37.                 {
  38.                     newPos.SetNext(new Node<int>(i));
  39.                     newPos = newPos.GetNext();
  40.                 }
  41.             }
  42.             return newList;
  43.         }
  44.         public static Node<int> qu2(Node<int> L)
  45.         {
  46.             Node<int> Lpos = L;
  47.             Node<int> newList = new Node<int>(0);
  48.             Node<int> newPos = newList;
  49.  
  50.             int sum = 0;
  51.             int lastNumber = 0;
  52.             while (Lpos != null)
  53.             {
  54.                 int value = Lpos.GetValue();
  55.                 if (value > lastNumber)
  56.                 {
  57.                     sum += value;
  58.                     lastNumber = value;
  59.                 }
  60.                 else
  61.                 {
  62.                     newPos.SetValue(sum);
  63.                     newPos.SetNext(new Node<int>(0));
  64.                     newPos = newPos.GetNext();
  65.                     sum = 0;
  66.                     lastNumber = 0;
  67.                 }
  68.                 Lpos = Lpos.GetNext();
  69.             }
  70.             return newList;
  71.         }
  72.         static void Main(string[] args)
  73.         {
  74.             int value = int.Parse(Console.ReadLine());
  75.             Node<int> ls = new Node<int>(value);
  76.             Node<int> pos = ls;
  77.             value = int.Parse(Console.ReadLine());
  78.             while (value!=999)
  79.             {
  80.                 pos.SetNext(new Node<int>(value));
  81.                 pos = pos.GetNext();
  82.                 value = int.Parse(Console.ReadLine());
  83.             }
  84.             Console.WriteLine("original Node:");
  85.             printList(ls);
  86.             Console.WriteLine("after qu2:");
  87.             printList(qu2(ls));
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement