Situations for using a Doubly Linked list would be
- deck of cards
- browser cache
- Metro
A new node’s previous points left, and next points right. When we add a new node, we pass in the linked list’s tail into the new Node’s constructor.
The new Node will re-wire it so that its previous points to the last node of the linked list (via the tail).
Then have the next of the last node point to the new Node.
Then, the linked list will proceed to re-assign the tail to the new node. Thus, completing the push.
Also, each node is connected with each by 2 traits:
- The first node’s next points to the second node. The first node’s previous points to the node on its left side. (or bottom, if you are looking at it vertically)
- The second node’s previous points to the first node. And its next points to the node on its right side. (or top, if you are looking at it vertically)
Getter/Setters
Getter setter is done by first declaring a name that matches your property but with a capital first letter. Then, you use keywords get and write a function for when you retrieve the property.
Use keyword set to write a function where you assign another value to your property. The incoming value is designated as keyword “value”. The value keyword is a contextual keyword, that is, it has a different meaning based on its context.
Inside a set block, it simply means the value that the programmer has set it to.
Outside of a set block, you can use value as a variable identifier, as such:
int value = 5;
1 2 3 4 5 6 7 8 9 10 11 |
public int Count { get { Console.WriteLine("-- DoublyLinkedList - Count, getter --"); return count; } set { Console.WriteLine("-- DoublyLinkedList - Count, setter --"); count = value; } } |
You’d use it like this:
myList.Count = 222;
Doubly Linked List source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
using System; public class Program { //note that previous points left, and next points right //create reference type Node public class DoublyLinkedNode { public DoublyLinkedNode next; public DoublyLinkedNode previous; public object data; // constructors public DoublyLinkedNode(Object newData) { this.previous = null; this.data = newData; this.next = null; } //if tail comes in, our previous, pointing left, would now point to tail, AKA end of the list //then tail.next takes on this. Thus, we just attached this Node to the end of the list public DoublyLinkedNode(object newData, DoublyLinkedNode previous) { this.data = newData; this.previous = previous; previous.next = this; } // data getter/setter public Object Data { get { return data; } set { data = value; } } // next getter/setter public DoublyLinkedNode Next { get { return next; } set { next = value; } } // next getter/setter public DoublyLinkedNode Previous { get { return previous; } set { previous = value; } } } //end of DoublyLinkedNode public class DoublyLinkedList { private DoublyLinkedNode head; private DoublyLinkedNode tail; public int count; // in order to use the getter/setter, call Count public int Count { get { Console.WriteLine("-- DoublyLinkedList - Count, setter --"); return count; } set { Console.WriteLine("-- DoublyLinkedList - Count, getter --"); count = value; } } // in order to use the getter/setter, call Head public DoublyLinkedNode Head { get { Console.WriteLine("DoublyLinkedList - Head, getter"); return head; } set { Console.WriteLine("DoublyLinkedList - Head, setter"); head = value; } } //constructor public DoublyLinkedList() { Console.WriteLine("-- DoublyLinkedList Constructor --"); this.head = null; this.tail = null; this.count = 0; //use 'this' to distinguish in case there are same variable name Console.WriteLine("-- DoublyLinkedList Constructor Done --"); } public void Add(object item) { if (this.head==null) { this.head = new DoublyLinkedNode(item); this.tail = this.head; } else { // incoming item should now be at the end of the list. // we pass in the tail so that the Node can sort out the attachment // 1) assign the data // 2) previous of new node points to this.tail, aka the last Node // 3) this.tail.next points to new Node DoublyLinkedNode newItem = new DoublyLinkedNode(item, this.tail); // 4) finally, reassign tail to this new Item this.tail = newItem; } } public void printForward() { Console.WriteLine("printForward Start----"); for (DoublyLinkedNode temp = this.head; temp != null; temp = temp.next) { Console.WriteLine("print: {0}", temp.data); } Console.WriteLine("printForward End----"); } public void printBackward() { Console.WriteLine("printBackward Start----"); for (DoublyLinkedNode temp = this.tail; temp != null; temp = temp.previous) { Console.WriteLine("print: {0}", temp.data); } Console.WriteLine("printBackward End----"); } } //end of DoublyLinkedList public static void Main() { Console.WriteLine("Hello World"); DoublyLinkedList myList = new DoublyLinkedList(); Console.WriteLine("{0} ", myList.Count); //uses the getter myList.Count = 222; Console.WriteLine("{0} ", myList.Count); //uses the getter myList.Add("haha"); myList.Add("hehe"); myList.Add("hoho"); myList.printForward(); myList.printBackward(); } } |