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

举报
肥学 发表于 2022/03/26 00:17:28 2022/03/26
【摘要】 import java.util.*; public class Main { static Node headNode=new Node(),temp; public static Node...

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

文章来源: blog.csdn.net,作者:肥学,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jiahuiandxuehui/article/details/123198434

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。