数据结构④-链表
来源:互联网 发布:jsp写java代码 编辑:程序博客网 时间:2024/06/08 14:54
链表最大优势是插入删除比较方便
其实链表也是一种灵活的数据结构,没有想象的那么复杂
public class LinkList<T> {private Node first;private LinkList(){first = null;}class Node<T>{T data;Node next;public Node(){}public Node(T t){this.data = t;}public void display(){System.out.print(" " + data);}}boolean isEmpty(){return null == first;}//头插法public void insertFirst(T t){Node node = new Node(t);node.next = first;first = node;}public Node deleteFirst(){Node node = first;first = first.next;return node;}//查找public Node find(T t){Node current = first;while(current.data != t){if(null == current)return null;current = current.next;}return current;}public void display(){if(isEmpty()){System.out.println("linkList is null.");return;}Node current = null;current = first;while(null != current){current.display();current = current.next;}}/** * 逆序输出 面试题 * 从尾到头打印链表 使用递归(优雅) * 递归的原理,其实就是一个栈(stack) “逐层递进”与“逐层回归”的过程! * @param head */public void reversePrint(Node head){if(null == head){return;}reversePrint(head.next);System.out.print(head.data + " ");}//插入一个节点,插到指定节点的后边public boolean insert(T t, Node newNode){boolean flag = false;LinkList.Node node = this.find(t);if(null != node){if(null != node.next){newNode.next = node.next;}node.next = newNode;flag = true;}return flag;}//删除指定节点public boolean delete(T t){boolean flag = false;LinkList.Node current = first;LinkList.Node prev = first;while(null != current && current.data != t){prev = current;current = current.next;}if(null == current){System.out.println("Not find target node.");return false;}if(first == current){first = current.next;}else if(null != current.next){//如果目标后继节点不为空prev.next = current.next;}else{prev.next = null;}flag = true;return flag;}public static void main(String[] args){LinkList list = new LinkList();list.insertFirst(22);list.insertFirst(33);list.insertFirst(55);list.insertFirst(66);list.insertFirst(88);//list.deleteFirst();//list.delete(55);list.display();System.out.println("");System.out.println("reverse List-->");list.reversePrint(list.first);}}
【双向链表-2016】==============================================================================
package com.dataStruct.demo;public class DLinkList {private DLinkNode first;private DLinkNode last;public boolean isEmpty(){if(null == first)return true;return false;}public void insertFirst(int id, String name){DLinkNode dLinkNode = new DLinkNode(id, name);if(isEmpty()){last = dLinkNode;}else{first.prev = dLinkNode;}dLinkNode.next = first;first = dLinkNode;}public void insertLast(int id, String name){DLinkNode dLinkNode = new DLinkNode(id, name);if(isEmpty()){first = dLinkNode;}else{last.next = dLinkNode;dLinkNode.prev = last;}last = dLinkNode;}public void delFirst(){if(isEmpty())return;if(first == last){first = null;last = null;}else{first = first.next;first.next.prev = null;}}public void delLast(){if(isEmpty())return;if(first == last){first = null;last = null;}else{last = last.prev;last.next = null;}}public boolean delete(int id){if(isEmpty()){System.out.println("DLinkNode is null!");return false;}DLinkNode current = first;while(null != current){if(current.id == id){if(first == last){first = null;last = null;}if(current == first){first = first.next;}else if(current == last){last = current.prev;current.prev.next = null;}else{current.prev.next = current.next;current.next.prev = current.prev;}return true;}current = current.next;}return false;}public void display(){DLinkNode current = first;if(isEmpty()){System.out.println("DLinkList is empty!!");return;}if(first == last){first.display();return;}while(null != current){current.display();current = current.next;}}public static void main(String[] args){DLinkList dl = new DLinkList();dl.insertFirst(1, "ZhangSan");dl.insertLast(2, "LiSi");dl.insertLast(3, "WangWu");//dl.delete(2);//dl.delFirst();dl.delLast();dl.display();}}
0 0
- 数据结构④-链表
- 数据结构---链表
- 数据结构 - 链表
- 【数据结构】链表
- 数据结构-链表
- '数据结构' 链表
- 数据结构-链表
- 【数据结构】链表
- 数据结构--链表
- 【数据结构】 链表
- 数据结构--链表
- 数据结构-链表
- 数据结构 - 链表
- 链表 - 数据结构
- 数据结构:链表
- [数据结构]链表
- 数据结构--链表
- 【数据结构】链表
- [推荐] VC与我的这四年[奇迹冬瓜] [问题点数:100分,结帖人runner2012] ——有感
- js:prototype
- 用OpenInventor实现的NeHe OpenGL教程-第六课
- 通过指定关键字在手册页中搜索信息
- 【Git笔记】运行man git时报错"No manual entry for git"的解决方法
- 数据结构④-链表
- 服务提供者框架(Service Provider Framework)
- iOS 富文本类库RTLabel
- 复习题目
- 【Android进阶学习】监听EditText的变化
- java中class.forName和classLoader加载类的区分
- OpenGL ES 纹理阵列的实现
- 千元手机比较
- 采用回调Activity的形式创建AlertDialog单选列表框