数据结构1_java---单链表的操作,约瑟夫问题
生活随笔
收集整理的這篇文章主要介紹了
数据结构1_java---单链表的操作,约瑟夫问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們經常實用c++來建立鏈表,為了學習的方便,此處我使用java實現了對鏈表的增刪改查功能
整個過程較為簡單。僅供參考
流程:
(1)通過內部類Node建立結點,內部變量作為指針域和數據域,并寫下構造函數
(2)通過建立對象初始化頭結點,也可直接在main函數中建立頭結點,創建帶有N個結點的鏈表
(3)建立鏈表的函數為public void create(int n),帶有n個結點
(4)刪除結點函數?public void delete(int i)
(5)插入結點函數?public void insert(int i,int m)
(6)尋找結點函數有兩類,一個是通過位置尋找,一個是通過數值尋找
(7)最后是打印鏈表內容
1 package Main; 2 3 4 import java.util.Scanner; 5 6 /*鏈表操作*/ 7 public class Main{ 8 public static Node head; //建立頭結點 9 class Node{ //內部類Node用于建立結點 10 public int data; //數據域 11 public Node next; //指針域 12 public Node() 13 { 14 super(); 15 } 16 public Node(int data) //初始化數據域 17 { 18 this.data = data; 19 } 20 } 21 public Main() //通過建立對象初始化頭結點,也可直接在main函數中建立頭結點 22 { 23 head = new Node(); 24 } 25 public Main(int n) 26 { 27 this(); 28 create(n); //創建帶有N個結點的鏈表 29 } 30 //建立鏈表 31 public void create(int n) 32 { 33 Node init = new Node(); 34 init = head; //新建一個結點指向頭結點 35 Scanner aScanner = new Scanner(System.in); 36 int data; 37 while(n!=0) 38 { 39 data = aScanner.nextInt(); 40 Node pNode = new Node(data); 41 init.next = pNode; //將當前結點指向新建的結點 42 init = init.next; //結點右移 43 n--; 44 } 45 } 46 //刪除結點 47 public void delete(int i) 48 { 49 Node pNode = new Node(); 50 pNode = head; 51 i--; 52 while(pNode.next!=null&&i!=0) //遍歷找到結點i 53 { 54 pNode = pNode.next; 55 i--; 56 } 57 pNode.next = pNode.next.next; //直接將該節點跨過 58 } 59 //插入結點 60 public void insert(int i,int m) 61 { 62 Node pNode = new Node(); 63 pNode = head; 64 i--; 65 while(pNode.next!=null&&i!=0) 66 { 67 pNode = pNode.next; 68 i--; 69 } 70 Node aNode = new Node(m); 71 aNode.next = pNode.next; 72 pNode.next = aNode; 73 } 74 /*尋找結點*/ 75 //按位置尋找 76 public int find(int m) 77 { 78 Node pNode = new Node(); 79 pNode = head; 80 while(pNode.next!=null&&m!=0) 81 { 82 pNode = pNode.next; 83 m--; 84 } 85 return pNode.data; 86 } 87 //按值尋找 88 public int indexof(int k) 89 { 90 Node pNode = new Node(); 91 pNode = head; 92 int location=0; 93 while(pNode.next!=null) 94 { 95 pNode = pNode.next; 96 location++; 97 if(pNode.data==k) //判斷,若是則返回位置,否則返回-1 98 return location; 99 } 100 return -1; 101 } 102 public void print() //打印鏈表 103 { 104 Node print = new Node(); 105 print = head; 106 while(print.next!=null) 107 { 108 print = print.next; 109 System.out.print(print.data+" "); 110 } 111 System.out.println(); 112 } 113 public static void main(String[] args) { 114 Main aMain = new Main(5); // 建立一個帶有5結點的鏈表 115 aMain.insert(4, 100); //在位置4處插入數字100 116 int findresult_1 = aMain.find(2); //尋找位置2處的數據 117 int findresult_2 = aMain.indexof(35); //尋找數據35所處的結點位置 118 aMain.print(); 119 System.out.println("第2個結點的數據查詢為:"+findresult_1); 120 if(findresult_2==-1) 121 { 122 System.out.println("未檢測到要查詢的數據"); 123 }else { 124 System.out.println("數據35的查詢位置為:"+findresult_2); 125 } 126 System.out.println("刪除后的鏈表為:"); 127 aMain.delete(5); //刪除位置5處的結點 128 aMain.print(); 129 } 130 }?算法,可直接插入其中,修改main即可!!
1 public void algorithm(int m) 2 { 3 Node pNode = new Node(); 4 pNode = head; 5 for(int i=0;i<n;i++) 6 { 7 for(int j=0;j<m;j++) 8 { 9 pNode = pNode.next; 10 while(pNode.data==0) 11 { 12 pNode = pNode.next; 13 } 14 } 15 System.out.println(pNode.data); 16 pNode.data=0; 17 } 18 }?
轉載于:https://www.cnblogs.com/liuhui5599/p/8617799.html
總結
以上是生活随笔為你收集整理的数据结构1_java---单链表的操作,约瑟夫问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安全篇:弱密码python检测工具
- 下一篇: java学习记录--ThreadLoca