//这里假设已经对原始数据进行了排序 public class LinkListStruct { //模拟数据 int[] datas = {0,10,300,600,0}; //记录每个元素的后继元素的位置数组 int[] postions = new int[6]; int k=1; int position = -1; int tmp = 0; int flag = 0; int val = 0; //循环遍历结果数组 public void traverse(){ if (k == 1 && position != 1){ System.out.println("输出" + datas[1]); } if (k == 1 && position == 1){ System.out.println("输出"+datas[4]); System.out.println("输出"+datas[postions[4]]); } if (postions[k] != 0) { System.out.println("输出"+datas[postions[k]]); k = postions[k]; traverse(); } } public void Main() { datas[4] = 400; for(int i = 0; i < datas.length; i++){ if (i <= datas.length-3 && i != 0){ postions[i] = i+1; } if (datas[4] < datas[i] && flag == 0){ position = i; flag = 1; } } if (position != -1) { if (position == 1){ postions[4] = 1; traverse(); } else { tmp = postions[position - 1]; postions[position - 1] = 4; postions[4] = tmp; traverse(); } } } public static void main(String[] args) { LinkListStruct struct = new LinkListStruct(); struct.Main(); } }
啊哈算法!之数组模拟链表插入数据练习
最新推荐文章于 2022-11-09 00:37:50 发布