linked list - C# how to modify the linkedlist node property? -
i have linkedlist class follows:
public class dbnode<t> { private t _data; private dbnode<t> _prev; private dbnode<t> _next; public t data { { return this._data; } set { this._data = value; } } public dbnode<t> prev { { return this._prev; } set { this._prev = value; } } public dbnode<t> next { { return this._next; } set { this._next = value; } } public dbnode(t data, dbnode<t> prev, dbnode<t> next) { this._data = data; this._prev = prev; this._next = next; } public dbnode(t data, dbnode<t> prev) { this._data = data; this._prev = prev; this._next = null; } public dbnode(dbnode<t> next) { this._data = default(t); this._next = next; this._prev = null; } public dbnode(t data) { this._data = data; this._prev = null; this._next = null; } public dbnode() { this._data = default(t); this._prev = null; this._next = null; } } public class dblinkedlist<t> { private dbnode<t> _head; public dbnode<t> head { { return this._head; } set { this._head = value; } } public dblinkedlist() { head = null; } public t this[int index] { { return this.getitemat(index); } } public bool isempty() { return head == null; } public t getitemat(int i) { if (isempty()) { console.writeline("the double linked list empty."); return default(t); } dbnode<t> p = new dbnode<t>(); p = head; if (0 == i) { return p.data; } int j = 0; while (p.next != null && j < i) { j++; p = p.next; } if (j == i) { return p.data; } else { console.writeline("the node dose not exist."); return default(t); } } public int count() { dbnode<t> p = head; int length = 0; while (p != null) { length++; p = p.next; } return length; } public void clear() { this.head = null; } public void addafter(t item, int i) { if (isempty() || < 0) { console.writeline("the double linked list empty or position uncorrect."); return; } if (0 == i) { dbnode<t> newnode = new dbnode<t>(item); newnode.next = head.next; head.next.prev = newnode; head.next = newnode; newnode.prev = head; return; } dbnode<t> p = head; int j = 0; while (p != null && j < i) { p = p.next; j++; } if (j == i) { dbnode<t> newnode = new dbnode<t>(item); newnode.next = p.next; if (p.next != null) { p.next.prev = newnode; } newnode.prev = p; p.next = newnode; } else { console.writeline("the position uncorrect."); } } public void addbefore(t item, int i) { if (isempty() || < 0) { console.writeline("the double linked list empty or position uncorrect."); return; } if (0 == i) { dbnode<t> newnode = new dbnode<t>(item); newnode.next = head; head.prev = newnode; head = newnode; return; } dbnode<t> n = head; dbnode<t> d = new dbnode<t>(); int j = 0; while (n.next != null && j < i) { d = n; n = n.next; j++; } if (n.next == null) { dbnode<t> newnode = new dbnode<t>(item); n.next = newnode; newnode.prev = n; newnode.next = null; } else { if (j == i) { dbnode<t> newnode = new dbnode<t>(item); d.next = newnode; newnode.prev = d; newnode.next = n; n.prev = newnode; } } } public void addlast(t item) { dbnode<t> newnode = new dbnode<t>(item); dbnode<t> p = new dbnode<t>(); if (head == null) { head = newnode; return; } p = head; while (p.next != null) { p = p.next; } p.next = newnode; newnode.prev = p; } public t removeat(int i) { if (isempty() || < 0) { console.writeline("the double linked list empty or position uncorrect."); return default(t); } dbnode<t> q = new dbnode<t>(); if (0 == i) { q = head; head = head.next; head.prev = null; return q.data; } dbnode<t> p = head; int j = 0; while (p.next != null && j < i) { j++; q = p; p = p.next; } if (i == j) { p.next.prev = q; q.next = p.next; return p.data; } else { console.writeline("the position uncorrect."); return default(t); } } public int indexof(t value) { if (isempty()) { console.writeline("the list empty."); return -1; } dbnode<t> p = new dbnode<t>(); p = head; int = 0; while (p.next != null && !p.data.equals(value)) { p = p.next; i++; } return i; } public void reverse() { dblinkedlist<t> tmplist = new dblinkedlist<t>(); dbnode<t> p = this.head; tmplist.head = new dbnode<t>(p.data); p = p.next; while (p != null) { tmplist.addbefore(p.data, 0); p = p.next; } this.head = tmplist.head; tmplist = null; } public string reversebyprev() { dbnode<t> tail = getnodeat(count() - 1); stringbuilder sb = new stringbuilder(); sb.append(tail.data.tostring() + ","); while (tail.prev != null) { sb.append(tail.prev.data + ","); tail = tail.prev; } return sb.tostring().trimend(','); } private dbnode<t> getnodeat(int i) { if (isempty()) { console.writeline("the list empty."); return null; } dbnode<t> p = new dbnode<t>(); p = this.head; if (0 == i) { return p; } int j = 0; while (p.next != null && j < i) { j++; p = p.next; } if (j == i) { return p; } else { console.writeline("the node not exist."); return null; } } public t fisrt() { return this.getitemat(0); } public t last() { return this.getitemat(this.count() - 1); } }
and alse have class product
contains properties:productid
, productvalue
:
public class product { private byte _productid; public byte productid { { return _productid; } set { _productid = value; notifypropertychanged("productid"); } } private uint16 _productvalue; public uint16 productvalue { { return _productvalue; } set { _productvalue = value; notifypropertychanged("productvalue"); } } }
i have listbox want add product
when click treeview:
mylinklist<product> mylinklist = new mylinklist<product>(); private void menuitem_onclick(object sender, routedeventargs e) { var item = this.treeview.selecteditem product; listbox.items.add(item); mylinklist.append(item); }
now problem want modify item's property selected add based on former one. example: if former one's productid
1
, selecteditem.productvalue = formeritem.productvalue + 1
, how suppose this? thanks!
Comments
Post a Comment