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;
- using System.Threading.Tasks;
- namespace ConsoleApp14
- {
- class Program
- {
- static void Main(string[] args)
- {
- LinkedList list = new LinkedList();
- list.Add(105);
- list.Add(10);
- list.Add(5);
- list.Add(4);
- list.Remove(10);
- list.Contains(3);
- list.Show();
- Console.ReadLine();
- //Node current = GetRoot();
- }
- //static Node GetRoot()
- //{
- // return new Node(0,
- // new Node(1,
- // new Node(2,
- // new Node(3,
- // new Node(4,
- // new Node(5, null))))));
- //}
- }
- class LinkedList
- {
- private Node _root;
- public int Count { get; private set; }
- public void Add(int value)
- {
- Node curNode = _root;
- if (curNode != null)
- {
- while(curNode.Next != null)
- {
- curNode = curNode.Next;
- }
- curNode.Next = new Node(value, null);
- }
- else
- {
- _root = new Node(value, null);
- }
- }
- public void Remove(int value)
- {
- Node curNode = _root;
- if (curNode.Data == value)
- {
- curNode = curNode.Next;
- }
- else
- {
- Node removeNode = SeachNode(value);
- if (removeNode.Next != null)
- {
- removeNode.Next = removeNode.Next.Next;
- }
- else
- {
- Console.WriteLine("Такого элемента нет");
- }
- }
- }
- public void RemoveAt(int index)
- {
- }
- public void Reverse()
- {
- }
- public bool Contains(int value)
- {
- Node currentNode = _root;
- while (currentNode.Data != value)
- {
- currentNode = currentNode.Next;
- if (currentNode.Next == null)
- {
- break;
- }
- }
- if (currentNode.Data == value)
- {
- Console.WriteLine(true);
- return true;
- } else
- {
- Console.WriteLine(false);
- return false;
- }
- }
- public Node SeachNode(int value)
- {
- Node curNode = _root;
- while (curNode.Next != null)
- {
- if (curNode.Next.Data != value)
- {
- curNode = curNode.Next;
- }
- else
- {
- return curNode;
- }
- }
- return curNode;
- }
- public void Show ()
- {
- Node currentNode = _root;
- while (currentNode != null)
- {
- Console.Write(currentNode.Data + " ");
- currentNode = currentNode.Next;
- }
- }
- }
- class Node
- {
- public Node Next;
- public int Data;
- public Node(int data, Node next)
- {
- Next = next;
- Data = data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment