单链表的增删改查
最主要是为了保存一下成果,有错误的话希望大家及时留言
package com.list;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
List list = new List();
ListNode node = new ListNode(1);
ListNode node1 = new ListNode(2);
ListNode node2 = new ListNode(8);
ListNode node3 = new ListNode(5);
ListNode node4 = new ListNode(6);
list.add_order(node);
list.add_order(node1);
list.add_order(node2);
list.add_order(node3);
list.add_order(node4);
list.showList();
System.out.println(list.sums());
// System.out.println();
// System.out.println(list.sum());//用于统计链表中结点的个数(不包括头结点)
list.Two_reverse();//先将list链表进行反转
}
}
class ListNode {
public int data;//字节的数字域存储
public ListNode next;//指针域指向下一个结点
public ListNode() {
}
public ListNode(int data) {
this.data = data;
}
}
class List//创建链表
{
public ListNode head;//定义一个头结点
public List() {
head = new ListNode(0);
}//List的无参构造方法
public void Two_reverse()//第二种方法进行链表翻转
{
if(this.head==null)
return;
ListNode temp;
ListNode cur=head.next;//cur=head 也可以将链表反转,但是会重复无限输出
ListNode pre=null;
while (cur!=null)
{
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
this.head.next=pre;
}
public void reverse()//将单链表翻转 方法一 自己独立没写出来,重新建立一个链表
{
if(head.next==null||head.next.next==null)
return;//链表为空,则直接退出
ListNode new_head=new ListNode(0);//定义一个新链表的头结点
ListNode temp=this.head.next;
ListNode cur=null;//用来指向下一个数组
while (temp!=null)
{
cur=temp.next;
temp.next=new_head.next;
new_head.next=temp;
temp=cur;
}
head.next=new_head.next;
}
public void search(int k)//寻找倒数第k个结点的值
{
if(this.head.next==null)
return;
ListNode front=this.head.next;//定义二个指针都指向下一个结点
ListNode rear=this.head;//定义二个指针
boolean falg=false;
while (rear.next!=null)//将后面的指针与头指针间距拉开k-1
{
k--;
if(k<0)
break;
rear=rear.next;
}
while (rear.next!=null)
{
rear=rear.next;
front=front.next;
}
System.out.println(front.data);
}
public void search_second(int k)//按照第二种方式查询倒数第k个结点的值
{
if(this.head==null)
return;
ListNode temp=head.next;//定义一个新的遍历指针
int count=0;//用来统计链表结点的个数
while (temp!=null)
{
count++;
temp=temp.next;//指向下一个结点
}
ListNode temp1=head.next;//定义另外一个新的遍历指针
int count1=0;
while (temp1!=null)
{
count1++;
if(count1==(count-k+1)) {
System.out.println(temp1.data);
break;
}
temp1=temp1.next;
}
}
public int sum()//计算链表中结点中个数
{
ListNode temp=this.head.next;
int count=0;
while(temp!=null)
{
count++;
temp=temp.next;
}
return count;
}
public int sums(){
if(head.next==null) {
System.out.println("链表为空");
return 0;
}
ListNode temp=this.head.next;
int count=0;
while (temp!=null)
{
count++;
temp=temp.next;
}
return count;
}
public void dele(ListNode new_node)//删除链表中的结点
{
ListNode temp=head;
boolean flag=false;
while (true)
{
if(temp.next==null)
break;
// if(temp.data==new_node.data)//错误代码。。。。。。。。。。。。。。。。。
if(temp.next.data==new_node.data)
{
flag=true;
break;
}
temp=temp.next;//temp变量指向下一个结点
}
if(true)
{
temp.next=temp.next.next;
}
else
{
System.out.println("链表中不存在该结点");
}
}
public void add_order(ListNode new_node) //按顺序将结点添加到链表中
{
ListNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null)
break;
else if (temp.next.data > new_node.data)
break;
else if (temp.next.data == new_node.data) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.println("已存在该值");
} else {
new_node.next = temp.next;
temp.next = new_node;
}
}
public void add(ListNode node)//向链表末尾中添加数据
{
ListNode temp = this.head;
while (true) {
if (temp.next == null)
break;
else
temp = temp.next;//如果不是最后一个结点,将移动到下一个结点上
}
temp.next = node;
}
public void showList()//将链表中数字都打印出来
{
if (head.next == null) {
System.out.println("链表为空");
return;
}
ListNode temp = head.next;
while (true) {
if (temp == null)
break;
System.out.print(temp.data+" ");
temp = temp.next;
}
}
}