首页 > 编程 > Java > 正文

java 实现单向链表、双向链表、双向循环链表

2019-11-06 07:31:22
字体:
来源:转载
供稿:网友

1.单向链表

class List {	Node head;  //指向链表头部	public List() {		clearList();	}	/**	 * 清空链表信息	 */	public void clearList() {		head = null;	}	/**	 * 获取链表信息	 */	public void getList() {		Node cur = head;		while (cur != null) {			System.out.PRint(cur.data + ",");			cur = cur.next;		}		System.out.println();	}	/**	 * 添加结点到链表底部	 * @param data	 */	public void addNode(int data) {		Node node = new Node(data);		Node pro = head;		Node cur = head;		if (cur == null) {			node.next = null;			head = node;		} else {			while (cur != null) {				pro = cur;				cur = cur.next;			}			cur = node;			pro.next = cur;		}	}	/**	 * 指定位置将结点插入到链表中	 * @param pos	 * @param data	 */	public void addNode(int pos, int data) {		int index;		Node node = new Node(data);		Node cur = head;		if (pos < 0 || pos > this.getLength() - 1) {			System.out.println("操作失败,超出链表长度!");			return;		} else {			for (index = 0; index < pos; index++) {				cur = cur.next;			}			if (cur == null) {				cur = node;			} else {				node.next = cur.next;				cur.next = node;			}		}	}	/**	 * 获取链表长度	 * @return	 */	public int getLength() {		int length = 0;		Node cur = head;		if (cur == null) {			return 0;		} else {			while (cur != null) {				length++;				cur = cur.next;			}			return length;		}	}	/**	 * 删除链表中结点信息	 * @param data	 */	public void deleteNode(int data) {		int index = searchNode(data);		int del = 0;		Node pro = head;		Node cur = head;		if (index < 0) {			System.out.println("操作失败,没有找到 " + data);			return;		} else if (index == 0) {			if(cur.next==null){				head=null;			}else {				head=head.next;			}		} else {			while (del != index) {				pro = cur;				if (cur.next != null) {					cur = cur.next;				}				del++;			}			pro.next = cur.next;			cur = cur.next;		}		System.out.println("操作成功!删除了 " + data);	}	/**	 * 查找链表中的结点,并返回其所在位置	 * @param data	 * @return	 */	public int searchNode(int data) {		int index = 0;		Node cur = head;		while (cur != null) {			if (cur.data == data) {				return index;			} else {				cur = cur.next;				index++;			}		}		return -1;	}	/**	 * 结点类	 * @author wky653	 *	 */	class Node {		int data;		Node next;		public Node(int data) {			this.data = data;		}		public Node() {		}	}}/** * 测试类 * @author wky653 * */public class Test01 {	public static void main(String[] args) {		List list = new List();		list.addNode(144);		list.addNode(15);		list.addNode(14);		list.addNode(13);		list.addNode(3, 12);		list.getList();		System.out.println("length=" + list.getLength());		list.deleteNode(12);		list.getList();		list.clearList();		list.getList();	}}

运行结果:

144,15,14,13,12,length=5操作成功!删除了 12144,15,14,13,

2.双向链表

//结点数据class Node {	int data; // 数据域	Node back; // 上一个指针域	Node next; // 下一个指针域	public Node(int data) {		this.data = data;	}}// 双向链表的具体操作class TwoList {	Node head; // 保存头节点信息	public TwoList() {		head = null;	}	/**	 * 打印链表所有结点信息	 */	public void getList() {		Node cur = head;		while (cur != null) {			System.out.print(cur.data + ",");			cur = cur.next;		}		System.out.println();	}	/**	 * 添加在链表头部	 * 	 * @param data	 */	public void addToHead(int data) {		Node node = new Node(data);		if (head == null) {			head = node;		} else {			node.next = head;			head.back = node;			head = node;		}	}	/**	 * 添加在链表尾部	 * 	 * @param data	 */	public void addToTail(int data) {		Node node = new Node(data);		Node pro = head;		Node cur = head;		if (head == null) {			head = node;		} else {			while (cur != null) {				pro = cur;				cur = cur.next;			}			cur = node;			cur.back = pro;			pro.next = cur;		}	}	/**	 * 在指定位置插入新的节点	 * 	 * @param pos	 * @param data	 */	public void addNode(int pos, int data) {		int index = 0;		Node node = new Node(data);		Node pro = head;		Node cur = head;		if (pos < 0 || pos > this.getLength() - 1) {			System.out.println("超出链表长度,添加" + data + "失败!");			return;		}		for (; index <= pos; index++) {			if (cur != null) {				pro = cur;				cur = cur.next;			} else {				System.out.println("添加" + data + "失败!");				return;			}		}		pro.next = node;		node.back = pro;		node.next = cur;		if (cur != null) {			cur.back = node;		}	}	/**	 * 获取链表长度	 * 	 * @return	 */	public int getLength() {		Node cur = head;		int length = 0;		if (cur == null) {			return -1;		}		while (cur != null) {			length++;			cur = cur.next;		}		return length;	}	/**	 * 删除链表中指定信息的结点	 * 	 * @param data	 */	public void deleteNode(int data) {		int flag = searchNode(data);		int index;		Node pro = head;		Node cur = head;		if (flag < 0) {			System.out.println(data + "不存在,删除操作失败!");		} else if (flag == 0) {			head.next.back = null;			head = head.next;		} else {			for (index = 0; index < flag; index++) {				pro = cur;				cur = cur.next;			}			pro.next = cur.next;			cur = cur.next;			// cur.back=pro;		}	}	/**	 * 删除指定位置的结点	 * 	 * @param pos	 * @param tl	 */	public void deleteNode(int pos, TwoList tl) {		int index;		Node pro = head;		Node cur = head;		if (pos < 0 || pos > tl.getLength() - 1) {			System.out.println("超出数组长度,删除操作失败!");		} else if (pos == 0) {			head.next.back = null;			head = head.next;		} else {			for (index = 0; index < pos; index++) {				pro = cur;				cur = cur.next;			}			if (cur != null) {				pro.next = cur.next;				cur = cur.next;				if (cur != null) {					cur.back = pro;				}			} else {				System.out.println("删除失败!");			}		}	}	/**	 * 返回指定信息的结点位置	 * 	 * @param data	 * @return	 */	public int searchNode(int data) {		int count = 0;		Node cur = head;		while (cur != null) {			if (cur.data == data) {				return count;			} else {				count++;				cur = cur.next;			}		}		return -1;	}}/** * 测试类 *  * @author wky653 * */public class Test02 {	public static void main(String[] args) {		TwoList list = new TwoList();		list.addToHead(18);		list.addToHead(14);		list.addToHead(11);		list.addToHead(48);		list.addToTail(20);		list.addToTail(22);		list.addNode(-1, 10);		list.getList();		System.out.println(list.getLength());		System.out.println(list.searchNode(14));		list.deleteNode(20);		list.getList();		list.deleteNode(4, list);		list.getList();	}}

运行结果:

超出链表长度,添加10失败!48,11,14,18,20,22,6248,11,14,18,22,48,11,14,18,

3.双向循环链表

class ThreeList {	Node head;	Node tail;	public ThreeList() {		this.clearList();	}	/**	 * 清空链表	 */	public void clearList() {		head = tail = null;	}	/**	 * 读取完整链表信息	 */	public void getList() {		Node cur = head;		if (cur == null) {			System.out.println("链表为空!");			return;		}		while (cur != tail) {			System.out.print(cur.data + ",");			cur = cur.next;		}		System.out.print(cur.data);		System.out.println();	}	/**	 * 返回链表长度	 * 	 * @return	 */	public int getLength() {		int length;		Node cur = head;		if (cur == null) {			return 0;		}		for (length = 0; cur != tail; length++) {			cur = cur.next;		}		return length + 1;	}	/**	 * 往链表头部插入结点	 * 	 * @param data	 */	public void addHeadNode(int data) {		Node node = new Node(data);		if (head == null) {			head = node;			tail = head;		} else if (head == tail) {			head = node;			head.next = tail;			head.back = tail;			tail.next = head;			tail.back = head;		} else {			node.next = head;			head.back = node;			head = node;			head.back = tail;			tail.next = head;		}		System.out.println("添加" + data + "成功!");	}	/**	 * 往链表尾部插入结点	 * 	 * @param data	 */	public void addTailNode(int data) {		Node node = new Node(data);		if (head == null) {			head = new Node(data);			tail = head;		} else if (head == tail) {			tail = node;			tail.back = head;			tail.next = head;			head.next = tail;			head.back = tail;		} else {			tail.next = node;			node.back = tail;			tail = node;			tail.next = head;			head.back = tail;		}		System.out.println("添加" + data + "成功!");	}	/**	 * 指定位置插入结点	 * 	 * @param pos	 * @param data	 */	public void addToNode(int pos, int data) {		int index = 0;		Node node = new Node(data);		Node cur = head;		Node pro = head;		if (pos > this.getLength()) {			System.out.println("操作失败,超出链表长度!");			return;		} else if (pos < 0) {			addHeadNode(data); // 往链表头部插入			return;		} else {			for (; index <= pos; index++) {				pro = cur;				cur = cur.next;			}			pro.next = node;			node.back = pro;			node.next = cur;			cur.back = node;			pro = node;		}		System.out.println("添加" + data + "成功!");	}	/**	 * 删除指定信息的结点	 * 	 * @param data	 */	public void deleteNode(int data) {		int index = searchNode(data);		int del = 0;		Node pro = head;		Node cur = head;		if (index < 0) {			System.out.println("操作失败,没有找到 " + data);			return;		} else if (index == 0) { // 目标是链表头部结点			head = head.next;			head.back = tail;			tail.next = head;		} else if (index == this.getLength() - 1) { // 目标是链表尾部结点			tail.back.next = head;			head.back = tail.back;			tail = tail.back;		} else {			while (del != index) {				pro = cur;				if (cur.next != null) {					cur = cur.next;				}				del++;			}			pro.next = cur.next;			cur.next.back = pro;			cur.back = cur.next = null;		}		System.out.println("操作成功!删除了 " + data);	}	/**	 * 查找指定信息的结点,并返回其所处位置	 * 	 * @param data	 * @return	 */	public int searchNode(int data) {		int index = 0;		Node cur = head;		if (cur == null) { // 空链表			return -1;		} else if (cur == tail) { // 链表只有一个结点			if (cur.data == data) {				return index;			} else				return -1;		}		while (cur != tail) {			if (cur.data == data) {				return index;			} else {				cur = cur.next;				index++;			}		}		if (cur.data == data) { // 判断链表尾部			return index++;		} else {			return -1;		}	}	/**	 * 结点类	 * 	 * @author wky653	 *	 */	class Node {		int data;		Node next;		Node back;		public Node(int data) {			this.data = data;		}		public Node() {		}	}}/** * 测试类 *  * @author wky653 * */public class Test03 {	public static void main(String[] args) {		ThreeList list = new ThreeList();		list.addTailNode(14);		list.addTailNode(21);		list.addHeadNode(20);		list.addToNode(5, 30);		list.getList();		System.out.println("length=" + list.getLength());		list.addTailNode(50);		list.getList();		list.deleteNode(50);		list.getList();		list.deleteNode(15);		list.getList();		System.out.println("length=" + list.getLength());		list.clearList();		list.getList();		System.out.println("length=" + list.getLength());	}}

运行结果:

添加14成功!添加21成功!添加20成功!操作失败,超出链表长度!20,14,21length=3添加50成功!20,14,21,50操作成功!删除了 5020,14,21操作失败,没有找到 1520,14,21length=3链表为空!length=0


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表