Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Kirill3
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             LinkedList<int> list = new LinkedList<int>();
  13.             list.AddFirst(1);
  14.             list.AddFirst(2);
  15.             list.AddLast(3);
  16.             foreach (var item in list)
  17.             {
  18.                 Console.WriteLine(item);
  19.             }
  20.  
  21.             LinkedListMine l = new LinkedListMine();
  22.             l.Add(1);
  23.             l.Add(2);
  24.             l.Add(3);
  25.             l.Print();
  26.         }
  27.  
  28.  
  29.         class LinkedListMine
  30.         {
  31.             public Node First;
  32.  
  33.             public void Add(int i)
  34.             {
  35.                 if(First == null)
  36.                 {
  37.                     First = new Node(i);
  38.                     return;
  39.                 }
  40.  
  41.                 Node prev = First;
  42.                 Node current = First.Next;
  43.                 while(current != null)
  44.                 {
  45.                     prev = current;
  46.                     current = current.Next;
  47.                 }
  48.                 current = new Node(i);
  49.                 prev.Next = current;
  50.             }
  51.  
  52.             public void Print()
  53.             {
  54.                 Node current = First;
  55.                 Console.WriteLine();
  56.                 while (current != null)
  57.                 {
  58.                     Console.Write(current.Value);
  59.                     Console.Write(" ");
  60.                     current = current.Next;
  61.                 }
  62.                 Console.WriteLine();
  63.             }
  64.         }
  65.  
  66.         class Node
  67.         {
  68.             public int Value;
  69.             public Node Next;
  70.  
  71.             public Node(int value)
  72.             {
  73.                 Value = value;
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement