【数据结构】单链表的倒序、删除相同结点、按值排序等简单操作用java实现

class LinkedList{
	private Node head;// 头结点
	private int length;	// 链表长度
	
	// 节点
	private class Node{
		int val;// 数据域
		Node next = null;// 指针域,指向下一个节点对象
		
		public Node(int data){
			this.val = data;
		}
	}
	
	// 链表的初始化
	LinkedList(){
		head = null;
		length = 0;
	}
	
	// 插入数据
	public void add(int d){
		Node node = new Node(d);
		if(head == null){// 如果头节点为空,则首先创建头结点
			head = node;
			length++;
			return;
		}
		Node temp = head;
		while(temp.next != null){
			temp = temp.next;
		}
		temp.next = node;
		this.length++;
	}
	
	// 删除第index个节点,index是从0开始的
	public Boolean remove(int index){
		if(index < 0 || index >= length){
			System.out.println("超出链表的范围!");
			return false;
		}
		if(head == null){
			System.out.println("链表为空!");
			return false;
		}
		Node buf = null;// 用于缓存要删除的结点
		Node cur = head;// 指向当前结点
		if(index == 0){
			head = head.next;
			this.length--;
			return true;
		}
		// 这个循环找到的是要删除结点的前一个结点
		for(int i=0; i<index-1 && (cur.next!=null); i++){
			cur = cur.next;
		}
		// 缓存要删除的节点
		buf = cur.next;
		// 连接
		cur.next = buf.next;
		this.length--;
		return true;
	}
	
	// 获得index结点上的数值,index是从0开始的
	public int getVal(int index){
		if(index < 0){
			System.out.println("下标不能为负数!");
			return 0;
		}
		Node cur = head;
		// 这个循环获得index的结点
		for(int i=0; i<index && (cur.next != null); i++){
			cur = cur.next;
		}
		return cur.val;
	}
	
	// 获得链表的长度
	public int size(){
		return this.length;
	}
	
	// 打印链表
	public void printLinkedList(){
		Node cur = head;
		for(int i=0; i<this.length-1; i++){
			System.out.print(cur.val + "->");
			cur = cur.next;
		}
		System.out.println(cur.val);
	}
	
	// 在index位置插入结点,index是从0开始
	public void add(int index, int val){
		Node newNode = new Node(val);
		if(index < 0){
			System.out.println("链表下标不能为负数!");
			return;
		}
		if(head == null){
			head = newNode;
			this.length++;
			return;
		}
		if(index == 0){
			newNode.next = head;
			head = newNode;
			this.length++;
			return;
		}
		Node cur = head; // 当前结点
		// 这个循环是找到添加结点index的上一个结点,index是从0开始
		for(int i=0; i<index-1 && (cur.next!=null); i++){
			cur = cur.next;
		}
		// 连接
		newNode.next = cur.next;
		cur.next = newNode;// 这两句的顺序不能错
		this.length++;
	}
	
	//对链表中数值按从下到大进行排序,但排序的效率不高,使用最简单的冒泡排序
	public void sort(){
		Node cur = head;
		Node next = null;
		int tempval = 0;
		while(cur != null){
			next = cur.next;
			while(next != null){
				if(cur.val > next.val){
					tempval = cur.val;
					cur.val = next.val;
					next.val = tempval;
				}
				next = next.next;
			}
			cur = cur.next;
		}
	}
	
	// 倒序输出链表中所有结点数值,这个递归虽然简单但必须给调用者这个链表的头结点
	public void reversePrint(Node hNode){
		if(hNode != null){
			reversePrint(hNode.next);
			System.out.print(hNode.val + "  ");
		}
	}
	public Node getHead() {
		return head;
	}
	
	// 删除链表中值的相同的结点
	public void deleteRepeat(){
		if(this.length < 2){
			return;
		}
		Node outsideCur = head;// 外部循环当前结点
		Node insidePreBuf = null;// 内部循环缓存上一个结点
		Node insideCur = null;// 内部循环当前结点
		while(outsideCur != null){
			insideCur = outsideCur.next;
			insidePreBuf = outsideCur;
			while(insideCur != null){
				if(insideCur.val == outsideCur.val){
					insidePreBuf.next = insideCur.next;
					this.length--;
				}
				insidePreBuf = insideCur;
				insideCur = insideCur.next;
			}
			outsideCur = outsideCur.next;
		}
	}
}

public class TestLinkedList {

	public static void main(String[] args) {
		LinkedList list = new LinkedList();
		list.add(0);
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		list.printLinkedList();
		list.sort();
		list.printLinkedList();
		list.reversePrint(list.getHead());
		list.printLinkedList();
		list.deleteRepeat();
		list.printLinkedList();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值