数据结构 单向简单链表java

xiaoxiao2021-02-28  6

1,定义一个数据类 DATA

2,定义一个结点类 CLType

3,第一个元素为head

注意:

1)无论怎么添加元素时,定义好head是哪一个,返回head

2)其他操作在 方法形参 有head的情况下,算法不难

3)使用时,定义一个结点head = null

//定义链表

package com.gec1; class DATA2{ String key; String name; int age; public DATA2() { super(); // TODO Auto-generated constructor stub } public DATA2(String key, String name, int age) { super(); this.key = key; this.name = name; this.age = age; } @Override public String toString() { return "DATA2 [key=" + key + ", name=" + name + ", age=" + age + "]"; } } public class CLType { DATA2 nodeData = new DATA2(); CLType nextNode; //追加尾部 CLType CLAddEnd(CLType head,DATA2 nodeData) { CLType node,htemp; node = new CLType(); node.nodeData = nodeData; node.nextNode = null; if(head == null) { head = node; return head; } htemp = head; while(htemp.nextNode!=null) { htemp = htemp.nextNode; } htemp.nextNode = node; return head; } //追加头部 CLType CLAddFirst(CLType head,DATA2 nodeData) { CLType node = new CLType(); node.nodeData = nodeData; if(head == null) head = node; else { node.nextNode = head; head = node; } return head; } //查找节点,关键字 CLType clFindNode(CLType head,String key) { CLType htemp = head; while (htemp!=null) { if(htemp.nodeData.key.equals(key)) { return htemp; } htemp = htemp.nextNode; } return null; } //插入结点 CLType CLInsertNode(CLType head,String findkey,DATA2 nodeData) { CLType node,htemp; node = new CLType(); node.nodeData = nodeData; htemp = clFindNode(head, findkey); if(htemp!=null) { node.nextNode = htemp.nextNode; htemp.nextNode = node; }else System.out.println("没有找到插入位置"); return head; } //删除结点 int CLDeleteNode(CLType head,String key) { CLType node ,htemp; htemp = head; node = head; while(htemp != null) { if(htemp.nodeData.key.equals(key)) { node.nextNode = htemp.nextNode; htemp = null; return 1; } node = htemp; htemp = htemp.nextNode; } return 0; } //获取链表结点数 int CLLength(CLType head) { CLType htemp = head; int len = 0; while(htemp!=null) { len ++; htemp = htemp.nextNode; } return len; } //遍历所有结点 void CLAllNode(CLType head) { CLType htemp = head; DATA2 nodeData; while(htemp!=null) { nodeData = htemp.nodeData; System.out.println(nodeData); htemp = htemp.nextNode; } } @Override public String toString() { return nodeData.toString() ; } }

//测试用例

package com.gec1; public class MainTest { public static void main(String[] args) { CLType node, head = null; CLType cl = new CLType(); String key,findKey; //定义元素 DATA2 data1 = new DATA2("1","小明",23); DATA2 data2 = new DATA2("2","小红",21); DATA2 data3 = new DATA2("3","小红1",21); DATA2 data4 = new DATA2("4","小红2",21); DATA2 data5 = new DATA2("5","小红3",21); DATA2 data6 = new DATA2("6","小红4",21); //添加元素 head = cl.CLAddFirst(head, data1); //System.out.println("head = "+head); head = cl.CLAddFirst(head, data2); head = cl.CLAddFirst(head, data3); head = cl.CLAddFirst(head, data4); head = cl.CLAddFirst(head, data5); head = cl.CLAddFirst(head, data6); //插入结点 findKey = "3"; DATA2 data7 = new DATA2("插入","小红4",21); cl.CLInsertNode(head, findKey, data7); //删除结点 key = "2"; cl.CLDeleteNode(head, key); //查找元素 key = "插入"; CLType findCL = cl.clFindNode(head, key); System.out.println("查找 = "+ findCL); //遍历元素 cl.CLAllNode(head); } }

转载请注明原文地址: https://www.6miu.com/read-1950267.html

最新回复(0)