• 数据结构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     }
  • 相关阅读:
    产品化软件开发与项目化软件开发的对比
    4.ThinkPHP 3.1.2 输出和模型使用
    ThinkPHP 3.1.2 输出和模型使用1
    事务管理配置与@Transactional注解使用
    logstash 区分多个文件index端配置
    logstash 读取多个系统相同文件shipper端
    centos 6.5安装git
    如何查看PHP的配置信息
    MVC模式和URL访问
    1.环境搭建
  • 原文地址:https://www.cnblogs.com/liuhui5599/p/8617799.html
Copyright © 2020-2023  润新知