您的位置:首页 > 理论基础 > 数据结构算法

JAVA数据结构---单链表

2016-03-05 21:55 288 查看
节点类:

package 自定义链表;

public class Node {

protected int data;
protected Node nextNode;

public Node(int data) {
super();
this.data = data;
}

public void printData() {
System.out.print(data + " ");
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public Node getNextNode() {
return nextNode;
}

public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}


}

链表类:

package 自定义链表;

public class LinkList {

private Node head;
private int index = 0;
private int length = 0;

// 初始化链表为null
public LinkList() {
this.head = null;
}

// 链表的长度
public int length() {
return length;
}

// 打印链表的元素
public void printAllNode() {
Node newnode = head;
while (newnode != null) {
newnode.printData();
System.out.print(" ");
newnode = newnode.nextNode;
}
System.out.println();
}

// 设置链表的头部(前追加)
public void setHead(int x) {
Node newnode = new Node(x);
newnode.nextNode = head;
head = newnode;
length++;
}

// 追加到链表
@SuppressWarnings("unused")
public void append(int x) {
Node newnode = new Node(x);
Node node_past = head;
Node node_now = head;
while (node_now.nextNode != null) {
node_past = node_now;
node_now = node_now.nextNode;
}
node_now.nextNode = newnode;
newnode.nextNode = null;
length++;
}

// 插入操作,除了第一个位置外的位置
public void insert(int position, int x) throws Exception {
if (position < 0 || position > length) {
throw new Exception("插入位置错误.");
}
Node newnode = new Node(x);
Node node_past = head;
Node node_now = head;
while (index != position) {
node_past = node_now;
node_now = node_now.nextNode;
index++;
}
if (index == 0) {
newnode.nextNode = head.nextNode;
head = newnode;
} else {
node_past.nextNode = newnode;
newnode.nextNode = node_now;
}
index = 0;
length++;
}

// 删除头部
public void deleteHead() {
head = head.nextNode;
length--;
}

// 删除尾部
public void deleteRear() {
Node node_temp = head;
while (index != length - 2) {
node_temp = node_temp.nextNode;
index++;
}
index = 0;
node_temp.nextNode = null;
length--;
}

// 删除byindex
@SuppressWarnings("unused")
public void deleteByIndex(int position) throws Exception {
if (position < 0 || position > length) {
throw new Exception("插入位置错误.");
}
Node newnode = head;
Node node_past = head;
Node node_now = head;
while (index != position) {
index++;
node_past = node_now;
node_now = node_now.nextNode;
}
if (node_now == head) {
head = head.nextNode;
} else if (node_now == null) {
node_past.nextNode = null;
} else {
node_past.nextNode = node_now.nextNode;
index = 0;
}
length--;
}

// 改变byindex
public void changeByIndex(int position, int x) throws Exception {
if (position < 0 || position > length) {
throw new Exception("插入位置错误.");
}
Node node_temp = head
4000
;
while (index != position) {
node_temp = node_temp.nextNode;
index++;
}
index = 0;
node_temp.setData(x);
}

// 查询findbyData
public int findByData(int x) {
int ret = 0;
Node node_temp = head;
while (node_temp.getData() != x) {
node_temp = node_temp.nextNode;
index++;
}
ret = index;
index = 0;
return ret;
}

// 查询getbyIndex
public int getByIndex(int position) {
Node node_temp = head;
while (index != position) {
node_temp = node_temp.nextNode;
index++;
}
index = 0;
return node_temp.getData();
}


}

测试类:

package 自定义链表;

public class Test {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

LinkList newList = new LinkList();

newList.setHead(100);
newList.setHead(1000);
newList.append(10);
newList.append(20);
newList.insert(2, 999);
newList.insert(1, 3434444);

newList.printAllNode();
System.out.println(newList.length());

newList.insert(0, 999999);
newList.printAllNode();
System.out.println(newList.length());

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: