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 LinkedListTest
- {
- class LinkedListTest
- {
- static void Main(string[] args)
- {
- //avaliable commands - Add,AddRange,InsertAt,InsertRange,Remove,RemoveAt,RemoveRange,ElementAt,IndexOf
- LinkedList testList = new LinkedList();
- }
- }
- class Node
- {
- private Node next;
- private object currValue;
- public Node Next
- {
- get { return this.next; }
- set { this.next = value; }
- }
- public object Value
- {
- get { return this.currValue; }
- set { this.currValue = value; }
- }
- }
- class LinkedList
- {
- private Node head;
- private int count;
- public int Count { get { return this.count; } }
- public void Add(object element)
- {
- if (head == null)
- {
- head = new Node();
- head.Next = null;
- head.Value = element;
- }
- else
- {
- Node toAdd = new Node();
- toAdd.Value = element;
- Node currNode = head;
- while (currNode.Next != null)
- {
- currNode = currNode.Next;
- }
- currNode.Next = toAdd;
- }
- count++;
- }
- public void AddRange(object[] items)
- {
- foreach (object item in items)
- {
- Add(item);
- }
- }
- public void InsertAt(object element, int index)
- {
- if (count != 0 && (index < 0 || index > count))
- {
- string message = string.Format
- ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
- throw new IndexOutOfRangeException(message);
- }
- if (head != null && index == 0)
- {
- Node toInsert = new Node();
- toInsert.Value = element;
- toInsert.Next = head;
- head = toInsert;
- }
- else if (head == null)
- {
- head = new Node();
- head.Value = element;
- head.Next = null;
- }
- else
- {
- Node toInsert = new Node();
- toInsert.Value = element;
- int indexCounter = 0;
- Node currNode = head;
- while (currNode.Next != null && indexCounter + 1 != index)
- {
- currNode = currNode.Next;
- indexCounter++;
- }
- toInsert.Next = currNode.Next;
- currNode.Next = toInsert;
- }
- count++;
- }
- public void InsertRange(object[] items, int index)
- {
- foreach (object item in items)
- {
- InsertAt(item, index);
- index++;
- }
- }
- public void Remove(object item)
- {
- bool containsItem = false;
- Node currNode = head;
- Node prevNode = null;
- while (currNode.Next != null)
- {
- if (currNode.Value.Equals(item))
- {
- containsItem = true;
- break;
- }
- prevNode = currNode;
- currNode = currNode.Next;
- }
- if (!containsItem && currNode.Value.Equals(item))
- {
- containsItem = true;
- }
- if (!containsItem && head.Value.Equals(item))
- {
- head = null;
- }
- else if (!containsItem)
- {
- throw new InvalidOperationException($"Object [{item.ToString()}] does not exist in the list.");
- }
- else if (containsItem && head.Value.Equals(item))
- {
- head = currNode.Next;
- }
- else
- {
- prevNode.Next = currNode.Next;
- currNode = null;
- }
- count--;
- }
- public void RemoveAt(int index)
- {
- if (index < 0 || index >= count)
- {
- string message = string.Format
- ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
- throw new IndexOutOfRangeException(message);
- }
- bool indexIsFound = false;
- Node prevNode = null;
- Node currNode = head;
- int indexCounter = 0;
- while (currNode.Next != null)
- {
- if (index == indexCounter)
- {
- indexIsFound = true;
- break;
- }
- prevNode = currNode;
- currNode = currNode.Next;
- indexCounter++;
- }
- if (!indexIsFound && index == indexCounter)
- {
- indexIsFound = true;
- }
- if (indexIsFound && index == 0)
- {
- head = currNode.Next;
- }
- else if (indexIsFound)
- {
- prevNode.Next = currNode.Next;
- currNode = null;
- }
- count--;
- }
- public void RemoveRange(object[] items)
- {
- foreach (object item in items)
- {
- Remove(item);
- }
- }
- public object ElementAt(int index)
- {
- if (index < 0 || index >= count)
- {
- string message = string.Format
- ("You tried to reach index [{0}] but the index of the last element is [{1}]", index, count - 1);
- throw new IndexOutOfRangeException(message);
- }
- Node currNode = head;
- bool searchIsFinished = false;
- int indexCounter = 0;
- while (!searchIsFinished)
- {
- if (currNode == null)
- {
- searchIsFinished = true;
- break;
- }
- if (indexCounter == index)
- {
- return currNode.Value;
- }
- currNode = currNode.Next;
- indexCounter++;
- }
- return currNode.Value;
- }
- public int IndexOf(object item)
- {
- bool itemExists = false;
- Node currNode = head;
- int indexCount = 0;
- while (currNode != null)
- {
- if (currNode.Value.Equals(item))
- {
- itemExists = true;
- break;
- }
- currNode = currNode.Next;
- indexCount++;
- }
- if (itemExists)
- {
- return indexCount;
- }
- else
- {
- throw new InvalidOperationException($"Item [{item.ToString()}] does not exist.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment