数据结构1_java---单链表的操作,约瑟夫问题
2024-08-29 01:54:22
我们经常实用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)最后是打印链表内容
package Main; import java.util.Scanner; /*链表操作*/
public class Main{
public static Node head; //建立头结点
class Node{ //内部类Node用于建立结点
public int data; //数据域
public Node next; //指针域
public Node()
{
super();
}
public Node(int data) //初始化数据域
{
this.data = data;
}
}
public Main() //通过建立对象初始化头结点,也可直接在main函数中建立头结点
{
head = new Node();
}
public Main(int n)
{
this();
create(n); //创建带有N个结点的链表
}
//建立链表
public void create(int n)
{
Node init = new Node();
init = head; //新建一个结点指向头结点
Scanner aScanner = new Scanner(System.in);
int data;
while(n!=0)
{
data = aScanner.nextInt();
Node pNode = new Node(data);
init.next = pNode; //将当前结点指向新建的结点
init = init.next; //结点右移
n--;
}
}
//删除结点
public void delete(int i)
{
Node pNode = new Node();
pNode = head;
i--;
while(pNode.next!=null&&i!=0) //遍历找到结点i
{
pNode = pNode.next;
i--;
}
pNode.next = pNode.next.next; //直接将该节点跨过
}
//插入结点
public void insert(int i,int m)
{
Node pNode = new Node();
pNode = head;
i--;
while(pNode.next!=null&&i!=0)
{
pNode = pNode.next;
i--;
}
Node aNode = new Node(m);
aNode.next = pNode.next;
pNode.next = aNode;
}
/*寻找结点*/
//按位置寻找
public int find(int m)
{
Node pNode = new Node();
pNode = head;
while(pNode.next!=null&&m!=0)
{
pNode = pNode.next;
m--;
}
return pNode.data;
}
//按值寻找
public int indexof(int k)
{
Node pNode = new Node();
pNode = head;
int location=0;
while(pNode.next!=null)
{
pNode = pNode.next;
location++;
if(pNode.data==k) //判断,若是则返回位置,否则返回-1
return location;
}
return -1;
}
public void print() //打印链表
{
Node print = new Node();
print = head;
while(print.next!=null)
{
print = print.next;
System.out.print(print.data+" ");
}
System.out.println();
}
public static void main(String[] args) {
Main aMain = new Main(5); // 建立一个带有5结点的链表
aMain.insert(4, 100); //在位置4处插入数字100
int findresult_1 = aMain.find(2); //寻找位置2处的数据
int findresult_2 = aMain.indexof(35); //寻找数据35所处的结点位置
aMain.print();
System.out.println("第2个结点的数据查询为:"+findresult_1);
if(findresult_2==-1)
{
System.out.println("未检测到要查询的数据");
}else {
System.out.println("数据35的查询位置为:"+findresult_2);
}
System.out.println("删除后的链表为:");
aMain.delete(5); //删除位置5处的结点
aMain.print();
}
}
算法,可直接插入其中,修改main即可!!
public void algorithm(int m)
{
Node pNode = new Node();
pNode = head;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
pNode = pNode.next;
while(pNode.data==0)
{
pNode = pNode.next;
}
}
System.out.println(pNode.data);
pNode.data=0;
}
}
最新文章
- objective-c static变量的使用总结
- Python基础、collections补充
- Twisted 阐述
- iframe 中嵌套刷新
- In Action(最短路+01背包)
- PHP和MySQL Web开发 原书第4版 高清文字版,有目录,附带源码
- jsp的九大天王
- raise
- Eureka的服务注册与发现概念(三)
- 初学Python——装饰器
- 设计模式C++学习笔记之一(Strategy策略模式)
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十八):注册中心(Spring Cloud Consul)
- 线程池和进程池的通用写法 ProcessPoolExecutor 和 ThreadPoolExecutor
- scala 稀疏向量
- 《Python数据分析》-Ch01 Python 程序库入门
- Jmeter--JDBC请求(sqlserver)
- 执行时的C程序
- Java中ArrayList和LinkedList区别、ArrayList和Vector的区别
- CC33:碰撞的蚂蚁
- Linux - 常用命令索引
热门文章
- Linux 笔记 - 第十章 Shell 基础知识
- Day 24 定时任务
- AtCoder从小白到大神的进阶攻略
- tomcat启动出现乱码
- (七十二)c#Winform自定义控件-雷达图
- IDEA加密算法(含所需jar包(commons-codec-1.11.jar ,bcprov-jdk15on-160.jar))
- Excel自定义格式参数
- The type java.lang.AutoCloseable cannot be resolved. It is indirectly referenced from required .class files
- C# 反射Reflection——反射反射程序员的快乐
- 死磕 java同步系列之Phaser源码解析