java链表程序_java实现简单链表

package com.zte.test; public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub OperaNode op = new OperaNode(); op.addToHead(1); op.addToHead(2); op.addToTail(3); op.addToHead(5); op.printAll(); } } class Node { public int size; public Node next; public Node(int i) { this(i, null); } public Node(int i, Node n) { size = i; next = n; } } class OperaNode { private Node head, tail; public OperaNode() { head = tail = null; } /** * 判断链表是否为空 * * @return */ public boolean isEntry() { if (head == null) { return true; } else { return false; } } /** * 在链表的头部添加数据 * * @param l */ public void addToHead(int l) { head = new Node(l, head); if (tail == null) { tail = head; } } /** * 在链表的尾部添加数据 * * @param l */ public void addToTail(int l) { if (!isEntry()) { tail.next = new Node(l); tail = tail.next; } else { head = tail = new Node(l); } } /** * 删除链表头部数据 */ public int deleteToHead() { int el=head.size; if (head == tail) { head = tail = null; } else { head = head.next; } return el; } /** * 删除链表最后一个数据 */ public int deleteToTail() { int el = tail.size; if (head == tail) { head = tail = null; } else { Node temp; for (temp = head; temp.next != tail; temp = temp.next) ; tail = temp; tail.next = null; } return el; } /** * 打印链表中所有的数据 */ public void printAll() { Node temp; for (temp = head; temp != null ; temp = temp.next) { System.out.println(temp.size); } } /** * 删除链表中I的值 * * @param i */ public void delete(int i) { if (!isEntry()) { if (head == tail && i == head.size) { head = tail = null; } else if (i == head.size) { head = head.next; } } else { Node pred, tmp; for (pred = head, tmp = head.next; tmp != null && tmp.size != i; pred = pred.next, tmp = tmp.next) ; if (tmp != null) { pred.next = tmp.next; if (tmp == tail) { tail = pred; } } } } /** * 用于判断链表中是否有这个值 * * @param el * @return */ public boolean isIntList(int el) { Node tmp; for (tmp = head; tmp != null && tmp.size != el; tmp = tmp.next) ; return tmp != null; } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值