資料結構複習--java實現單鏈表基本操作

來源:互聯網
上載者:User

標籤:單鏈表   資料結構   

單鏈表的基本操作包括建立單鏈表、尋找運算(按序尋找和按值尋找)、插入運算(前插和後插)和刪除運算。下面給出具體的java實現程式:

package com.zpp.test;//首先建立一個節點類public class Node {     private Node next; //指標域     private int data;//資料域          public Node( int data) {           this. data = data;     }       }package com.zpp.test;public class LinkList {     public Node first; // 定義一個前端節點     private int pos = 0;// 節點的位置     public LinkList() {           this. first = null;     }     // 插入一個前端節點     public void addFirstNode( int data) {          Node node = new Node(data);          node. next = first;           first = node;     }     // 刪除前端節點,並返回前端節點     public Node deleteFirstNode() {          Node tempNode = first;           first = tempNode. next;           return tempNode;     }     // 在指定位置index的後面插入節點      public void insertAfter(int index, int data) {          Node node = new Node(data);          Node current = first;           while ( pos != index) {              current = current. next;               pos++;          }          node. next = current.next;          current. next = node;           pos = 0;     }     // 在指定位置index的前面插入節點      public void insertBefore(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;     }            // 刪除指定位置的節點     public void 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;          }     }     // 根據節點的data刪除節點(僅僅刪除第一個)     public void deleteByData( int data) {          Node current = first;          Node previous = first; //記住上一個節點           while (current. data != data) {               if (current. next == null) {              System.out.println("沒有找到對應節點,無法進行刪除操作!");              }              previous = current;              current = current. next;          }           if(current == first) {               first = first. next;          } else {              previous. next = current. next;          }     }     // 根據位置尋找節點資訊     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;     }       // 顯示出所有的節點資訊     public void displayAllNodes() {          Node current = first;           while (current != null) {              System.out.print(current.data+" ");                            current = current. next;          }           System.out.println();     }}package com.zpp.test;//測試類別public class TestLinkList {     public static void main(String[] args) {          LinkList linkList = new LinkList();          linkList.addFirstNode(20);          linkList.addFirstNode(21);          linkList.addFirstNode(19);           //19,21,20          linkList.insertBefore(1, 22); //19,22,21,20          linkList.insertBefore(2, 23); //19,22,23,21,20          linkList.insertAfter(3, 99); //19,22,23,21,99,20          linkList.displayAllNodes();//        Node node = linkList.deleteFirstNode();//        System.out.println("node : " + node.data);//        linkList.displayAllNodes();//        node = linkList.deleteByPos(2);//        System.out.println("node : " + node.data);//        linkList.displayAllNodes();//        linkList.deleteFirstNode();          linkList.deleteByData(19);//        Node node = linkList.deleteByPos(0);          //System. out.println( "node : " + node. data);          linkList.displayAllNodes();          Node node1 = linkList.findByPos(0);          System. out.println( "node1: " + node1. data);          Node node2 = linkList.findByData(22);          System. out.println( "node2: " + node2. data);     }}




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

資料結構複習--java實現單鏈表基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.