c#顺序存储和链式存储
顺序存储顺序存储用一组地址连续的存储单元依次存储线性表的各个数据元素线性表是一种数据结构是由n个具有相同特性的数据元素的有限序列比如数组、ArrayList、Stack、Queue、链表等等顺序存储和链式存储 是数据结构中两种 存储结构数组、Stack、Queue、List、ArrayList —— 顺序存储只是 数组、Stack、Queue的 组织规则不同而已链式存储单向链表、双向链表、循环链表 —— 链式存储链式存储(链接存储)用一组任意的存储单元存储线性表中的各个数据元素注意点修改引用变量本身重新赋值→ 只影响该变量不影响其他变量。修改引用变量所指向的对象的内部字段如current.value xxx→ 会影响所有指向该对象的变量lastnode.nextNode node;与lastNode node;这两句中的lastnode是把原链表的lastnode。nextNode指向node再把现在的链表的lastnode指向node。lastnode.nextNode是原链表的最后一个节点的下个节点是让新增node上链lastnodenode;是改变链表的lastnode让它指向正确的最后一个节点也就是新加的node。手搓一个单向链表class LinkedListT { public LinkedNodeT headNode; public LinkedNodeT lastNode; public void Add(T value) { LinkedNodeT node new LinkedNodeT(value); if (headNode null) { headNode node; lastNode node; } else { lastNode.nextnode node; lastNode node; } } public void Remove(T value) { if (headNode null) { return; } if (headNode.value.Equals(value)) { headNodeheadNode.nextnode; if (headNode null) { lastNode null; } return; } LinkedNodeTcurrent headNode; while (current.nextnode ! null) { if (current.nextnode.value.Equals(value)) { current.nextnode current.nextnode.nextnode; if (current.nextnode null) lastNode current; return; } current current.nextnode; } } } class LinkedNodeT { public T value; public LinkedNode(T value) { this.value value; } public LinkedNodeT nextnode; } LinkedListint link new LinkedListint(); link.Add(1); link.Add(2); link.Add(3); link.Add(4); LinkedNodeint node link.headNode; while (node ! null) { Console.WriteLine(node.value); node node.nextnode; } link.Remove(2);