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;
- using System.Threading.Tasks;
- namespace num
- {
- class Node
- {
- public Node next;
- public int data;
- public int index;
- public Node() : this(null, 0, 0) { }
- public Node(int data, int index) : this(null, data, index) { }
- public Node(Node next, int data) : this(next, data, 0) { }
- public Node(Node next, int data, int index)
- {
- this.data = data;
- this.next = next;
- this.index = index;
- }
- }
- class OurList
- {
- private int size = 0;
- private Node root = null;
- private Node mirror(Node _temp)
- {
- Node temp = null;
- while (_temp != null)
- {
- temp = new Node(temp, _temp.data, _temp.index);
- _temp = _temp.next;
- }
- return temp;
- }
- public int Size
- {
- get => size;
- private set => size = value;
- }
- public void DeleteList()
- {
- root = null;
- }
- public void Add(int data, int index)
- {
- if (root == null)
- {
- ++size;
- root = new Node(data, index);
- }
- else if (index == Find(index).index)
- {
- Console.WriteLine("error index " + data + ":"+ index + " was already added" );
- return;
- }
- else
- {
- ++size;
- root = new Node(root, data, index);
- }
- }
- public void Add(int data)
- {
- if (root == null)
- {
- ++size;
- root = new Node(data, size);
- }
- else
- {
- ++size;
- root = new Node(root, data, size);
- }
- }
- public Node Find(int index)
- {
- Node save = new Node(-1, -1);
- Node temp = root;
- while (temp != null )
- {
- if (temp.index == index)
- return temp;
- temp = temp.next;
- }
- return save;
- }
- public void Print()
- {
- Node temp = mirror(root);
- while (temp != null)
- {
- Console.WriteLine(" data:{0,5} index:{1,5}",temp.data,temp.index );
- temp = temp.next;
- }
- Console.WriteLine();
- }
- public void DeleteFirstElement()
- {
- root = First.next;
- root = First;
- }
- public Node First
- {
- get => mirror(root);
- }
- public override string ToString()
- {
- return base.ToString();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- OurList a = new OurList();
- a.Add(15,4);
- a.Add(152);
- a.Add(123, 3);
- a.Add(1534, 25);
- a.Add(1534, 43);
- a.Add(1534, 20);
- a.Print();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement