Reescribir el código fuente de String y LinkedList

Vuelva a escribir el código fuente de String JAVA de la siguiente manera:

classs String{ 
private: char *m_data; //数据成员 
public: String(const char *str = NULL) //普通构造函数 
{ 
if (str == NULL) { 
m_data = new char[1]; 
*m_data = '\0'; } 
else { 
int len = strlen(str); 
m_data = new char[len+1]; 
strcpy(m_data, str); } } 

~String() //析构函数 
{ 
delete []m_data; m_data = NULL; } 

String(const String &other) //拷贝构造函数 
{ 
int len = strlen(other.m_data); 
m_data = new char[len + 1]; 
strcpy(m_data, other.m_data); } 

String& operate=(const String &other) //赋值函数 
 { 
if (&other == this) { 
return *this; } 
else { delete []m_data; 
m_data = NULL; 
int len = strlen(other.m_data); 
m_data = new char[len+1]; 
strcpy(m_data, other.m_data); 
return *this; } 
}};

Vuelva a escribir el código fuente de LinkedList de la siguiente manera:

package linkedList_zlh;

import java.util.Iterator;

public class LinkedList_H<T> implements Iterable<T> {
	// 记录元素个数
	private int Size;
	// 记录修改次数
	private int Count = 0;
	// 定义两个节点,首节点和尾节点
	private Node<T> begin;
	private Node<T> end;

	public LinkedList_H() {
		clear();
	}

	// 初始化一个空表
	public void clear() {
		begin = new Node<T>(null, null, null);
		end = new Node<T>(null, begin, null);

		begin.next = end;

		Size = 0;
		Count++;
	}

	public int size() {
		return Size;
	}

	public boolean isEmpty() {
		return size() == 0;
	}

	public boolean add(T value) {
		add(size(), value);
		return true;
	}

	public void add(int index, T value) {
		// 先getNode(index).然后执行addBefore
		addBefore(getNode(index), value);
	}

	// 在指定的那个节点P的前方插入值为value的一个元素
	private void addBefore(Node<T> p, T value) {
		Node<T> newNode = new Node<T>(value, p.prev, p);
		newNode.prev.next = newNode;
		p.prev = newNode;
		Size++;
		Count++;
	}

	// 通过索引值返回对应位置的节点类信息
	private Node<T> getNode(int index) {
		Node<T> p;
		if (index < 0 || index > size()) {
			throw new IndexOutOfBoundsException();
		}

		if (index < (size() / 2)) {
			p = begin.next;
			for (int i = 0; i < index; i++) {
				p = p.next;
			}

		} else {
			p = end;
			for (int i = size(); i > index; i--) {
				p = p.prev;
			}
		}
		return p;
	}

	// 返回指定索引处的元素值
	public T get(int index) {
		return getNode(index).data;
	}

	// 将指定所引处的元素值修改为value
	public T set(int index, T value) {
		Node<T> p = getNode(index);
		T oldData = p.data;
		p.data = value;
		return oldData;
	}

	// 移除指定索引处的元素值
	public T remove(int index) {
		return remove(getNode(index));
	}

	private T remove(Node<T> p) {

		p.next.prev = p.prev;
		p.prev.next = p.next;

		Size--;
		Count++;
		return p.data;

	}

	@Override
	public Iterator<T> iterator() {
		return new LinkedListIteraor();
	}

	private class LinkedListIteraor implements Iterator<T> {

		// 保留第一个起始位置的节点
		private Node<T> current = begin.next;
		// 记录此刻集合修改的总次数,之后会拿Count再和此值作比较,如果不相等,说明在迭代过程中,
		// 集合发生了修改操作,则会抛出异常
		private int exceptedModCount = Count;
		// 判断是否可以向后移动指针
		private boolean canMove = false;

		@Override
		public boolean hasNext() {
			return current != end;
		}

		@Override
		public T next() {
			if (exceptedModCount != Count) {
				throw new java.util.ConcurrentModificationException();
			}
			if (!hasNext()) {
				throw new java.util.NoSuchElementException();
			}

			T nextValue = current.data;
			current = current.next;
			canMove = true;
			return nextValue;
		}

		@Override
		public void remove() {
			if (exceptedModCount != Count) {
				throw new java.util.ConcurrentModificationException();
			}
			if (!canMove) {
				throw new IllegalStateException();
			}
			LinkedList_H.this.remove(current.prev);
			canMove = false;
			exceptedModCount++;
		}

	}

	private static class Node<T> {
		// 当前节点数据
		public T data;
		// 到前一个节点的链
		public Node<T> prev;
		// 到后一个节点的链
		public Node<T> next;

		public Node(T data, Node<T> prev, Node<T> next) {
			this.data = data;
			this.prev = prev;
			this.next = next;
		}
	}

}

 

Supongo que te gusta

Origin blog.csdn.net/qq_32301683/article/details/87873949
Recomendado
Clasificación