单链表与双链表队列

    一,链表分类:单链表与双向链表;

    二,链表定义:数据域(data)与指针域(node)的下一个节点(next),头节点(head),上一个节点(pre);

    三,定义类;

         (1)Node类:传值与重写get,set方法;

         (2)LinkNodeList类;将对数据的增删差改的基本操作方法或属性;

         (3)main();主函数类,进行数据的调用;

    四,单链表的示例代码;

public class LinkListNode {
	// 定义单链表的头接点;
	private CreateNode head;
	CreateNode pre;

	// 添加
	public void add(String data) {
		CreateNode node = new CreateNode(data);
		if (head == null) {
			head = node;
		} else {
			// 赋值新节点为头节点
			CreateNode newNode = head;
			// 找到最后一个节点;
			while (newNode.getNext() != null) {
				newNode = newNode.getNext();
			}
			// 设置新节点为最后一个节点
			newNode.setNext(node);
		}
	}

	// 插入
	public void insert(int index, String data) {
		// 首先,判断是否参数正确
		if (index < 0 || index >= size()) {
			try {
				throw new Exception("索引位置错误");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			CreateNode node = new CreateNode(data);
			if (index == 0) {
				node.setNext(head);
				head = node;
				return;
			}

			int count = 0;
			CreateNode newNode = head;
			// 找到下一个节点的位置
			while (count != (index - 1)) {
				newNode = newNode.getNext();
				count++;

			}
			// 获取下一个节点的位置,给当前节点
			CreateNode nextNode = newNode.getNext();
			newNode.setNext(node);
			node.setNext(nextNode);
		}

	}

	public int size() {
		// 定义一个计数器
		int count = 0;
		// 判断头节点是否为空
		if (head == null) {
			return count;
		} else {
			// 当前头节点为空
			CreateNode newNode = head;
			count++;
			while (newNode.getNext() != null) {
				newNode = newNode.getNext();
				count++;

			}
			// 返回当前节点,为所有节点的数值
			return count;
		}

	}

	// 查找
	public String get(int index) {
		// 判断索引位置是否正确
		if (index < 0 || index >= size()) {
			try {
				throw new Exception("索引位置错误");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		} else {
			int count = 0;
			CreateNode newNode = head;
			// 找到最后一个节点
			while (count != index) {
				newNode = newNode.getNext();
				count++;
			}
			// 返回数据域的值
			return newNode.getData();
		}
	}

	// 删除
	public void delete(int index) {
		if (index < 0 || index >= size()) {
			try {
				throw new Exception("索引位置错误");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			if (index == 0) {
				// 当元素为0时,找头节点的下一个,并赋值给头节点
				CreateNode newNode = head;
				pre = newNode.getNext();
				head = pre;
				return;
			}

			int count = 0;
			CreateNode newNode = head;
			// 当前下标减1,继续找下一个下标
			while (count != index - 1) {
				newNode = newNode.getNext();
				count++;
			}
			// 初始位置指向下一个值
			CreateNode current = newNode.getNext();
			// 下一个位置的初始位置
			CreateNode next = current.getNext();
			// 设置上一个位置的下一个的值
			newNode.setNext(next);
		}
		return;
	}

	// 更新
	public void updata(int index, String data) {

		if (index < 0 || index >= size()) {
			try {
				throw new Exception("索引位置错误");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {

			if (index + 1 == 0) {
				// 当元素为0时,找头节点的下一个,并赋值给头节点
				CreateNode newNode = head;
				// pre = newNode.getNext();
				head = newNode;
				return;
			}

			int count = 0;
			CreateNode newNode = head;
			// 找到下一个节点的位置
			while (count != (index)) {
				newNode = newNode.getNext();
				count++;

			}
			// 获取下一个节点的位置,给当前节点
			newNode.setData(data);
			return;
		}

	}
}


test()类

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkListNode t = new LinkListNode();

		t.add("元素1");
		t.add("元素2");
		t.add("元素3");
		t.add("元素4");
		t.insert(0, "元素0");
		t.updata(4, "e");
		t.delete(0);

		for (int i = 0; i < t.size(); i++) {
			System.out.print("第" + i + "元素为:" + t.get(i) + "\n");

		}

	}


测试运行结果为;



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值