public void Push(Object item) { var node = new Node((int)item); node.Next = top; top = node; }
// Initializes the LL with a head public DoublyLinkedList(int val) { head = new Node(val); head.next = null; head.prev = null; tail = head; }
private void InsertAtHead(int value) { if(IsEmpty()) { var node = new Node(value); _head = node; _tail = node; } }
public void Have_Ability_To_Add_To_Head_Or_Tail_Node() { LinkedList<int> list = new LinkedList<int>(); Node<int> headNode = new Node<int>(100); Node<int> tailNode = new Node<int>(200); list.AddToHead(headNode); list.AddToTail(tailNode); Assert.AreSame(headNode, list.Head); Assert.AreSame(tailNode, list.Tail); }
public void Have_A_Pointer_To_Next_Node() { Node<int> nodeA = new Node<int>(); nodeA.Value = 100; Node<int> nodeB = new Node<int>(); nodeB.Value = 200; nodeA.Next = nodeB; Assert.AreSame(nodeB, nodeA.Next); }
public Object Pop() { if (top != null) { Object item = top.Data; top = top.Next; return item; } return null; }
public void Add(int value) { if (ReferenceEquals(First, null)) First = new Node { Value = value }; else { var current = First; while (!ReferenceEquals(current.Next, null)) current = current.Next; current.Next = new Node { Value = value }; } Count++; }
public void Push(int value) { if (_node == null) { _node = new Node(value); } else { var newNode = new Node(value); newNode.SetNextNode(_node); _node = newNode; } }
public void Enqueue(Object item) { if (_first == null) { _last = new Node((int)item); _first = _last; } else { _last.Next = new Node((int)item); _last = _last.Next; } }
public static HashTable<char, BitStream> MakeEncodingTable(Node node, BitStream progress) { HashTable<char,BitStream> result = new HashTable<char,BitStream>(char.MaxValue,HashChar); //if the current node is not null and it is a leaf, we want to add it to result if (node != null && node.Left == null && node.Right == null) { //we only store leaf nodes in the result array //make a new bit stream BitStream stream = new BitStream(); //append progress to it stream.Append(progress); //add node.data and the new stream to result result.Add(node.Data, stream); } //else if the node is not null, we are going to want to process it's childern else { if (node != null && node.Left != null) { //if left subtree is not null //make new bit stream BitStream stream = new BitStream(); //append progress stream.Append(progress); //append 0 (because we are going left) stream.Append(false); //recursivley call this function on node.Left with the new stream we just created //store the result of the previous call in a local hashtable HashTable<char, BitStream> local = MakeEncodingTable(node.Left, stream); //loop through the just created hash table(the return of the recursive function) SLinkedList<char> list = local.Keys; for (int i = 0; i < list.Size; i++) { //add each key value pair to this function's result hash table result.Add(list[i], local[list[i]]); //we do this because the results of each recursion bubble back up to the top. //by the time the root returns, all of the leaf nodes will be in the final hash table }//end loop }//end if if (node != null && node.Right != null) { //repeat above for the right subtree BitStream stream = new BitStream(); stream.Append(progress); stream.Append(true); HashTable<char, BitStream> local = MakeEncodingTable(node.Right, stream); SLinkedList<char> list = local.Keys; for (int i = 0; i < list.Size; i++) { result.Add(list[i], local[list[i]]); } }//end else } return result; }
public void Have_Count_Of_Current_Items() { LinkedList<int> list = new LinkedList<int>(); Node<int> node100 = new Node<int>(100); Node<int> node200 = new Node<int>(200); Node<int> node300 = new Node<int>(300); Assert.AreEqual(0, list.Count); list.AddToTail(node100); Assert.AreEqual(1, list.Count); list.AddToTail(node200); Assert.AreEqual(2, list.Count); list.AddToTail(node300); Assert.AreEqual(3, list.Count); }
// Compares by Length, Height, and W<idth. public int CompareTo(DataStructures.Node a, DataStructures.Node b) { if (a.Value > b.Value) { return(1); } else if (a.Value < b.Value) { return(-1); } else { return(0); } }
public void Have_Ability_To_Remove_Head_Or_Tail_Node() { LinkedList<int> list = new LinkedList<int>(); Node<int> node100 = new Node<int>(100); Node<int> node200 = new Node<int>(200); Node<int> node300 = new Node<int>(300); list.AddToTail(node100); list.AddToTail(node200); list.AddToTail(node300); Assert.AreSame(node100, list.Head); list.RemoveFromHead(); Assert.AreSame(node200, list.Head); Assert.AreSame(node300, list.Tail); list.RemoveFromTail(); Assert.AreSame(node200, list.Tail); }
public Object Dequeue() { if (_first != null) { Object item = _first.Data; _first = _first.Next; if (_first == null) { // Queue is empty _last = null; } return item; } return null; }
// Adds a new node to the tail of the LL // O(n) time public void Append(int val) { if (tail == null) { head = new Node(val); tail = head; return; } if (head == tail) { tail = new Node(val); tail.prev = head; head.next = tail; return; } var newTail = new Node(val); tail.next = newTail; newTail.next = null; newTail.prev = tail; tail = newTail; }
// Searches LL for the first occurence of a node with val = val, and removes it // O(n) time // Post: val if node was found and deleted, -1 if no such node with val = val exists public int Delete(int val) { Node current = head; Node previous = null; while (current != null) { if (current.val == val) { if (previous == null) // at head { head = current.next; if (head != null) { head.prev = null; } } else { if (current.next == null) // at tail { current.prev.next = null; tail = current; } else { current.prev.next = current.next; current.next.prev = current.prev; } } return val; } previous = current; current = current.next; } return -1; }
public NodeList(Node node) : base(node) { }
// Traverses the LL from the head to the specified index and removes the node // O(n) time // Post: node's val if node was found and deleted, -1 if no such node at index = index exists public int DeleteAt(int index) { if (index < 0) { return -1; } int currIndex = 0; Node current = head; Node previous = null; while (current != null) { if (currIndex == index) { int nodeVal; if (previous == null) // at head { if (current == tail) { head = null; tail = null; return index; } nodeVal = head.val; head = current.next; if (head != null) { head.prev = null; } } else { if (current.next == null) // at tail { current.prev.next = null; tail = current; } else { current.prev.next = current.next; current.next.prev = current.prev; } nodeVal = current.val; } return nodeVal; } previous = current; current = current.next; currIndex++; } return -1; }
// Helper - Inits the doubly linked list with values 5 to 0, head to tail public void InitializeDLL() { this.head = null; for (int i = 0; i < 6; i++) { Push(i); } }
public static Node MakeHuffmanTree(HashTable<char, int> frequencyTable) { //First, create a singly linked list of nodes to return SLinkedList<Node> looseNodes = new SLinkedList<Node>(); //we need to loop through the given frequency table SLinkedList<char> keys = frequencyTable.Keys; for (int i = 0; i < keys.Size; i++) { //make new node for each element in hash table Node n = new Node(); //node Data is current key (keys[i]) n.Data = keys[i]; //node frequency is going to be the value for the current key (table[key]) n.Frequency = frequencyTable[keys[i]]; //add this new node to the end(tail) of the loose nodes list looseNodes.AddTail(n); }//end loop /* Comments, no code */ // Next we need to take this loose collection of nodes and build a tree out of it. // We do this by combining the smallest value nodes under a parent node. Each iteration // Of the below loop will reduce size by 1, because it removes two nodes and adds one. // We know we have a tree built, when the loose nodes list has a size of 1. /* End comments */ //loop while the looseNodes list has more than one element in it while (looseNodes.Size > 1) { //make local variable called left and set it to looseNodes[0] Node left = looseNodes[0]; //we want to set left to the lowest frequency node, loop through nodes for (int i = 0; i < looseNodes.Size; i++) { //if frequency of current node is less than left if (left.Frequency > looseNodes[i].Frequency) { //set left to current node left = looseNodes[i]; }//end if }//end loop //now that we have a reference to the smallest node, lets remove it from the list looseNodes.RemoveAt(looseNodes.IndexOf(left)); //Repeat above steps for a new local node called right Node right = looseNodes[0]; for (int i = 0; i < looseNodes.Size; i++) { if (right.Frequency > looseNodes[i].Frequency) { right = looseNodes[i]; } } looseNodes.RemoveAt(looseNodes.IndexOf(right)); //Make a new node Node n = new Node(); //set its left to the local left node n.Left = right; //set its right to the local right node n.Right = left; //set its frequency to the sum of the left and right nodes frequencies n.Frequency = right.Frequency + left.Frequency; //set its data to '\0' (char equivalent of null) n.Data = '\0'; //Add the new node to the end of the looseNodes list(AddTail) looseNodes.AddTail(n); }//end loop //at this point the loose node list has only one element, and it's the root node of our huffman table //return [0] of the loose list return looseNodes[0]; //end loop ?? what loop }
public void Have_A_Value() { Node<int> node = new Node<int>(); node.Value = 100; Assert.AreEqual(100, node.Value); }
// Removes the tail from the LL // O(1) time public int Pop() { if (tail == null) { throw new Exception("Cannot pop from an empty Linked List!"); } int tailVal; if (head == tail) { tailVal = tail.val; head = null; tail = null; return tailVal; } tailVal = tail.val; tail = tail.prev; tail.next = null; return tailVal; }
// Adds a new node to the head of the LL // O(1) time public void Push(int val) { if (head == null) // new head and tail { head = new Node(val); tail = head; return; } if (head.next == null) // head becomes tail { tail = new Node(head.val); tail.prev = head; tail.next = null; head.next = tail; head.val = val; return; } var oldHead = new Node(head.val); // just a new head oldHead.next = head.next; oldHead.prev = head; head.next.prev = oldHead; head.val = val; head.next = oldHead; }
public Node(Node node) { Data = node.Data; }
public NodeTree(Node node) : base(node) { }
public void SetPrevious(Node node) { if (_previousNode == null) { _previousNode = node; _previousNode.SetNextNode(this); } }
public void SetAt(int index, Node element) { if (!IsWithinBounds(index)) return; if (index == 0) { if (ReferenceEquals(element, null)) { First = First.Next; Count--; } else { element.Next = First.Next; First = element; } } else { var current = First; for (int i = 0; i < index - 1; i++) current = current.Next; if (ReferenceEquals(element, null)) { current.Next = current.Next.Next; Count--; } else { element.Next = current.Next.Next; current.Next = element; } } }