day13--链表

1.定义一个表示链表的类

  • data表示这个链表中存储的数据…
  • next指向下一个链表节点
  • 每当new 一个Node 实例,在内存中就会为其分配内存空间,它就会有一个内存地址
class Node {	
		int data;
		Node next;
		//构造方法
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}
	}// of class Node

2.查找链表中的数据

  • 循环遍历链表,比对数据,相同则返回数据所在位置
  • 若循环链表没有找到,则返回-1
public int locate(int paraValue) {
		int tempPosition = -1;

		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		//循环遍历链表
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} 

			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // of while

		return tempPosition;
	}// of locate

3.在链表中插入一个数

  • 首先判断待插入的位置是否合法
  • 合法:先构造一个新的结点,将新结点链接到链表中去
  • 不合法:输出The position paraPosition is illegal.返回false
public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} 
			tempNode = tempNode.next;
		} // of for i

		//构造一个新的结点
		tempNewNode = new Node(paraValue);

		//将新构造的结点连接到链表中
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}// Of insert

4.删除链表中的某个位置的数

  • 首先判断链表是否为空
  • 再判断待删除的位置是否合法
  • 找到待删除位置的前一个位置
public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		} 
		Node tempNode = header;
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // of if
			tempNode = tempNode.next;
		} // of for i

		tempNode.next = tempNode.next.next;

		return true;
	}// of delete

5.完整代码实现

package com.datastructure;

public class LinkedList {
	//内置类
	class Node {
		
		int data;
		
		Node next;

		//构造方法
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		}
	}// of class Node

	//头节点
	Node header;

	//构造方法,创建一个空的链表
	public LinkedList() {
		header = new Node(0);
		//header.next = null; 
	}// Of the first constructor

	
	public String toString() {
		String resultString = "";

		if (header.next == null) {
			return "empty";
		} // of if

		Node tempNode = header.next;
		while (tempNode.next != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		}	
		resultString += tempNode.data;
				
		return resultString;
	}// of toString

	//重置
	public void reset() {
		header.next = null;
	}// of reset

	/*
	 * 查找:
	 * 若找到,则返回数值所在的位置
	 * 若未找到,则返回-1
	 */
	public int locate(int paraValue) {
		int tempPosition = -1;

		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		//循环遍历链表
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} 

			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // of while

		return tempPosition;
	}// of locate

	//插入
	public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;

		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} 

			tempNode = tempNode.next;
		} // of for i

		//构造一个新的结点
		tempNewNode = new Node(paraValue);

		//将新构造的结点连接到链表中
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;

		return true;
	}// of insert

	//删除
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		} 

		Node tempNode = header;
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // of if
			tempNode = tempNode.next;
		} // of for i
		tempNode.next = tempNode.next.next;
		
		return true;
	}// of delete

	public static void main(String args[]) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0, i);
		} // of for i
		System.out.println("Inserted, the list is: " + tempFirstList.toString());

		tempFirstList.insert(6, 9);

		tempFirstList.delete(4);

		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		tempFirstList.delete(0);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());

		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		} // of for i
		
	}// of main
}// of class LinkedList
代码结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值