//Prints the current list in reversed order public void PrintReversed() { CustomNode <T> current = tail; for (int i = count; i > 0; i--) { Console.WriteLine(current.Data); current = current.Prev; } }
//inserts a new node to the list //Param name "date, the data to insert, and "index", the location of the data public void Insert(T data, int index) { if (head == null) { add(data); return; } else if (index < 0) { throw new IndexOutOfRangeException(" Invalid"); } else if (index >= count) { add(data); return; } CustomNode <T> previous = head; CustomNode <T> current = head; int position = 0; while (position < count) { if (position == index) { CustomNode <T> newNode = new CustomNode <T>(data); if (position == 0) { newNode.Next = head; head.Prev = newNode; head = newNode; } else { newNode.Next = current; previous.Next = newNode; newNode.Prev = current.Prev; current.Prev = newNode; count++; position = count; } } position++; previous = current; current = current.Next; } }
//Returns the data value from the node at the index specified public T GetData(int index) { if (index < 0 || index >= count) { throw new IndexOutOfRangeException("Invalid"); } // Loop through the nodes until we get // to the node at index CustomNode <T> current = head; for (int i = 0; i < index; i++) { current = current.Next; } // Return the data of the current node return(current.Data); }
//Removing elements from the list and returning its data //Param name "index" , the location of the index to remove from public T RemoveAt(int index) { if (index < 0 || index >= count) { throw new IndexOutOfRangeException("Index was invalid"); } else if (index == 0) { CustomNode <T> temp = head; head = head.Next; count--; return(temp.Data); } //if index is the last data in the list else if (index == count - 1) { CustomNode <T> temp = tail; tail = tail.Prev; tail.Next = null; count--; return(temp.Data); } //if the index points to a node in the middle of the list else { T tempdata = GetData(index); CustomNode <T> current = head; for (int i = 0; i < index; i++) { current = current.Next; } current.Next.Prev = current.Prev; current.Prev.Next = current.Next; count--; return(tempdata); } }
//Adds a new node object to the list //param name "data" public void add(T data) { CustomNode <T> node = new CustomNode <T>(data); if (count == 0) { head = node; tail = node; } else if (count == 1) { head.Next = node; tail = node; tail.Prev = head; } else { node.Prev = tail;// current tail tail.Next = node; tail = node; // new tail } count++; }
//Clears the entire list public void Clear() { head = null; tail = null; count = 0; }
public CustomNode(T data) { this.data = data; next = null; }
static void Main(string[] args) { //Creates Custom Linked List CustomLinkedList <string> words = new CustomLinkedList <string>(); Console.WriteLine("Type Something:"); String input; //Looks for a specific sets of commands and executes the right function from the LinkedList class. while (true) { input = Console.ReadLine(); if (input == "quit" || input == "q") { Console.WriteLine("The loop has ended"); return; } else if (input == "print") { Console.WriteLine(" The following items are in the list"); for (int i = 0; i < words.Count; i++) { Console.WriteLine(words.GetData(i)); } } else if (input == "count") { Console.WriteLine(" There are currently " + words.Count + " items in the list"); } else if (input == "clear") { words.Clear(); Console.WriteLine("The list has been cleared"); } else if (input == "remove") { Random t = new Random(); words.RemoveAt(t.Next(words.Count)); Console.WriteLine("One element has been randomly removed from the list"); } else if (input == "reverse") { Console.WriteLine("The following items are in the list in reversed order"); words.PrintReversed(); } else if (input == "scramble") { //Removes a single random element from the list and insert it back into the list at another random index //Press print after the scramble command to see the results Random t = new Random(); int randomIndex = t.Next(words.Count); CustomNode <string> temp = new CustomNode <string>(words.GetData(randomIndex)); words.RemoveAt(randomIndex); int newrandom = t.Next(words.Count); words.Insert(temp.Data, newrandom); } //simply adds any input to the list other than the command else { words.add(input); } } }