码迷,mamicode.com
首页 > 编程语言 > 详细

java数据结构和算法------线性表(链表结构)

时间:2015-07-10 12:56:47      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

  1 package iYou.neugle.list;
  2 
  3 public class MyChainList<T> {
  4     // 向链表尾部添加元素
  5     public Node<T> AddEnd(Node<T> head, T data) {
  6         Node<T> node = new Node<T>();
  7         node.data = data;
  8         if (head == null) {
  9             head = node;
 10             return head;
 11         }
 12         SearchEnd(head).next = node;
 13         return head;
 14     }
 15 
 16     private Node<T> SearchEnd(Node<T> head) {
 17         if (head.next == null) {
 18             return head;
 19         }
 20         return SearchEnd(head.next);
 21     }
 22 
 23     // 向链表头部添加元素
 24     public Node<T> AddHead(Node<T> head, T data) {
 25         Node<T> node = new Node<T>();
 26         node.data = data;
 27         Node<T> nodeCopy = DeepCopy(head, new Node<T>());
 28         node.next = nodeCopy;
 29 
 30         head.data = node.data;
 31         head.next = node.next;
 32         return head;
 33     }
 34 
 35     private Node<T> DeepCopy(Node<T> head, Node<T> node) {
 36         if (head != null) {
 37             node.data = head.data;
 38             node.next = head.next;
 39         } else {
 40             return null;
 41         }
 42         DeepCopy(head.next, node.next);
 43         return node;
 44     }
 45 
 46     // 向链表中插入数据
 47     public Node<T> Insert(Node<T> head, T key, T data) {
 48         if (head.next == null) {
 49             return null;
 50         }
 51 
 52         if (head.data.equals(key)) {
 53             return AddHead(head, data);
 54         }
 55 
 56         if (head.next.data.equals(key)) {
 57             Node<T> node = new Node<T>();
 58             node.data = data;
 59             node.next = head.next;
 60             head.next = node;
 61             return head;
 62         }
 63         Insert(head.next, key, data);
 64         return head;
 65     }
 66 
 67     // 向链表中删除数据
 68     public Node<T> Delete(Node<T> head, T key) {
 69         if (head.next == null) {
 70             return null;
 71         }
 72 
 73         if (head.data.equals(key)) {
 74             head.data = head.next.data;
 75             head.next = head.next.next;
 76             return head;
 77         }
 78 
 79         if (head.next.data.equals(key)) {
 80             head.next = head.next.next;
 81             return head;
 82         }
 83         Delete(head.next, key);
 84         return head;
 85     }
 86 
 87     // 按关键字查找节点
 88     public void Search(Node<T> head, T key) {
 89         if (head.data.equals(key)) {
 90             System.out.println("查询节点为头结点,节点值为:" + key);
 91             return;
 92         }
 93         while (head.next != null) {
 94             if (head.next.data.equals(key)) {
 95                 System.out.print("查询节点为:" + key + ",");
 96                 System.out.print("查询节点前置节点为:" + head.data + ",");
 97                 if (head.next.next != null) {
 98                     System.out.println("查询节点后置节点为:" + head.next.next.data);
 99                 } else {
100                     System.out.println("查询节点为尾节点!");
101                 }
102                 return;
103             }
104             head = head.next;
105         }
106         System.out.println("没有该节点!");
107     }
108 
109     // 获取链表长度
110     public int SizeOf(Node<T> head) {
111         // 如果没有节点,返回-1
112         if (head == null) {
113             return -1;
114         }
115         int n = 1;
116         while (head.next != null) {
117             n++;
118             head = head.next;
119         }
120         return n;
121     }
122 
123     public void Print(Node<T> head) {
124         while (head != null) {
125             System.out.println(head.data);
126             head = head.next;
127         }
128     }
129 }

 

java数据结构和算法------线性表(链表结构)

标签:

原文地址:http://www.cnblogs.com/niuxiaoha/p/4635236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!