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 ConsoleApplication7
- {
- class Node
- {
- object _data;
- public object Data {
- get { return _data; }
- set { _data = value; }
- }
- Node _next;
- public Node Next
- {
- get { return _next; }
- set { _next = value; }
- }
- Node _prev;
- public Node Prev
- {
- get { return _prev; }
- set { _prev = value; }
- }
- public Node(object d, Node n, Node p)
- {
- Data = d;
- Next = n;
- Prev = p;
- }
- }
- class DoublyList
- {
- Node _head;
- public Node head
- {
- get { return _head; }
- set { _head = value; }
- }
- Node _tail;
- public Node tail
- {
- get { return _tail; }
- set { _tail = value; }
- }
- int _count;
- public int count
- {
- get { return _count; }
- set { }
- }
- public DoublyList()
- {
- head = null;
- tail = null;
- count = 0;
- }
- public object Add(object o)
- {
- return Add(count, o);
- }
- public object Add(int index, object o)
- {
- if (index < 0 || index > count)
- {
- throw new IndexOutOfRangeException("Az index nem lehet negatív!");
- }
- if (Empty() || index == 0)
- {
- head = new Node(o, null, null);
- tail = head;
- }
- else
- {
- if (count / 2 > index)
- {
- Node current = head;
- for (int i = 0; i < index - 1; i++)
- {
- current = current.Next;
- }
- Node next = current.Next;
- current.Next = new Node(o, current.Next, current);
- if (next != null)
- {
- next.Prev = current.Next;
- }
- else
- {
- tail = current.Next;
- }
- }
- else
- {
- Node current = tail;
- for (int i = 0; i < index - 1; i++)
- {
- current = current.Prev;
- }
- Node prev = current.Next;
- current.Prev = new Node(o, current.Prev, current);
- if (prev != null)
- {
- prev.Next = current.Prev;
- }
- else
- {
- head = current.Prev;
- }
- }
- }
- _count++;
- return o;
- }
- public object Remove(int index)
- {
- object x;
- if (index < 0 || index > count)
- {
- throw new IndexOutOfRangeException("Az index nem lehet negatív!");
- }
- else if (Empty() || index == 0)
- {
- x = head.Next.Data;
- head.Next = head.Next.Next;
- }
- else
- {
- Node current = head;
- for (int i = 0; i < index; i++)
- {
- current = current.Next;
- }
- x = current.Data;
- current = current.Next;
- }
- _count--;
- return x;
- }
- public object Get(int index)
- {
- Node current = head;
- for (int i = 0; i < index - 1; i++)
- {
- current = current.Next;
- }
- return current.Next.Data;
- }
- public void Clear()
- {
- head = null;
- }
- public bool Empty()
- {
- return count == 0;
- }
- public int IndexOf(object o)
- {
- Node current = head;
- if (Empty() || o == null)
- {
- throw new Exception("");
- }
- else
- {
- for (int i = 0; i < count; i++)
- {
- if (current.Data.Equals(o))
- {
- return i;
- }
- current = current.Next;
- }
- return -1;
- }
- }
- public bool Contains(object o)
- {
- return !(IndexOf(o) == -1);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- DoublyList a = new DoublyList();
- a.Add(10);
- a.Add(10);
- a.Add(10);
- a.Add(10);
- a.Add(10);
- Console.WriteLine(a.Get(4));
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement