public bool Remove(T value) { if (Count == 0) { return(false); } if (Count == 1) { if (_head.Value.Equals(value)) { _head = null; _tail = null; Count--; return(true); } return(false); } LinkedNode <T> previous = null; var current = _head; while (current != null) { if (current.Value.Equals(value)) { if (current == _head) { _head = _head.Next; Count--; return(true); } if (current == _tail) { previous.Next = _tail = null; Count--; return(true); } previous.Next = current.Next; Count--; return(true); } previous = current; current = current.Next; } return(false); }
public void printList(LinkedNode curr) { if (curr == null) { Console.WriteLine("\nEmpty linked list"); } Console.WriteLine("\nLinked list characters: "); while (curr != null) { Console.Write(curr.data + "->"); curr = curr.next; } Console.WriteLine("End"); }
public void Push(T value) { if (_head == null) { _head = new LinkedNode <T>(value); _tail = _head; } else { var previousHead = _head; _head = new LinkedNode <T>(value); _head.Next = previousHead; } Count++; }
public void Append(T value) { if (_head == null) { _head = new LinkedNode <T>(value); _tail = _head; } else { var newNode = new LinkedNode <T>(value); _tail.Next = newNode; _tail = newNode; } Count++; }
public void deleteTri(LinkedNode root) { LinkedNode curr = head; LinkedNode prev = head; //Traverse through the list, storing the values in a dict char:num IDictionary <char, int> dict = new Dictionary <char, int>(); while (curr != null) { int index = 0; if (dict.ContainsKey(curr.data)) { dict[curr.data] += 1; if (dict[curr.data] > 2) { if (curr.next == null) { prev.next = null; } //delete the curr node prev.next = curr.next; } } else { dict.Add(curr.data, 1); } index++; prev = curr; curr = curr.next; } foreach (KeyValuePair <char, int> item in dict) { Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value); } }
public void delete(char del) { LinkedNode prev = null; LinkedNode curr = head; if (head.data == del) { head = head.next; } else { while (curr != null) { if (curr.data == del) { prev.next = curr.next; } prev = curr; curr = curr.next; } } }
public LinkedList() { head = null; }
public LinkedNode(char val) { data = val; next = null; count++; }
public LinkedNode(T value, LinkedNode <T> next) { Value = value; Next = next; }
static void Main(string[] args) { Console.WriteLine("Binary Tree implementation"); Console.WriteLine("---------------------------"); //Binary tree implementation Node root = new Node(3); root.left = new Node(5); root.right = new Node(6); root.left.right = new Node(9); root.left.left = new Node(11); root.right.left = new Node(18); root.right.right = new Node(20); //Binary tree Tree tree = new Tree(); //Inorder tree traversal Console.Write("Tree leafs: "); tree.traversal(root); //Sum of the leafs Console.WriteLine("\nThe sum is: " + tree.sumLeaf(root)); //Depth of the tree Console.WriteLine("The depth of the tree is: " + tree.findDepth(root, 0)); Console.WriteLine("\n\nUser defined LinkedList implementation"); Console.WriteLine("--------------------------------------"); //Linked list implementation LinkedList linkedList = new LinkedList(); LinkedNode firstNode = new LinkedNode('E'); LinkedNode secondNode = new LinkedNode('B'); LinkedNode thirdNode = new LinkedNode('E'); LinkedNode forthNode = new LinkedNode('E'); LinkedNode fithNode = new LinkedNode('B'); LinkedNode sixthNode = new LinkedNode('A'); LinkedNode seventhNode = new LinkedNode('B'); linkedList.head = firstNode; firstNode.next = secondNode; secondNode.next = thirdNode; thirdNode.next = forthNode; forthNode.next = fithNode; fithNode.next = sixthNode; sixthNode.next = seventhNode; Console.Write("Number of chars in Linked list: " + LinkedNode.count); //Console.WriteLine(forthNode.data); linkedList.printList(linkedList.head); //linkedList.delete('B'); //Console.Write("After deletion: " + LinkedNode.count); //Console.WriteLine(forthNode.data); linkedList.printList(linkedList.head); linkedList.deleteTri(linkedList.head); Console.Write("After deletion"); linkedList.printList(linkedList.head); Console.WriteLine("\n\nSystem defined LinkedList implementation"); Console.WriteLine("----------------------------------------"); //Inbuilt C# Linkedlist LinkedList <Char> linkedList2 = new LinkedList <char>(); linkedList2.AddLast('A'); linkedList2.AddLast('B'); linkedList2.AddLast('A'); linkedList2.AddLast('C'); Console.WriteLine("Number of chars in Linked list: " + linkedList2.Count); Console.Write("Linked list characters: "); foreach (char i in linkedList2) { Console.Write(i + "->"); } Console.WriteLine("End"); linkedList2.Remove('A'); foreach (char i in linkedList2) { Console.Write(i + "->"); } Console.WriteLine("End"); }