zoukankan      html  css  js  c++  java
  • 实现一个链表和队列

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import org.apache.log4j.Logger;
    
    /*
     * 实现一个泛型链表类接口, 
    1.链表增加一个节点,
    2.指定位置增加一个节点
    3.删除指定位置的节点
    4.删除某种内容的所有节点
    
    加上日志
     * */
    
    public class LinkMe {
    
    	public static void main(String args[]){
    		
    		//BasicConfigurator.configure ();
    		//PropertyConfigurator.configure ( "D:/workspace/HomeWork/src/log4j.properties");
    		
    		MyLink<String> l = new MyLink<>("l","String");
    		l.insertFirst("zhang san");
    		l.insertFirst("li si");
    		l.insertFirst("wang wu");
    		l.insertFirst("zhao liu");
    		l.printMyLink();
    		l.del(4);
    		l.printMyLink();
    		
    		l.insert(3, "new Man");
    		l.printMyLink();
    		
    		l.delZD("li");
    		l.printMyLink();
    		
    		System.out.println();
    		MyLink<Integer> in = new MyLink<>("in","Integer");
    		in.insertFirst(1);
    		in.insertFirst(2);
    		in.insertFirst(3);
    		in.printMyLink();
    		
    		in.del(2);
    		in.printMyLink();
    		
    		in.insert(2, 5);
    		in.printMyLink();
    	}
    	
    }
    
    class MyLink<E>{
    	
    	Logger logger = Logger.getLogger(LinkMe.class.getName());
    	private LinkData<E> first;
    	private static int no;
    	
    	MyLink(String name,String lx){
    		first = null;
    		no=0;
    		logger.debug("---------------------------------------");
    		logger.debug(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
    		logger.debug("新建一个链表:"+name+",类型为"+lx);
    	}
    	
    	public void insertFirst(E aName){     //每次在链表尾部插入
    		LinkData<E> ld = new LinkData<>(aName);
    		ld.setNext(first);
    		first = ld;
    		no++;
    	}
    	
    	public void insert(int aI,E aName){  //指定位置插入节点
    		int i = 1;
    		LinkData<E> ld = new LinkData<>(aName);
    		LinkData<E> t = first;
    		logger.debug("在节点"+aI+"插入:"+ aName );
    		if (aI == 1 || aI < 1) {
    			insertFirst(aName);
    		} else if(aI>no) {
    			while(t.getNext()!=null){
    				t = t.getNext();
    			}
    			t.setNext(ld);
    			ld.setNext(null);
    		} else {
    			while(++i!=aI){
    				t = t.getNext();
    			}
    			LinkData<E> tl = t ;
    			ld.setNext(tl.getNext());
    			t.setNext(ld);
    		}
    		no++;
    	}
    	
    	public void delZD(String str){   //删除包含某种字符的所有节点
    		LinkData<E> t = first;
    		int i = 1;
    		while(t.getNext()!=null){
    			if(i==1&&((String)t.getName()).indexOf(str)!=-1){   //删除第一个
    				first = first.getNext();
    				t = first;
    				no--;
    			}
    			i++;
    			if(((String)t.getNext().getName()).indexOf(str)!=-1){     //删除非第一个
    				t.setNext(t.getNext().getNext());
    				no--;
    			}else{
    				t=t.getNext();
    			}
    		}
    	}
    	
    	public void del(int aI){  //删除指定节点
    		LinkData<E> t = first;
    		int i = 1;
    		boolean note = false;
    		while(t.getNext()!=null){
    			if(aI==1&&i==aI){   //删除第一个
    				first = first.getNext();
    				t = first;
    				note=true;
    				no--;
    			}
    			if(++i==aI){     //删除非第一个
    				t.setNext(t.getNext().getNext());
    				note=true;
    				no--;
    			}else{
    				t=t.getNext();
    			}
    		}
    		if(note){
    			logger.debug("删除节点"+aI);
    		}else{
    			logger.debug("删除节点"+aI);
    		}
    	}
    	
    	public void printMyLink(){
    		LinkData<E> t = first;
    		String str = "";
    		while(t!=null){
    			str += t.getName()+",";
    			t = t.getNext();
    		}
    		if(str!=""){
    			logger.debug(str+"  共"+no+"个节点");
    		}
    	}
    	
    }
    
    class LinkData<E>{    //定义节点
    	
    	private E name;
    	private LinkData<E> next;
    	
    	LinkData(E aName){
    		setName(aName);
    	}
    
    	public LinkData<E> getNext() {
    		return next;
    	}
    
    	public void setNext(LinkData<E> next) {
    		this.next = next;
    	}
    
    	public E getName() {
    		return name;
    	}
    
    	public void setName(E aName) {
    		this.name = aName;
    	}
    	
    }
    
    /*
     * 建一个队列类
     * */
    
    public class QueueMe {
        
        public static void main(String args[]){
            
            QueueList q = new QueueList(7);
            q.insert("a");
            q.insert("b");
            q.insert("c");
            q.insert("d");
            q.insert("e");
            q.insert("f");
            q.printQueue();
            
            q.del();
            q.del();
            q.printQueue();
            
            q.insert("a1");
            q.insert("b1");
            q.printQueue();
            
            q.del();
            q.del();
            q.del();
            q.del();
            q.del();
            q.del();
            q.del();
            q.del();
            q.printQueue();
        }
        
    }
    
    class QueueList{
        
        public int font;
        public int rear;
        public int size;
        public int num;
        public String[] arr;
        
        QueueList(int aI){
            size = aI;
            arr = new String[size];
            font = 0;
            rear = 0;
            num = 0;
        }
        
        public void insert(String aI){
            if (!isFull()) {
                if(arr[size-1]!=null){   //到栈顶后从底下插入
                    rear = 0;
                }
                arr[rear++] = aI;
                num++;
            }
        }
        
        public void del(){
            if(!isEmpty()){
                if (font==size) {
                    font = 0;
                }
                arr[font] = null;
                font++;
                num--;
            }
        }
        
        public boolean isFull(){
            if(num>=size){
                System.out.println("queue is full,can not insert.");
                return true;
            }else{
                return false;
            }
        }
        
        public boolean isEmpty(){
            if(num==0){
                System.out.println("queue is null,can not del.");
                return true;
            }else{
                return false;
            }
        }
        
        public void printQueue(){
            for(String i:arr)System.out.print(i+" ");
            System.out.println();
        }
        
    }
  • 相关阅读:
    one-hot 编码
    typeError:The value of a feed cannot be a tf.Tensor object.Acceptable feed values include Python scalars,strings,lists.numpy ndarrays,or TensorHandles.For reference.the tensor object was Tensor...
    tensorflw-gpu 运行 。py程序出现gpu不匹配的问题
    空间金字塔池化 ssp-net
    随便说一说bootstrap-table插件
    初识 .NET平台下作业调度器——Quartz.NET
    在VS中关于MySQL的相关问题
    教你记住ASP.NET WebForm页面的生命周期
    国内流行的两大开源.net微信公众平台SDK对比分析
    一次利用MSSQL的SA账户提权获取服务器权限
  • 原文地址:https://www.cnblogs.com/runwulingsheng/p/5208777.html
Copyright © 2011-2022 走看看