您的位置:首页 > 其它

单链表与双链表队列

2015-11-12 19:46 197 查看
一,链表分类:单链表与双向链表;

二,链表定义:数据域(data)与指针域(node)的下一个节点(next),头节点(head),上一个节点(pre);

三,定义类;

(1)Node类:传值与重写get,set方法;

(2)LinkNodeList类;将对数据的增删差改的基本操作方法或属性;

(3)main();主函数类,进行数据的调用;

四,单链表的示例代码;

public class LinkListNode {
// 定义单链表的头接点;
private CreateNode head;
CreateNode pre;

// 添加
public void add(String data) {
CreateNode node = new CreateNode(data);
if (head == null) {
head = node;
} else {
// 赋值新节点为头节点
CreateNode newNode = head;
// 找到最后一个节点;
while (newNode.getNext() != null) {
newNode = newNode.getNext();
}
// 设置新节点为最后一个节点
newNode.setNext(node);
}
}

// 插入
public void insert(int index, String data) {
// 首先,判断是否参数正确
if (index < 0 || index >= size()) {
try {
throw new Exception("索引位置错误");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
CreateNode node = new CreateNode(data);
if (index == 0) {
node.setNext(head);
head = node;
return;
}

int count = 0;
CreateNode newNode = head;
// 找到下一个节点的位置
while (count != (index - 1)) {
newNode = newNode.getNext();
count++;

}
// 获取下一个节点的位置,给当前节点
CreateNode nextNode = newNode.getNext();
newNode.setNext(node);
node.setNext(nextNode);
}

}

public int size() {
// 定义一个计数器
int count = 0;
// 判断头节点是否为空
if (head == null) {
return count;
} else {
// 当前头节点为空
CreateNode newNode = head;
count++;
while (newNode.getNext() != null) {
newNode = newNode.getNext();
count++;

}
// 返回当前节点,为所有节点的数值
return count;
}

}

// 查找
public String get(int index) {
// 判断索引位置是否正确
if (index < 0 || index >= size()) {
try {
throw new Exception("索引位置错误");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
} else {
int count = 0;
CreateNode newNode = head;
// 找到最后一个节点
while (count != index) {
newNode = newNode.getNext();
count++;
}
// 返回数据域的值
return newNode.getData();
}
}

// 删除
public void delete(int index) {
if (index < 0 || index >= size()) {
try {
throw new Exception("索引位置错误");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (index == 0) {
// 当元素为0时,找头节点的下一个,并赋值给头节点
CreateNode newNode = head;
pre = newNode.getNext();
head = pre;
return;
}

int count = 0;
CreateNode newNode = head;
// 当前下标减1,继续找下一个下标
while (count != index - 1) {
newNode = newNode.getNext();
count++;
}
// 初始位置指向下一个值
CreateNode current = newNode.getNext();
// 下一个位置的初始位置
CreateNode next = current.getNext();
// 设置上一个位置的下一个的值
newNode.setNext(next);
}
return;
}

// 更新
public void updata(int index, String data) {

if (index < 0 || index >= size()) {
try {
throw new Exception("索引位置错误");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {

if (index + 1 == 0) {
// 当元素为0时,找头节点的下一个,并赋值给头节点
CreateNode newNode = head;
// pre = newNode.getNext();
head = newNode;
return;
}

int count = 0;
CreateNode newNode = head;
// 找到下一个节点的位置
while (count != (index)) {
newNode = newNode.getNext();
count++;

}
// 获取下一个节点的位置,给当前节点
newNode.setData(data);
return;
}

}
}


test()类

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkListNode t = new LinkListNode();

t.add("元素1");
t.add("元素2");
t.add("元素3");
t.add("元素4");
t.insert(0, "元素0");
t.updata(4, "e");
t.delete(0);

for (int i = 0; i < t.size(); i++) {
System.out.print("第" + i + "元素为:" + t.get(i) + "\n");

}

}


测试运行结果为;




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