C# 链表 --增 -删-反转-删除最小值
程序员文章站
2023-04-08 13:24:12
1. Node.cs 2.LinkList.cs 3.Program.cs 输出结果: 5为链表的长度 ......
1.
node.cs
namespace 链表 { public class node<t> { public t data; //这个就是地址 public node<t> next; //构造函数用来初始化 public node() { data = default(t); next = null; } public node(t value) { data = value; next = null; } } }
2.linklist.cs
namespace 链表 { //一般链表都是有头部节点的,简称头结点,头结点不参与运算 public class linklist<t> { private node<t> _head; private int _count; public linklist() { _head = new node<t>(); _count = 0; } public void additem(node<t> newnode) { node<t> tmpnode = _head; //找到头结点 while (tmpnode.next != null) //循环找到最后节点 { tmpnode = tmpnode.next; //一直下移 } tmpnode.next = newnode; //将最后节点和即将插入的节点连接 _count++; } public int getlength() { return _count; } public void insert(int index, node<t> newnode) //插 { if(index<0||index>_count) { console.writeline("over"); return; } node<t> tmpnode = _head; for (int i = 0; i < index; i++) { tmpnode = tmpnode.next; } //tmpnode (index的前一个节点) newnode.next = tmpnode.next; tmpnode.next = newnode; _count++; } //第一个为index,第二个为value public void showitem(action<int,t> ac) { if (_count ==0) { console.writeline("空"); return; } node<t> tmpnode = _head.next; for (int i = 0; i < _count; i++) { ac(i, tmpnode.data); tmpnode = tmpnode.next; } } public t removeat(int index) { t returnvalue = default(t); // 定义一个data的返回值 if (index < 0 || index >= _count)//判断是否出界 { console.writeline("error"); goto returntip; } node<t> tmpnode = _head; //删除节点的前一个节点 for (int i = 0; i < index; i++)//循环走 { tmpnode = tmpnode.next; } node<t> deletenode = tmpnode.next; //要删除的节点 tmpnode.next = tmpnode.next.next;//牵手删除节点的后一个节点 deletenode.next = null;//不让其连接 _count--; returnvalue = deletenode.data;//返回删除节点的数据data returntip: return returnvalue; } public void reverse() //链表反转 { if (_count < 1) { console.writeline("链表长度不足"); return; } node<t> x1, x2; x2 = _head.next; _head.next = null; while (x2 != null) { x1 = x2.next; x2.next = _head.next; _head.next = x2; x2 = x1; } } public t removemindemo(func<node<t>,node<t>,boolean> _func) //删除最小值 { node<t> deletepremin, deletemin, premin, min; deletepremin = premin = _head; deletemin = min = _head.next; while (min != null) { if (_func(min,deletemin)) { deletepremin = premin; deletemin = min; } premin = premin.next; min = min.next; } deletepremin.next = deletepremin.next.next; deletemin.next = null; _count--; return deletemin.data; } public void clear() { _head.next = null; _count = 0; } } }
3.program.cs
namespace 链表 { class program { public static bool sh(node<int> a, node<int> b) { if (a.data > b.data) { return false; } return true; } public static void show(int index, int value) { console.writeline("第{0}个元素是{1}",index+1,value); } static void main(string[] args) { linklist<int> linklist = new linklist<int>(); linklist.additem(new node<int>(9)); linklist.additem(new node<int>(3)); linklist.additem(new node<int>(10)); linklist.additem(new node<int>(4)); linklist.additem(new node<int>(21)); linklist.additem(new node<int>(100)); linklist.reverse();//反转 //9 3 10 4 21 100 linklist.removemindemo(sh); //删除最小值 linklist.showitem(show); console.writeline(linklist.getlength()); console.readline(); } } }
输出结果:
5为链表的长度
上一篇: 补水方法不对更干燥 教你秋天如何正确补水