day13:链表

一、线性链表
(一)基础知识点
1、线性表的链式存储结构的特点:用一组任意的存储单元存储线性表的数据元素(可以连续也可以不连续)。
2、结点:用于存储本身的信息与直接后继的存储位置。
3、数据域:存储数据元素信息的域。
4、指针域:存储直接后继存储位置的域。
因此,n个结点链结成一个链表。每个结点只包含一个指向直接后继结点的指针域,称为线性链表或者单链表。
(二)单链表小结——更容易插入和删除(需要寻找前驱结点)
1、存储内容:数据元素ai + 指向ai直接相邻的指针 / 结点
2、优点:动态结构——整个存储空间可以为多个链表共用;
不需要预先分配空间。
3、缺点:指针占用额外的存储空间;
不能随机存取,查找速度较慢 。
二、循环链表
特点:表中最后一个结点的指针域指向头结点,整个链表形成一个环。因此,从表中任一结点出发都可找到表中其他结点。
三、双向链表
特点:结点中有两个指针域,一个指向直接前驱,另一个指向直接后继。
具体实现:
1、为节点创造一个类

class Node {
    int data;
			
	Node next;
	public Node(int paraValue) {
		data = paraValue;
		next = null;
	}// Of the constructor
}// Of class Node

2、构造一个空链表

Node header;
	
public LinkedList() {
	header = new Node(0);
}

3、重写toString方法并判断链表是否为空,如果不空则打印链表里面的元素

public String toString() {
	String resultString = "";
	if (header.next == null) {
		return "empty";
	} // Of if
		
	Node tempNode = header.next;
	while (tempNode != null) {
		resultString += tempNode.data + ", ";
		tempNode = tempNode.next;
	} // Of while
		
	return resultString;
}// Of toString

4、查找元素位置,若一个元素出现多次,则返回第一次出现的位置。

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;
		} // Of if
			
		tempNode = tempNode.next;
		tempCurrentPosition++;
	} // Of while
	return tempPosition;
}// Of locate

5、在指定位置插入指定元素

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;
		} // Of if
		tempNode = tempNode.next;
	}// Of for i
		
	tempNewNode = new Node(paraValue);
	tempNewNode.next = tempNode.next;
	tempNode.next = tempNewNode;
		
	return true;
}// Of insert

6、删除指定位置的元素
(如果删除0位置的元素,首先head的值为null,head的next直接指向1位置的元素,不会进入循环。)

public boolean delete(int paraPosition) {
	if (header.next == null) {
		System.out.println("The list is empty and we can not delete it.");
		return false;
	} // Of if
		
	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

综合代码:

package linear_list;

public class LinkedList {
	
	/**
    * Create an inner class for the node.
    */
	class Node {
			int data;
			
			/**
			 * The reference to the next node.
			 */
			Node next;
			public Node(int paraValue) {
				data = paraValue;
				next = null;
			}// Of the constructor
		}// Of class Node
	
	// The header node which data is never used.
	Node header;
	
	/**
	 ********************
	 * Construct an empty linked list.
	 * The header.next is null.
	 ********************
	 */
	public LinkedList() {
		header = new Node(0);
	}
	
	/**
	 ********************
	 * Override the toString method.
	 * Determine whether the linked list is empty and print the linked list.
	 ********************
	 */
	public String toString() {
		String resultString = "";
		if (header.next == null) {
			return "empty";
		} // Of if
		
		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		} // Of while
		
		return resultString;
	}// Of toString
	
	
	public void reset() {
		header.next = null;
	}
	
	/**
	 ********************
	 * Find the given value and return the first position.
	 * @param paraValue  -1 stand for not find the element.
	 * @return
	 ********************
	 */
	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;
			} // Of if
			
			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // Of while
		return tempPosition;
	}// Of locate
	
	/**
	 ********************
	 * Insert a given value to the given position.
	 * @param paraPosition
	 * @param paraValue
	 * @return
	 ********************
	 */
	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;
			} // Of if
			tempNode = tempNode.next;
		}// Of for i
		
		tempNewNode = new Node(paraValue);
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;
		
		return true;
	}// Of insert
	
	/**
	 ******************** 
	 * Delete the value of the given position.
	 * @param paraPosition
	 * @return
	 ********************
	 */
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("The list is empty and we can not delete it.");
			return false;
		} // Of if
		
		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
	
	/**
	 ********************
	 * The entrance of the program.
	 * 
	 * @param args
	 ********************
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedList tempFirstList = new LinkedList();
		System.out.println("The list is:" + tempFirstList.toString());
		
		for (int i = 0; i < 10; i++) {
			tempFirstList.insert(0, i);
		} // Of for i
		System.out.println("Inserted, the list is:" + tempFirstList.toString());
		
		tempFirstList.insert(12, 9);
		
		tempFirstList.delete(10);
		
		tempFirstList.delete(4);
		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 < 10; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		} // Of for i
	}// Of main
}// Of class LinkedList

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值