数据结构 java

单链表

public class Node {

	public Object data;
	public Node next;

}
import java.util.Scanner;

public class LinkedList {
	public Node head;

	public LinkedList() {
		head = new Node();
	}

	public void listHeadInsert() {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		while (x != 9999) {
			Node s = new Node();
			s.data = x;
			s.next = head.next;
			head.next = s;
			x = sc.nextInt();
		}
	}

	public void listTailInsert() {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		Node r = head;
		while (x != 9999) {
			Node s = new Node();
			s.data = x;
			s.next = r.next;
			r.next = s;
			r = s;
			x = sc.nextInt();
		}
	}

	public int length() {
		int len = 0;
		Node p = head;
		while (p.next != null) {
			p = p.next;
			len++;
		}
		return len;
	}

	public Node locataElem(Object e) {
		Node p = head.next;
		while (p != null && p.data != e) {
			p = p.next;
		}
		return p;
	}

	public Node getElem(int i) {
		if (i < 0) {
			return null;
		}
		int j = 0;
		Node p = head;
		while (p != null && j < i) {
			p = p.next;
			j++;
		}
		return p;
	}

	public boolean deleteNode(Node p) {
		if (p == null) {
			return false;
		}
		Node q = p.next;
		p.data = q.data;
		p.next = q.next;
		return true;
	}

	public boolean delete(int i) {
		if (i < 1) {
			return false;
		}
		Node p = getElem(i - 1);
//		int j = 0;
//		Node p = head;
//		while (p != null && j < i - 1) {
//			p = p.next;
//			j++;
//		}
		if (p == null) {
			return false;
		}
		if (p.next == null) {
			return false;
		}
		Node q = p.next;
		p.next = q.next;
		return true;
	}

	public boolean insertPriorNode(Node p, Object e) {
		if (p == null) {
			return false;
		}
		Node s = new Node();
		s.next = p.next;
		p.next = s;
		s.data = p.data;
		p.data = e;
		return true;
	}

	public boolean insertNextNode(Node p, Object e) {
		if (p == null) {
			return false;
		}
		Node s = new Node();
		s.data = e;
		s.next = p.next;
		p.next = s;
		return true;
	}

	public boolean insert(int i, Object e) {
		if (i < 1) {
			return false;
		}
		Node p = getElem(i - 1);
//		int j = 0;
//		Node p = head;
//		while (p != null && j < i - 1) {
//			p = p.next;
//			j++;
//		}

		return insertNextNode(p, e);
//		if (p == null) {
//			return false;
//		}
//		Node s = new Node();
//		s.data = e;
//		s.next = p.next;
//		p.next = s;
//		return true;
	}

	public void insertHead(Object e) {
		Node node = new Node();
		node.data = e;
		node.next = head.next;
		head.next = node;
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		Node current = head.next;
		while (current != null) {
			res.append(current.data + "->");
			current = current.next;
		}
		res.append("null");
		return res.toString();
	}
}

双向链表

public class DNode {
	public Object data;
	public DNode prior;
	public DNode next;
}
import java.util.Scanner;

public class DLinkedList {
	public DNode head;

	public DLinkedList() {
		head = new DNode();
	}

	public DNode getElem(int i) {
		if (i < 0) {
			return null;
		}
		int j = 0;
		DNode p = head;
		while (p != null && j < i) {
			p = p.next;
			j++;
		}
		return p;
	}

	public void listInsertHead() {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		while (x != 9999) {
			DNode s = new DNode();
			s.data = x;
			InsertNextDNode(head, s);
			x = sc.nextInt();
		}
	}

	public boolean insert(int i, Object e) {
		if (i < 1) {
			return false;
		}
		DNode p = head;
		int j = 0;
		while (p != null && j < i - 1) {
			p = p.next;
			j++;
		}
		if (p == null) {
			return false;
		}
		DNode s = new DNode();
		s.data = e;
		s.next = p.next;
		if (p.next != null) {
			p.next.prior = s;
		}
		s.prior = p;
		p.next = s;
		return true;
	}

	public boolean InsertNextDNode(DNode p, DNode s) {
		if (p == null || s == null) {
			return false;
		}
		s.next = p.next;
		if (p.next != null) {
			p.next.prior = s;
		}
		s.prior = p;
		p.next = s;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		DNode current = head.next;
		while (current != null) {
			res.append(current.data + "->");
			current = current.next;
		}
		res.append("null");
		return res.toString();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值