今天是:
 
您现在的 位置: 浩特个人网 >> 文章频道 >> 编程源码 >> JAVA源代码 >> 正文 本站公告:
注册会员体验更多精彩!  [2006-08-17 09:54:52]      浩特个人网介绍  [2006-08-17 09:52:30]      浩特个人网全新改版!  [2006-07-24 13:27:58]
  java实现的栈和队列    5星级
java实现的栈和队列
[ 作者:无     来源:搜集整理     点击数:     更新时间:2006-04-03     文章录入:
【字体: 字体颜色
class Node  
  {  
  Object data;  
  Node next;//指向下一个结点  
  } 

import java.io.*;  
  public class List   
  {  
  /*用变量来实现表头*/  
  private Node Head=null;  
  private Node Tail=null;  
  private Node Pointer=null;  
  private int Length=0;  
public void deleteAll()  
  /*清空整个链表*/  
  {  
  Head=null;  
  Tail=null;  
  Pointer=null;  
  Length=0;  
  }  
  public void reset()  
  /*链表复位,使第一个结点  
成为当前结点*/  
  {  
  Pointer=null;  
  }  
  public boolean isEmpty()  
  /*判断链表是否为空*/  
  {  
  return(Length==0);  
  }  
  public boolean isEnd()  
  /*判断当前结点是否  
为最后一个结点*/  
  {  
  if(Length==0)  
  throw new java.lang.NullPointerException();  
  else if(Length==1)  
  return true;  
  else  
  return(cursor()==Tail);  
  }  
  public Object nextNode()  
  /*返回当前结点的下一个结点的值,  
并使其成为当前结点*/  
  {  
  if(Length==1)  
  throw new java.util.NoSuchElementException();  
  else if(Length==0)  
  throw new java.lang.NullPointerException();  
  else  
  {  
  Node temp=cursor();  
  Pointer=temp;  
  if(temp!=Tail)  
  return(temp.next.data);  
  else  
  throw new java.util.NoSuchElementException();  
  }  
  }  
  public Object currentNode()  
  /*返回当前结点的值*/  
  {  
  Node temp=cursor();  
  return temp.data;  
  }  
    
  public void insert(Object d)  
  /*在当前结点前插入一个结点,  
并使其成为当前结点*/  
  {  
  Node e=new Node(d);  
  if(Length==0)  
  {  
  Tail=e;  
  Head=e;  
  }  
  else  
  {  
  Node temp=cursor();  
  e.next=temp;  
  if(Pointer==null)  
  Head=e;  
  else  
  Pointer.next=e;  
  }  
  Length++;  
  }  
  public int size()  
  /*返回链表的大小*/  
  {  
  return (Length);  
  }  
  public Object remove()  
  /*将当前结点移出链表,下一个结点成为当前结点,如果移出的结点是最后一个结点,则第一个结点成为当前结点*/  
  {  
  Object temp;  
  if(Length==0)  
  throw new java.util.NoSuchElementException();  
  else if(Length==1)  
  {  
  temp=Head.data;  
  deleteAll();  
  }  
  else  
  {  
  Node cur=cursor();  
  temp=cur.data;  
  if(cur==Head)  
  Head=cur.next;  
  else if(cur==Tail)  
  {  
  Pointer.next=null;  
  Tail=Pointer;  
  reset();  
  }  
  else  
  Pointer.next=cur.next;  
  Length--;  
  }  
  return temp;  
  }  
  private Node cursor()  
  /*返回当前结点的指针*/  
  {  
  if(Head==null)  
  throw new java.lang.NullPointerException();  
  else if(Pointer==null)  
  return Head;  
  else  
  return Pointer.next;  
  }  
  public static void main(String[] args)  
  /*链表的简单应用举例*/  
  {  
  List a=new List ();  
  for(int i=1;i<=10;i++)  
  a.insert(new Integer(i));  
  System.out.println(a.currentNode());  
  while(!a.isEnd())  
  System.out.println(a.nextNode());  
  a.reset();  
  while(!a.isEnd())  
  {  
  a.remove();  
  }  
  a.remove();  
  a.reset();  
  if(a.isEmpty())  
  System.out.println("There is no Node in List  \n");  
  System.in.println("You can press return to quit\n");  
  try  
  {  
  System.in.read();  
//确保用户看清程序运行结果  
  }  
  catch(IOException e)  
  {}  
  }  
  }  
  class Node  
  /*构成链表的结点定义*/  
  {  
  Object data;  
  Node next;  
  Node(Object d)  
  {  
  data=d;  
  next=null;  
  }  
  }  
   
  class Node  
  {  
  Object data;  
  Node next;  
  Node previous;  
  Node(Object d)  
  {  
  data=d;  
  next=null;  
  previous=null;  
  }  
  }  
  • 上一篇文章: MIDP1.0小游戏入门-五子棋1.1

  • 下一篇文章: 没有了
  • 发表评论   告诉好友   打印此文  收藏此页  关闭窗口  返回顶部
     最新5篇热点文章
     面对面有时不一定是相遇(...
     Windows Vista 主题安装包...
     两首《你到底爱谁》歌词:...
     四首《童话》歌词:光良、...
     情人节手机短信搞笑大全
     
     最新5篇推荐文章
     面对面有时不一定是相遇(...
     现在的女孩子满脑子都是色...
     千万别找太聪明的美眉做女...
     fireworks logo教程
     电话英语生活应用12大类
     
     相 关 文 章

      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)


    制作:笨超 Copyright©2006-2007
    陇ICP备05005205号