算法最简单之_链表
2016-08-03 17:05
573 查看
node类:
(一)单链表
1)构造链表
2)链表头插入
3)链表头删除
只需要 first = first.next 即可
4)链表任意位置添加
添加位置前后断开,分别指向对应的引用即可
5)任意位置删除
6)链表遍历
7)根据位置和根据节点查找
(一)双链表
1)它是这样的
2)双链表整体鸟览
3)双链表添加
4)双链表删除
5)双链表代码
参考:
http://blog.csdn.net/tayanxunhua/article/details/11100097/ http://hiliangliang1130-126-com.iteye.com/blog/1144023 http://www.cnblogs.com/xilifeng/archive/2012/10/06/2713185.html http://blog.sina.com.cn/s/blog_7d44748b01013fsf.html
//节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 public Node( int data) { this. data = data; } //显示此节点 public void display() { System. out.print( data + " "); } }
(一)单链表
1)构造链表
2)链表头插入
// 插入一个头节点 public void addFirstNode( int data) { Node node = new Node(data); node. next = first; first = node; }
3)链表头删除
只需要 first = first.next 即可
// 删除一个头结点,并返回头结点 public Node deleteFirstNode() { Node tempNode = first; first = tempNode. next; return tempNode; }
4)链表任意位置添加
添加位置前后断开,分别指向对应的引用即可
// 在任意位置插入节点 在index的后面插入 public void add(int index, int data) { Node node = new Node(data); Node current = first; Node previous = first; while ( pos != index) { previous = current; current = current. next; pos++; } node. next = current; previous. next = node; pos = 0; }
5)任意位置删除
// 删除任意位置的节点 public Node deleteByPos( int index) { Node current = first; Node previous = first; while ( pos != index) { pos++; previous = current; current = current. next; } if(current == first) { first = first. next; } else { pos = 0; previous. next = current. next; } return current; }
6)链表遍历
// 显示出所有的节点信息 public void displayAllNodes() { Node current = first; while (current != null) { current.display(); current = current. next; } System.out.println(); }
7)根据位置和根据节点查找
// 根据位置查找节点信息 public Node findByPos( int index) { Node current = first; if ( pos != index) { current = current. next; pos++; } return current; } // 根据数据查找节点信息 public Node findByData( int data) { Node current = first; while (current. data != data) { if (current. next == null) return null; current = current. next; } return current; }
(一)双链表
1)它是这样的
2)双链表整体鸟览
3)双链表添加
4)双链表删除
5)双链表代码
/** * 双向链表 * @author yl * */ public class LinkNode { private Object obj; private LinkNode child; private LinkNode parent; public LinkNode(Object obj){ this.obj=obj; } //定义方法 public Object getObj(){ return obj; } public void setObj(Object obj){ this.obj=obj; } public LinkNode getChild(){ return child; } public void setChild(LinkNode child){ this.child=child; } public LinkNode getParent(){ return parent; } public void setParent(LinkNode parent){ this.parent=parent; } } 再来对这个链表进行测试: package cn.链表; /** * 双向链表的测试 * @author Administrator * */ public class LinkListTest { private static LinkNode front=null; private LinkNode last=front; public static void main(String args[]){ LinkListTest list=new LinkListTest(); list.add("头结点"); for(int i=0;i<10;i++){ list.add("结点"+i); } //测试指定位置下取得结点 Object obj=list.getLinkNode(3).getObj(); //测试在指定位置插入元素 list.add(3, "新来的元素"); System.out.println("<><><><><><><>"+obj); list.printLinkNode(front); System.out.println("<><><><><><><><><><><><><><><><><><><><><"); // list.remove(3); // list.printLinkNode(front); list.upDate(3, "值被改变的元素"); list.printLinkNode(front); System.out.println("<><><><><><><><><><><><><><><><><><><><><"); list.printLinkNode1(3); } /** * 在链表的后面插入元素 * @param obj */ public void add(Object obj){ //根据给定的值创建结点 LinkNode node=new LinkNode(obj); if(front==null){ //如果链表为空的时候 // front.setChild(node); // node.setParent(front); //第一个结点也即是最后一个结点 front=node; last=front; // System.out.println(front.getObj()); } else{ //新插入的结点为最后一个结点 last.setChild(node); node.setParent(last); last=node; } } /** * 在指定位置插入元素 * @param index * @param obj */ public void add(int index,Object obj){ //先创建结点 LinkNode node=new LinkNode(obj); //判断传入的下标 if(index<0||index>size()){ throw new RuntimeException("下标越界:size:"+size()+"index:"+index); }else{ //传入的下标符合要求 if(front==null){ //如果链表为空的时候 front=node; last=front; }else if(index==size()){ add(node); }else{ //链表不为空,取得当前下标的结点 LinkNode nownode=getLinkNode(index); //得到父结点 LinkNode fnode=nownode.getParent(); //重新定义新的引用关系 fnode.setChild(node); node.setParent(fnode); node.setChild(nownode); nownode.setParent(node); } } } /** * 根据下标,删除当前的结点 * @param index */ public void remove(int index){ if(index<0||index>size()){ throw new RuntimeException("下标越界:size:"+size()+"index:"+index); }else{ //传入的下标符合要求 if(front==null){ //如果链表为空的时候 System.out.println("链表为空,不能删除元素啦!!!"); } else{ //链表不为空,取得当前下标的结点 LinkNode nownode=getLinkNode(index); //得到父结点 LinkNode fnode=nownode.getParent(); //得到父结点 LinkNode cnode=nownode.getChild(); //重新定义新的引用关系 fnode.setChild(cnode); cnode.setParent(fnode); } } } /** * 根据下标取得当前的结点 * @param index 下标值 * @return */ public LinkNode getLinkNode(int index){ //判断传入的下标 if(index<0||index>size()){ throw new RuntimeException("下标越界:size:"+size()+"index:"+index); }else{ //先取得头结点 LinkNode node=front; int i=0; while(i<index){ i++; node=node.getChild(); } return node; } } /** * 在指定的位置,更新该结点,结点的值为obj * @param index * @param obj 更改的结点的值 */ public void upDate(int index,Object obj){ if(index<0||index>size()){ throw new RuntimeException("下标越界:size:"+size()+"index:"+index); }else{ //传入的下标符合要求 if(front==null){ //如果链表为空的时候 System.out.println("链表为空,不能更新元素啦!!!"); } else{ //链表不为空,取得当前下标的结点 LinkNode nownode=getLinkNode(index); //给结点重新赋值 nownode.setObj(obj); } } } /** * 得到链表的长度 * @return */ public int size(){ if(front==null){ //链表为空 return 0; }else{ //不为空 LinkNode node=front; int count=0; while(node!=null){ count++; node=node.getChild(); } return count; } } /** * 打印链表 * @param node 传入链表的头结点 */ public void printLinkNode(LinkNode node){ if(front==null){ System.out.println("此链表为空!!!"); }else{ //先取得头结点 LinkNode n=front; //遍历链表 while(n!=null){ Object obj=n.getObj(); System.out.println(obj); n=n.getChild(); } } } /** * 根据指定的位置来前后遍历 * @param index */ public void printLinkNode1(int index){ if(index<0||index>size()){ throw new RuntimeException("下标越界:size:"+size()+"index:"+index); } LinkNode nownode=getLinkNode(index); LinkNode cnode=nownode.getChild(); int i=index; //往前遍历 while(nownode!=null&&i>=0){ Object obj=nownode.getObj(); System.out.println(obj); i--; nownode=nownode.getParent(); } nownode=getLinkNode(index); //往后遍历 while(nownode!=null&&i<size()){ Object obj=nownode.getObj(); System.out.println(obj); i++; nownode=nownode.getChild(); } } }
参考:
http://blog.csdn.net/tayanxunhua/article/details/11100097/ http://hiliangliang1130-126-com.iteye.com/blog/1144023 http://www.cnblogs.com/xilifeng/archive/2012/10/06/2713185.html http://blog.sina.com.cn/s/blog_7d44748b01013fsf.html
相关文章推荐
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- 简单算法 - 找到链表的中间点和倒数第K个节点(普通方法和快慢指针方法开销一样)
- 【简单算法】24.回文链表
- 《Delphi 算法与数据结构》学习与感悟[6]: 一个简单的"单向链表"
- 【简单算法】23.反转链表
- 【简单算法】22.删除链表的倒数第N个节点
- 8---------单链表算法的简单应用和巩固
- 简单算法 - 链表是否有环-两链表是否相交
- (四)一个简单的删除链表中某个元素的算法
- 微软面试简单算法之 反序链表
- 【数据结构与算法】(二) c 语言链表的简单操作
- 简单算法 - 链表逆序思路详解
- 算法与数据结构基础7:C++双链表的简单实现
- leetcode-2. Add Two Numbers简单的java链表算法题 面试碰到了吗?
- 算法与数据结构【二】简单链表的实现
- 【简单算法】21.删除链表的结点
- 【简单算法】25. 环形链表
- 一些简单的链表算法二
- 【数据结构与算法】(二) c 语言链表的简单操作
- 【简单算法】23.合并两个有序链表