我们经常实用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;
}
}

最新文章

  1. objective-c static变量的使用总结
  2. Python基础、collections补充
  3. Twisted 阐述
  4. iframe 中嵌套刷新
  5. In Action(最短路+01背包)
  6. PHP和MySQL Web开发 原书第4版 高清文字版,有目录,附带源码
  7. jsp的九大天王
  8. raise
  9. Eureka的服务注册与发现概念(三)
  10. 初学Python——装饰器
  11. 设计模式C++学习笔记之一(Strategy策略模式)
  12. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十八):注册中心(Spring Cloud Consul)
  13. 线程池和进程池的通用写法 ProcessPoolExecutor 和 ThreadPoolExecutor
  14. scala 稀疏向量
  15. 《Python数据分析》-Ch01 Python 程序库入门
  16. Jmeter--JDBC请求(sqlserver)
  17. 执行时的C程序
  18. Java中ArrayList和LinkedList区别、ArrayList和Vector的区别
  19. CC33:碰撞的蚂蚁
  20. Linux - 常用命令索引

热门文章

  1. Linux 笔记 - 第十章 Shell 基础知识
  2. Day 24 定时任务
  3. AtCoder从小白到大神的进阶攻略
  4. tomcat启动出现乱码
  5. (七十二)c#Winform自定义控件-雷达图
  6. IDEA加密算法(含所需jar包(commons-codec-1.11.jar ,bcprov-jdk15on-160.jar))
  7. Excel自定义格式参数
  8. The type java.lang.AutoCloseable cannot be resolved. It is indirectly referenced from required .class files
  9. C# 反射Reflection——反射反射程序员的快乐
  10. 死磕 java同步系列之Phaser源码解析