单链表与双链表队列
2015-11-12 19:46
197 查看
一,链表分类:单链表与双向链表;
二,链表定义:数据域(data)与指针域(node)的下一个节点(next),头节点(head),上一个节点(pre);
三,定义类;
(1)Node类:传值与重写get,set方法;
(2)LinkNodeList类;将对数据的增删差改的基本操作方法或属性;
(3)main();主函数类,进行数据的调用;
四,单链表的示例代码;
test()类
测试运行结果为;
二,链表定义:数据域(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"); } }
测试运行结果为;
相关文章推荐
- emoji表情引发的JNI崩溃
- 【BZOJ】4130: [PA2011]Kangaroos【KD树——最长连续1的子段长度】
- nyoj 阶乘的0 84 (简单数学转换)
- MySql 数据库编程 笔记
- java程序员第一课 html 网页标签
- 工厂设计模式
- Codeforces Testing Round #12 B. Restaurant 贪心
- 浅析JQuery中的html(),text(),val()区别
- Java [leetcode 33]Search in Rotated Sorted Array
- Openwrt 3G模块
- vmware安装增强功能
- swift存储策略实践
- 转载:java面试题(一)
- Apache-POI读取Excel2003和Excel2007中数据。
- tinyhttpd-0.1.0源码分析
- 移动端图片格式调研
- Android基础入门教程——10.8 LayoutInflater(布局服务)
- 32位linux系统调用
- OC14_OC结业考试错题集
- 鸟哥的LINUX私房菜基础篇第三版 阅读笔记 三 Linux磁盘与文件系统管理