JAVA 单链表

import java.util.Scanner;
public class Hello {  
    public static void main(String[] args){
    	LinkList L=new LinkList();
    	Scanner reader=new Scanner(System.in);
    	System.out.println("请输入数据,最后按233结束");
    	int data=reader.nextInt();
    	while(data!=233) {
    		L.add(data);
    		data=reader.nextInt();
    	}
    	System.out.print("你输入的数据为:");
    	L.show();
    	boolean flag=true;
    	while(flag) {
    	   	System.out.println("请输入你的操作:1、插入 2、删除 3、查找 4、查看 5、结束 ");
    	   	int choose=reader.nextInt();
    		switch(choose) 
    		{
    		case 1: System.out.println("请输入你要插入的位置和数值"); 
    				int i=reader.nextInt(); 
    				data=reader.nextInt(); 
    				L.insert(i,data); 
    				System.out.print("你输入的数据为:"); 
    				L.show(); 
    				break;
    		case 2: System.out.println("请输入你要删除的数值");  
    				data=reader.nextInt(); 
    				L.delete(data); 
    				System.out.print("你输入的数据为:");
    				L.show(); 
    				break;
    		case 3: System.out.println("请输入你要查找的数值");  
    				data=reader.nextInt(); 
    				L.lookfor(data); 
    				System.out.print("你输入的数据为:");
    				L.show();
    				break;
    		case 4: System.out.print("你输入的数据为:");
    				L.show(); 
    				break;
    		case 5: flag=false; break;
    		}
    	}
    	reader.close();
/*    	data=reader.nextInt();
    	L.delete(data);
    	data=reader.nextInt();
    	L.lookfor(data);
    	L.show();*/
    }  
}  
class Node{
	int data;
	Node next;
	Node(int data){
		this.data=data;
		next=null;
	}
}
class LinkList{
	int length=0;
	Node first;
	Node last;
///////////////////////////////////////////////
//	队尾插入
//////////////////////////////////////////////
	void add(int data){
		Node newnode=new Node(data);
		length++;
		if(length==1) {
			first=newnode;
			last=newnode;
		}
		last.next=newnode;
		last=newnode;
	}
///////////////////////////////////////////////
//  任意位置插入
//////////////////////////////////////////////	
	int insert(int i,int data) {
		if(i<=0 || i>length+1) {
			System.out.println("插入不合法");
			return 0;
		}
		Node newnode=new Node(data);
		if(i==1) {
			newnode.next=first;
			first=newnode;
			length++;
			return 0;
		}
		else if(i==length+1) {
			last.next=newnode;
			last=newnode;
			length++;
			return 0;
		}
		Node cur=first.next;
		Node pre=first;
		for(int j=1;j<i-1;j++) {
			cur=cur.next;
			pre=pre.next;
		}
		pre.next=newnode;
		newnode.next=cur;
		length++;
		return 0;
	}
///////////////////////////////////////////////
//	删除
//////////////////////////////////////////////	
	int delete(int data) {
		if(length==0) {                               //链表为空
			System.out.println("链表为空");
			return 0;
		}
		Node cur=first;		
		if(length==1) {               //链表只有一个结点
			if(cur.data==data) {        //如果找到
				first=null;
				length--;
				return 0;
			}
			System.out.println("你要删除的数据不存在");
			return 0;
		}
		Node pre=cur;
		if(cur.data==data) {      //如果是第一个数据
			length--;
			first=cur.next;
			return 0;
		}
		cur=cur.next;
		while(cur!=null) {    
			if(cur.data==data) {
				pre.next=cur.next;
				length--;
				return 0;
			}
			cur=cur.next;
			pre=pre.next;
		}
		System.out.println("你要删除的数据不存在");
		return 0;
	}
///////////////////////////////////////////////
//	查找
//////////////////////////////////////////////
	int lookfor(int data) {
		if(length==0) {
			System.out.println("链表为空");
			return 0;
		}
		Node here=first;
		while(here!=null) {
			if(here.data==data) {
				System.out.println("存在");
				return 0;
			}
			here=here.next;
		}
		System.out.println("不存在");
		return 0;
	}
///////////////////////////////////////////////
//	查看
//////////////////////////////////////////////
	void show() {
		Node here=first;
		while(here!=null) {
			System.out.print(here.data+" ");
			here=here.next;
		}
		System.out.print("链表长度为:"+length);
		System.out.println();
	}
}

猜你喜欢

转载自blog.csdn.net/abc1498880402/article/details/80489113