构建有头节点的链表并实现增删改查和翻转操作。


import java.util.*;

public class Main {
	static Node headNode=new Node(),temp;
	
	public static Node InitialList() {//尾插
		temp=headNode;
		int n=4,m=1;//n为元素个数
		while(n>0) {
			Node node=new Node(n+1);
			temp.next=node;
			temp=temp.next;
			n--;
			m++;
		}
		headNode.data=m-1;//链表长度不包括头节点
		return headNode;
	}
	public static void reverseList() {//头插法
		System.out.println("反转:");
		temp=headNode.next;
		int[] arr=new int[headNode.data];
		for(int i=0;i<headNode.data;i++) {
			arr[i]=temp.data;
			temp=temp.next;
		}
		int flag=0;
		temp=headNode;
		temp.next=null;
		while(flag<headNode.data) {
			Node node=new Node(arr[flag]);
			node.next=temp.next;
			temp.next=node;
			flag++;
		}
		playdata(headNode);
	}
	public static void playdata(Node node) {
		temp=node.next;
		System.out.println("长度为"+headNode.data);
		while(temp!=null) {
			System.out.print(temp.data+" ");
			temp=temp.next;
		}
		System.out.println();
	}
	public static boolean ListInsert(int i,int j) {//i表示要插入的位置,j为要插入的元素
		System.out.println("插入:");
		if(i>headNode.data||i<=0)return false;
		temp=headNode.next;
		int flag=1;
		while(flag<i-1) {
			temp=temp.next;
			flag++;
		}
		Node node=new Node(j);
		node.next=temp.next;
		temp.next=node;
		headNode.data++;
		return true;
	}
	public static boolean deleteNode(int i) {
		System.out.println("删除:");
		Node node=searchNode(i);
		if(node.next!=null&&node!=null) {
			node.data=node.next.data;
			node.next=node.next.next;
			headNode.data--;
			System.out.print("删除后链表为:");
			playdata(headNode);
		}else if(node!=null&&node.next==null) {
			int n=2;
			temp=headNode.next;
			while(n<headNode.data) {
				temp=temp.next;
				n++;
			}
			temp.next=null;
			headNode.data--;
			System.out.print("删除后链表为:");
			playdata(headNode);
		}else {
			return false;
		}
		return true;
	}
	public static Node searchNode(int i) {
		System.out.println("查询:");
		System.out.println("目前链表长度为"+headNode.data);
		temp=headNode.next;
		int flag=1;
		boolean ans=false;
		while(temp.data!=i&&temp.next!=null) {
			temp=temp.next;
			if(temp.data==i)ans=true;
			flag++;
		}
		if(ans)
		System.out.println("元素"+i+"在该链表的第"+flag+"位");
		else
			System.out.println("该链表不存在"+i+"元素");
		return temp;
	}
	
	public static void main(String[] args) {
		InitialList();
		System.out.print("插入前:");
		playdata(headNode);
		System.out.print("插入后:");
		if(ListInsert(3,9))playdata(headNode);
		else {
			System.out.println("插入位置不合法!");
		}
		searchNode(2);//查找
		deleteNode(2);//删除
		reverseList();//反转链表
	}
}

class Node{
	public int data;
	public Node next;
	public Node() {
		
	}
	public Node(int data) {
		this.data=data;
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥学

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值