java数据结构单链表的基本操作

文中内容来源于《数据结构 --Java语言描述》(第二版) 刘小晶 杜选 主编
此系列文章作为学校实验记录,若文中内容有误,请大家指出,谢谢

实验目的

1、掌握线性链表的操作特点,即指针是逻辑关系的映像。
2、掌握动态产生单链表的方法。
3、熟练掌握单链表的插入、删除操作特点,即指针赋值的先后次序。
4、熟练掌握单链表的取元素操作

实验内容

1、定义单链表类型并动态创建单链表;
2、实现单链表的取元素操作、插入操作和删除操作;
3、实现输出单链表中各元素值的操作;
4、将单链表中的最小元素移到最前面。

实验步骤

1、定义单链表节点类;
2、定义单链表类,并实现单链表的创建、插入、删除、取元素操作和将单链表中的最小元素移到最前面的操作;
3、从键盘上依次输入21、75、30、18、42、56,顺序或逆序创建单链表,并输出单链表中的各元素值;
4、分别在单链表的第3个位置和第9个位置插入67和10,给出插入成功或失败的信息,并输出单链表中的各元素值;
5、删除单链表中的第4个数据元素和第8个数据元素,给出删除成功或失败的信息,并输出单链表中的各元素值;
6、取单链表中的第5个数据元素和第7个数据元素;
7、将单链表中的最小元素移到最前面,并输出单链表中的各元素以检查操作是否正确实现。

源代码

//Node.java
package sjjg_test1;

public class Node {
	public Object data;
	public Node next;
	
	public Node(Object data, Node next) {
		this.data = data;
		this.next = next;
	}
	
	public Node(Object data) {
		this(data,null);
	}
	
	public Node() {
		this(null, null);
	}
}

//LinkList.java
package sjjg_test1;
import java.util.Scanner;
public class LinkList {
	public Node head;
	
	public LinkList() {
		head = new Node();
	}
	
	public LinkList(int n,boolean Order) throws Exception {
		this();
		if(Order)
			create1(n);
		else
			create2(n);
	}
	
	public void create1(int n) throws Exception {
		Scanner sc = new Scanner(System.in);
		for(int j=0;j<n;j++)
			insert(length(), sc.nextInt());
	}
	
	public void create2(int n) throws Exception {
		Scanner sc = new Scanner(System.in);
		for(int j=0;j<n;j++)
			insert(0, sc.nextInt());
	}
	
	public void clear() {
		head.data = null;
		head.next = null;
		System.out.println("clear successfully");
	}
	
	public boolean isEmpty() {
		return head.next == null;
	}
	
	public int length() {
		Node p = head.next;
		int length = 0;
		while(p != null) {
			p = p.next;
			++length;
		}
		return length;
	}

	public Object get(int i) throws Exception {
		Node p = head.next;
		int j = 0;
		while(p != null && j<i) {
			p = p.next;
			++j;
		}
		if(j>i || p==null) {
			throw new Exception("第" + i + "个元素不存在");
		}
		System.out.println("get successfully");
		return p.data;
	}

	public void insert(int i, Object x) throws Exception {
		Node p = head;
		int j = -1;
		while(p != null && j< i-1) {
			p = p.next;
			++j;
		}
		if(j>i-1 || p==null)
			throw new Exception("插入位置不合法");
		Node s = new Node(x);
		s.next = p.next;
		p.next = s;
		System.out.println("insert successfully");
	}
	
	public void remove(int i) throws Exception {
		Node p = head;
		int j = -1;
		while(p.next != null && j<i-1) {
			p = p.next;
			++j;
		}
		if(j>i-1 || p.next==null)
			throw new Exception("删除位置不合法");
		p.next = p.next.next;
		System.out.println("remove successfully");
	}

	public int indexOf(Object x) {
		Node p = head.next;
		int j = 0;
		while(p != null && !p.data.equals(x)) {//here
			p = p.next;
			++j;
		}
		if(p != null)
			return j;
		else
			return -1;
	}
	
	public void display() {
		Node node = head.next;
		while(node != null) {
			System.out.print(node.data + " ");
			node = node.next;
		}
		System.out.println();
	}
	
	public void tip() {
		System.out.println("请输入以下选项对应的数字:");
		System.out.println("1:插入");
		System.out.println("2:删除");
		System.out.println("3:获取元素");
		System.out.println("4:将最小值所在节点移动到表头");
		System.out.println("5:清空链表");
		System.out.println("6:判断链表是否为空");
		System.out.println("7:获取列表长度");
		System.out.println("8:按值查找元素下标");
		System.out.println("9:结束本次操作");
	}
	
	public void moveMin(Node head) throws Exception {
        //寻找最小
        Node current = head.next;
        Object min = current.data;
        while (current != null) {
                if ((int)current.data < (int)min) {
                        min = (int)current.data;
                }
                current=current.next;
        }
        Node previous = head;
        Node s;
        current = head.next;
        //两层循环为了移动所有的最小节点
        while(current!=null){
        	if((int)current.data != (int)min) {
        		previous = current;
                current = current.next;
        	} else {
        		previous.next=current.next;
                s = current;
                current=previous.next;
                s.next = head.next;
                head.next = s;
        	}
        }
	}
}

