⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bean.java

📁 输入字符串并按队列和链表输出再排序的小程序
💻 JAVA
字号:
import java.io.*;


public class bean
       {
		   public static void main(String args[])
		      {
                  linklist  list=new linklist("end");
                  int i,count;
                  String str="",str1="";

                  count=getnumber();
                  for(i=0;i<count;i++)
                  {   System.out.print("请输入第"+(i+1)+"个字符串:");
                      str=getstring();
					  list.insertatbegin(str);  //向空链表中插入用户希望的字符串数目
				      System.out.println(list.visitallnode());//遍历显示各节点

				  }

				  System.out.print("请输入您希望在哪个字符串后面插入:");
				  str=getstring();
				  System.out.print("请输入您要插入的字符串:");
				  str1=getstring();
				  list.insertafterid(str1,str);  //在指定位置插入字符串
				  System.out.println(list.visitallnode());//遍历插入操作过后的各节点
                  list.sort(count);
				  System.out.print("请输入您将要删除的字符串:");
				  str=getstring();
				  if(list.removeatid(str))
				          System.out.println(list.visitallnode());

				  else
				          System.out.println("链表中不存在这个字符串!"); //删除字符串

				    list.sortstring(count); //排序
				    //System.out.println(list.visitallnode());
				    System.out.println(list.m_firstnode.getdata());
				     System.out.println(list.visitallnode());

				  list.removeall(); //删除整个链表


			  }

		  public static int getnumber()//取得字符串个数
			  {
				  int count=0;
				  String s;

				  System.out.print("请输入您将输入字符串的个数:");

				  try{
					      BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
					 	  s=br.readLine();
					 	  count=Integer.parseInt(s);
					 }catch(IOException e){}

				  return count;

			  }
		 public  static String getstring()//取得字符串
		     {
				 String s="";

				 try{
				 	  BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
				 	  s=br.readLine();
					 }catch(IOException e){}

				 return s;
			 }


	   }




class linklist
	   {
		   Node m_firstnode;

		   linklist()//构造函数
		   		{
					m_firstnode=null;
				 }

		   linklist(String data)
		   		 {
					 m_firstnode=new Node(data);
				 }

		   String visitallnode()//遍历表中的每个节点
		         {
					 Node next=m_firstnode;
					 String s="";

					 while(next!=null)
					    {
							 s=s+next.getdata()+";  ";
							 next= next.getnext(); //使next指向下一个节点
						}

					 return s;
				 }
			void sortstring(int counta)
			{
				Node now=m_firstnode,now2=m_firstnode;
				int a=0,i,j;

               for(i=counta;i>0;i--)
               	   {
					 now=m_firstnode;
               		for(j=0;j<i;j++)
				      {
						  now2=now.getnext();
						  a=now.comparenode(now2);
				          if(a>0)
				          {
							  insertafterid(now.getdata(),now2.getdata());
				              removeatid(now.getdata());
					      }

				          now=now2;

				      }
			       }
			}//冒泡排序

			void sortstring2(int countb)
			{
				Node now=m_firstnode,now2,temp=m_firstnode;
				int a=0,i,j,k;
				String s;
				for(i=0;i<countb;i++)
				{
					now=m_firstnode;
					for(j=0;j<i;j++)
					  {
						  now2=now.getnext();
						  if(now.comparenode(now2)>0)
						     {
								 insertafterid(now.getdata(),now2.getdata());
								 removeatid(now.getdata());
						     }
						  now=now2;
					  }

				}
	 	    }



		   Node getprenode(Node now)//获得前一个节点
		         {
					 Node next=m_firstnode;
					 while(next.getnext()!=now)
					 	{
							next=next.getnext();
						}
					 return next;
				 }

			void sort(int countb)
			 {
			     int i;
			     for(i=0;i<777;i++)
			       sortstring(countb);
		     }

			void sort2(int countb)
			 {
			     int i;
			     for(i=0;i<777;i++)
			       sortstring(countb);
		     }


		   void insertatbegin(String data)//插入
		   	     {
					 if(m_firstnode==null)
					 		m_firstnode=new Node(data);
					 else
					 m_firstnode=new Node(data,m_firstnode);
			     }

		  void insertafterid(String data,String id)//插在字符串id的后面
		         {
					 Node next=m_firstnode;

					 if(next==null)
					 		m_firstnode = new Node(data);
					 else
					   {
						   while(next.getnext()!=null&&!(next.getdata().equals(id)))
						       next=next.getnext();
						   next.setnext(new Node(data,next.getnext()));
					   }
				 }

           boolean removeatid(String id) //删除
                 {
					 Node ahead =m_firstnode;
					 Node follow = ahead;

					 if(ahead==null)
					      return false;
					 else if(ahead.getdata().equals(id))
					       {
							   m_firstnode=m_firstnode.getnext();
							   return true;
						   }
					 else
					      {
							  ahead=ahead.getnext();
							  while(ahead.getdata()!=null)
							      {
									  if(ahead.getdata().equals(id))
									    {
											follow.setnext(ahead.getnext());
											return true;
										}
										follow=ahead;
										ahead=ahead.getnext();
								  }
							   return false;
						  }
				 }

		   void removeall()//删除所有节点
		         {
					 m_firstnode=null;
				 }



	   }



class queue extends linklist
      {
		  boolean isempty()//判断队列是否为空
		     {
				 if(m_firstnode==null)
				   return true;
				 else
				   return false;
			 }

		  void enqueque(String newdata)//加队操作
		     {
				 Node next=m_firstnode;
				  if(next==null)
				     m_firstnode=new Node(newdata);
				  else
				    {
						while(next.getnext()!=null)
						    next=next.getnext();
						 next.setnext(new Node(newdata));
					}
			 }

		  String dequeue()//减队操作
		     {
				 String data;
				 if(!isempty())
				   {
					   data=m_firstnode.getdata();
					   m_firstnode=m_firstnode.getnext();
					   return data;
				   }
				  else return null;
			 }
	  }









class Node
      {
		 private String m_string;  //节点保存的数据
		 private Node   m_next;    //节点中的指针属性,指向下一个node对象的对象引用

		  Node(String data)  //构造函数
		      {
				  m_string = data;
				  m_next = null;

			  }

		  Node(String data,Node next)
		      {
				  m_string=data;
				  m_next=next;
			  }

		 void setdata( String data) //修改数据
		      {
				  m_string=data;
			  }

		 String getdata()   //获得数据
		      {
				  return m_string;
			  }

		 void setnext(Node next) //修改节点中的指针
		 	  {
				  m_next=next;
			  }

		 Node getnext()  //获得节点中指针指向的对象引用
		 	  {
				  return m_next;
			  }

		 int comparenode(Node now)
		      {
				  int a;
				  a=m_string.compareTo(now.getdata());
				  return a;
			  }

		 void setnode(Node node)
		      {
				  m_string=node.getdata();
				  m_next=node.getnext();
			  }
		 Node getnumnext(int i)
		 	 {
		 		int j;
		 	    Node now=new Node("");
		 		for(j=0;j<i;j++)
		 		now=m_next.getnext();
		 		return now;

		      }


	  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -