Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Kirill3
- {
- class Program
- {
- static void Main()
- {
- LinkedList<int> list = new LinkedList<int>();
- list.AddFirst(1);
- list.AddFirst(2);
- list.AddLast(3);
- foreach (var item in list)
- {
- Console.WriteLine(item);
- }
- LinkedListMine l = new LinkedListMine();
- l.AddLast(1);
- l.AddLast(2);
- l.AddLast(3);
- l.AddFirst(4);
- l.AddLast(5);
- l.Print();
- //пример
- }
- class LinkedListMine
- {
- public Node First;
- public Node Last;
- public void AddFirst(int i)
- {
- if(First == null)
- {
- First = new Node(i);
- Last = First;
- return;
- }
- var newFirst = new Node(i);
- First.Prev = newFirst;
- newFirst.Next = First;
- First = newFirst;
- }
- //добавить функцию ADD AFTER
- public void AddAfter(Node node, int value)
- {
- }
- //добавить функцию Find
- public Node Find(int value)
- {
- return null;
- }
- public void AddAfter(int valueAfter, int value)
- {
- AddAfter(Find(valueAfter), value);
- }
- //Улучшить с помощтю ссылки на Last
- public void AddLast(int i)
- {
- if(First == null)
- {
- First = new Node(i);
- Last = First;
- return;
- }
- Node prev = First;
- Node current = First.Next;
- while(current != null)
- {
- prev = current;
- current = current.Next;
- }
- current = new Node(i);
- prev.Next = current;
- current.Prev = prev;
- Last = current;
- }
- public void Print()
- {
- Node current = First;
- Console.WriteLine();
- while (current != null)
- {
- Console.Write(current.Value);
- Console.Write(" ");
- current = current.Next;
- }
- Console.WriteLine();
- }
- }
- class Node
- {
- public int Value;
- public Node Next;
- public Node Prev;
- public Node(int value)
- {
- Value = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement