java 代码
- class IntNode {
- public int info;
-
- public IntNode next;
-
- public IntNode(int i) {
-
- this(i, null);
- }
-
- public IntNode(int i, IntNode n) {
- info = i;
- next = n;
- }
- }
-
-
-
-
- public class IntSLList {
- private IntNode head, tail;
-
- public IntSLList() {
- head = tail = null;
- }
-
- public boolean isEmpty() {
- return head == null;
- }
-
- public void addToHead(int el) {
- head = new IntNode(el, head);
-
-
- if (tail == null)
- tail = head;
- }
-
- public void addToTail(int el) {
- if (!isEmpty()) {
- tail.next = new IntNode(el);
- tail = tail.next;
- } else {
- head = tail = new IntNode(el);
- }
- }
-
- public int deleteFromHead() {
- int el = head.info;
- if (head == tail) {
- head = tail = null;
- } else
- head = head.next;
- return el;
- }
-
- public int deleteFromTail() {
- int el = tail.info;
- if (head == tail)
- head = tail = null;
- else {
- IntNode temp;
- for (temp = head; temp.next == tail; temp = temp.next)
-
- tail = temp;
- tail.next = null;
- }
- return el;
- }
-
- public void printAll() {
- for (IntNode temp = head; temp != null; temp = temp.next)
- System.out.println(temp.info + " ");
- }
-
- public boolean isInList(int el){
- IntNode temp;
- for(temp=head;temp!=null&&temp.info!=el;temp=temp.next);
- return temp!=null;
- }
-
- public void delete(int el) {
- if (!isEmpty())
- if (head == tail && head.info == el)
- head = tail = null;
- else if (el == head.info)
- head = head.next;
- else {
- IntNode pred, temp;
- for (pred = head, temp = head.next; temp != null
- && temp.info != el; pred = pred.next, temp = temp.next);
- if (temp != null) {
- pred.next = temp.next;
- if (temp == tail)
- tail = pred;
- }
- }
- }
- }
|
评论也很精彩,请点击查看精彩评论。欢迎您也添加评论。查看详细 >>
JavaEye推荐
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA
广州:急招 JAVA开发经理/系统架构师(10-15K/月)也招聘java程序员
与Hibernate之父面对面-4月19日 Gavin King上海交流研讨会
高薪工作机会 美国法国上海 15-20k/月 J2EE SA
文章来源:
http://xiaozhe.javaeye.com/blog/59214
posted on 2007-03-12 10:15
xiaozhe 阅读(88)
评论(0) 编辑 收藏