//LinkListTest.java
package sjjg_test1;
import java.util.Scanner;
public class LinkListTest {
	public static void main(String[] args) throws Exception {
		System.out.println("how many nodes do you want to creat in this LinkList:");
		Scanner input = new Scanner(System.in);
		int LinkListNum = input.nextInt();
		System.out.println("please input "+LinkListNum+" numbers to creat a LinkList:"); 
		LinkList a = new LinkList(LinkListNum,true);
		System.out.println("the numbers of LinkList are following:");
		a.display();
		Object shuzi;
		int xiabiao,sum,pEnd;
		String judge;
		while(true) {
			a.tip();
			int choice = input.nextInt();
			switch (choice) {
				case 1://insert
					System.out.println("请输入插入元素个数");
					sum = input.nextInt();
					System.out.println("请输入要插入元素的位置和值");
					while(sum>0) {
						xiabiao = input.nextInt();
						shuzi = input.nextInt();
						try {
							a.insert(xiabiao, shuzi);
							a.display();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						sum--;
					}
					break;
				case 2://delete
					System.out.println("请输入删除个数");
					sum = input.nextInt();
					System.out.println("请输入删除元素位置");
					while(sum>0) {
						xiabiao = input.nextInt();
						try {
							a.remove(xiabiao);
							a.display();
						} catch (Exception e) {
							e.printStackTrace();
						}
						sum--;
					}
					break;
				case 3://get
					System.out.println("请输入所取元素个数");
					sum = input.nextInt();
					while(sum>0) {
						System.out.println("请输入所取元素位置");
						xiabiao = input.nextInt();
						try {
							System.out.println(a.get(xiabiao));
						} catch (Exception e) {
							e.printStackTrace();
						}
						sum--;
					}
					break;
				case 4://moveMin
					a.moveMin(a.head);
					a.display();
					break;
				case 5:
					a.clear();
					break;
				case 6:
					System.out.println(a.isEmpty());
					break;
				case 7:
					System.out.println("链表长度为:"+a.length());
					break;
				case 8:
					System.out.println("请输入查找元素个数");
					sum = input.nextInt();
					while(sum>0) {
						System.out.println("请输入要查找元素");
						shuzi = input.nextInt();
						System.out.println(shuzi+"的下标为:"+a.indexOf(shuzi));
						sum--;
					}
					break;
				case 9:
					break;
				default:
					System.out.println("输入有误");
					break;
			}
			pEnd = 1;
			while(pEnd>0) {
				System.out.println("是否要结束本条链表的操作?y/n");
				judge = input.next();
				if(judge.equals("y"))
					break;
				else if(judge.equals("n"))
					pEnd = -1;
				else
					System.out.println("输入有误");
			}
			if(pEnd==1) {
				break;
			}
		}
		input.close();
		System.out.println("end of program");	
	}
}

运行结果

how many nodes do you want to creat in this LinkList:
6
please input 6 numbers to creat a LinkList:
21 75 30 18 42 56
insert successfully
insert successfully
insert successfully
insert successfully
insert successfully
insert successfully
the numbers of LinkList are following:
21 75 30 18 42 56 
请输入以下选项对应的数字:
1:插入
2:删除
3:获取元素
4:将最小值所在节点移动到表头
5:清空链表
6:判断链表是否为空
7:获取列表长度
8:按值查找元素下标
9:结束本次操作
1
请输入插入元素个数
2
请输入要插入元素的位置和值
2 67
insert successfully
21 75 67 30 18 42 56 
8 10
java.lang.Exception: 插入位置不合法
是否要结束本条链表的操作?y/n
	at sjjg_test1.LinkList.insert(LinkList.java:76)
	at sjjg_test1.LinkListTest.main(LinkListTest.java:33)
n
请输入以下选项对应的数字:
1:插入
2:删除
3:获取元素
4:将最小值所在节点移动到表头
5:清空链表
6:判断链表是否为空
7:获取列表长度
8:按值查找元素下标
9:结束本次操作
2
请输入删除个数
2
请输入删除元素位置
3
remove successfully
21 75 67 18 42 56 
7
java.lang.Exception: 删除位置不合法
	at sjjg_test1.LinkList.remove(LinkList.java:91)
	at sjjg_test1.LinkListTest.main(LinkListTest.java:49)
是否要结束本条链表的操作?y/n
n
请输入以下选项对应的数字:
1:插入
2:删除
3:获取元素
4:将最小值所在节点移动到表头
5:清空链表
6:判断链表是否为空
7:获取列表长度
8:按值查找元素下标
9:结束本次操作
3
请输入所取元素个数
2
请输入所取元素位置
4
get successfully
42
请输入所取元素位置
6
java.lang.Exception:6个元素不存在
是否要结束本条链表的操作?y/n
	at sjjg_test1.LinkList.get(LinkList.java:60)
	at sjjg_test1.LinkListTest.main(LinkListTest.java:65)
n
请输入以下选项对应的数字:
1:插入
2:删除
3:获取元素
4:将最小值所在节点移动到表头
5:清空链表
6:判断链表是否为空
7:获取列表长度
8:按值查找元素下标
9:结束本次操作
4
18 21 75 67 42 56 
是否要结束本条链表的操作?y/n
y
end of program

  • 11
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值