【数据结构和算法】用java简单的实现单链表的基本操作

来源:互联网 发布:优美的句子 知乎 编辑:程序博客网 时间:2024/04/28 08:11

[java] view plain copy
  1. package com.tyxh.link;  
  2. //节点类  
  3. public class Node {  
  4.      protected Node next; //指针域  
  5.      protected int data;//数据域  
  6.        
  7.      public Node( int data) {  
  8.            this. data = data;  
  9.      }  
  10.        
  11.      //显示此节点  
  12.      public void display() {  
  13.           System. out.print( data + " ");  
  14.      }  
  15. }  
  16.   
  17. package com.tyxh.link;  
  18. //单链表  
  19. public class LinkList {  
  20.      public Node first; // 定义一个头结点  
  21.      private int pos = 0;// 节点的位置  
  22.   
  23.      public LinkList() {  
  24.            this. first = null;  
  25.      }  
  26.   
  27.      // 插入一个头节点  
  28.      public void addFirstNode( int data) {  
  29.           Node node = new Node(data);  
  30.           node. next = first;  
  31.            first = node;  
  32.      }  
  33.   
  34.      // 删除一个头结点,并返回头结点  
  35.      public Node deleteFirstNode() {  
  36.           Node tempNode = first;  
  37.            first = tempNode. next;  
  38.            return tempNode;  
  39.      }  
  40.   
  41.      // 在任意位置插入节点 在index的后面插入  
  42.      public void add(int index, int data) {  
  43.           Node node = new Node(data);  
  44.           Node current = first;  
  45.           Node previous = first;  
  46.            while ( pos != index) {  
  47.               previous = current;  
  48.               current = current. next;  
  49.                pos++;  
  50.           }  
  51.           node. next = current;  
  52.           previous. next = node;  
  53.            pos = 0;  
  54.      }  
  55.   
  56.      // 删除任意位置的节点  
  57.      public Node deleteByPos( int index) {  
  58.           Node current = first;  
  59.           Node previous = first;  
  60.            while ( pos != index) {  
  61.                pos++;  
  62.               previous = current;  
  63.               current = current. next;  
  64.           }  
  65.            if(current == first) {  
  66.                first = first. next;  
  67.           } else {  
  68.                pos = 0;  
  69.               previous. next = current. next;  
  70.           }  
  71.            return current;  
  72.      }  
  73.   
  74.      // 根据节点的data删除节点(仅仅删除第一个)  
  75.      public Node deleteByData( int data) {  
  76.           Node current = first;  
  77.           Node previous = first; //记住上一个节点  
  78.            while (current. data != data) {  
  79.                if (current. next == null) {  
  80.                     return null;  
  81.               }  
  82.               previous = current;  
  83.               current = current. next;  
  84.           }  
  85.            if(current == first) {  
  86.                first = first. next;  
  87.           } else {  
  88.               previous. next = current. next;  
  89.           }  
  90.            return current;  
  91.      }  
  92.   
  93.      // 显示出所有的节点信息  
  94.      public void displayAllNodes() {  
  95.           Node current = first;  
  96.            while (current != null) {  
  97.               current.display();  
  98.               current = current. next;  
  99.           }  
  100.           System. out.println();  
  101.      }  
  102.   
  103.      // 根据位置查找节点信息  
  104.      public Node findByPos( int index) {  
  105.           Node current = first;  
  106.            if ( pos != index) {  
  107.               current = current. next;  
  108.                pos++;  
  109.           }  
  110.            return current;  
  111.      }  
  112.   
  113.      // 根据数据查找节点信息  
  114.      public Node findByData( int data) {  
  115.           Node current = first;  
  116.            while (current. data != data) {  
  117.                if (current. next == null)  
  118.                     return null;  
  119.               current = current. next;  
  120.           }  
  121.            return current;  
  122.      }  
  123. }  
  124.   
  125. package com.tyxh.link;  
  126. //测试类  
  127. public class TestLinkList {  
  128.      public static void main(String[] args) {  
  129.           LinkList linkList = new LinkList();  
  130.           linkList.addFirstNode(20);  
  131.           linkList.addFirstNode(21);  
  132.           linkList.addFirstNode(19);  
  133.            //19,21,20  
  134.           linkList.add(122); //19,22,21,20  
  135.           linkList.add(223); //19,22,23,21,20  
  136.           linkList.add(399); //19,22,23,99,21,20  
  137.           linkList.displayAllNodes();  
  138. //        Node node = linkList.deleteFirstNode();  
  139. //        System.out.println("node : " + node.data);  
  140. //        linkList.displayAllNodes();  
  141. //        node = linkList.deleteByPos(2);  
  142. //        System.out.println("node : " + node.data);  
  143. //        linkList.displayAllNodes();  
  144. //        linkList.deleteFirstNode();  
  145.           Node node = linkList.deleteByData(19);  
  146. //        Node node = linkList.deleteByPos(0);  
  147.           System. out.println( "node : " + node. data);  
  148.           linkList.displayAllNodes();  
  149.           Node node1 = linkList.findByPos(0);  
  150.           System. out.println( "node1: " + node1. data);  
  151.           Node node2 = linkList.findByData(22);  
  152.           System. out.println( "node2: " + node2. data);  
  153.      }  
  154. }  
  155. 引用地址:http://blog.csdn.net/tayanxunhua/article/details/11100097/

0 0
原创粉丝点击