数据结构 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
    评论
很高兴为您介绍数据结构Java实验。数据结构是计算机科学中的一个重要分支,它研究数据的组织、存储、检索、排序和管理等问题。Java是一种广泛使用的编程语言,它具有面向对象、跨平台等特点,非常适合用于数据结构的实现。 在Java中,常用的数据结构包括栈、队列、链表、树、图等。下面以栈和队列为例,介绍Java中的数据结构实验。 1. 栈的实现 栈是一种后进先出(LIFO)的数据结构,它的基本操作包括入栈、出栈、判断栈是否为空、获取栈顶元素等。下面是Java中栈的实现代码: ```java public class Stack<T> { private ArrayList<T> list = new ArrayList<T>(); public void push(T item) { list.add(item); } public T pop() { if (list.isEmpty()) { throw new NoSuchElementException("Stack is empty"); } return list.remove(list.size() - 1); } public boolean isEmpty() { return list.isEmpty(); } public T peek() { if (list.isEmpty()) { throw new NoSuchElementException("Stack is empty"); } return list.get(list.size() - 1); } } ``` 2. 队列的实现 队列是一种先进先出(FIFO)的数据结构,它的基本操作包括入队、出队、判断队列是否为空、获取队头元素等。下面是Java中队列的实现代码: ```java public class Queue<T> { private LinkedList<T> list = new LinkedList<T>(); public void enqueue(T item) { list.addLast(item); } public T dequeue() { if (list.isEmpty()) { throw new NoSuchElementException("Queue is empty"); } return list.removeFirst(); } public boolean isEmpty() { return list.isEmpty(); } public T peek() { if (list.isEmpty()) { throw new NoSuchElementException("Queue is empty"); } return list.getFirst(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值