链表操作 顶

2019-08-20 10:48:35 浏览数 (1)

/**

* Created by Administrator on 2018-01-28.

*/

public class LinkedList {

public CLType CLAddEnd(CLType head,Data2 nodeData) {

CLType node,htemp;

if((node = new CLType()) == null) {

System.out.println("申请内存失败!");

return null;

}

else {

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;

}

}

public CLType CLAddFirst(CLType head,Data2 nodeData) {

CLType node;

if((node = new CLType()) == null) {

System.out.println("申请内存失败!");

return null;

}else {

node.nodeData = nodeData;

node.nextNode = head;

head = node;

return head;

}

}

public CLType CLFindNode(CLType head,String key) {

CLType htemp = head;

while (htemp != null) {

if(htemp.nodeData.getKey().equals(key)) {

return htemp;

}

htemp = htemp.nextNode;

}

return null;

}

public CLType CLInsertNode(CLType head,String findkey,Data2 nodeData) {

CLType node;

if((node = new CLType()) == null) {

System.out.println("申请内存失败");

return null;

}else {

node.nodeData = nodeData;

}

CLType htemp = head;

while (htemp != null) {

if(htemp.nodeData.getKey().equals(findkey)) {

node.nextNode = htemp.nextNode;

htemp.nextNode = node;

return head;

}

htemp = htemp.nextNode;

}

if(htemp == null)

System.out.println("未找到正确的插入位置!");

return head;

}

public CLType CLDeleteNode(CLType head,String key) {

CLType htemp,node;

htemp = head;

node = head;

while (htemp != null) {

if(! htemp.getNodeData().getKey().equals(key)) {

node = htemp;

htemp = htemp.getNextNode();

}else {

node.nextNode = htemp.nextNode;

htemp = null;

}

}

return head;

}

public void CLAllNode(CLType head) {

CLType htemp;

Data2 nodeData;

htemp = head;

while (htemp != null) {

System.out.println("编号" htemp.getNodeData().getKey() ",姓名" htemp.getNodeData().getName() ",年龄" htemp.getNodeData().getAge());

htemp = htemp.getNextNode();

}

}

}

class Data2 {

private String key;

private String name;

private int age;

public Data2() {}

public Data2(String key,String name,int age) {

this.key = key;

this.name = name;

this.age = age;

}

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

class CLType {

protected Data2 nodeData;

protected CLType nextNode;

public Data2 getNodeData() {

return nodeData;

}

public void setNodeData(Data2 nodeData) {

this.nodeData = nodeData;

}

public CLType getNextNode() {

return nextNode;

}

public void setNextNode(CLType nextNode) {

this.nextNode = nextNode;

}

}

class MainFunc {

public static void main(String[] args) {

Data2 data1 = new Data2("001","张三",18);

Data2 data2 = new Data2("002","李四",20);

Data2 data3 = new Data2("003","王五",21);

CLType cl1 = new CLType();

CLType cl2 = new CLType();

CLType cl3 = new CLType();

cl1.setNodeData(data1);

cl2.setNodeData(data2);

cl3.setNodeData(data3);

cl1.setNextNode(cl2);

cl2.setNextNode(cl3);

cl3.setNextNode(null);

LinkedList linkedList = new LinkedList();

Data2 testData = new Data2("111","刘双",39);

CLType head = linkedList.CLInsertNode(cl1,"002",testData);

head = linkedList.CLDeleteNode(head,"002");

linkedList.CLAllNode(head);

}

}

0 人点赞