代码笔记:环形队列和单链表

环形队列:

import java.util.Scanner;

public class CircleArrayQueueDemo {

	public static void main(String[] args) {
		//创建一个环形队列
				CircleArray queue = new CircleArray(4);
				char key = ' ';//接收用户输入
				Scanner scanner = new Scanner(System.in);
				boolean loop = true;
				while(loop) {
					System.out.println("s:显示队列");
					System.out.println("e:退出");
					System.out.println("a:添加数据");
					System.out.println("g:获取数据");
					System.out.println("h:查看队头");
					key = scanner.next().charAt(0);
					switch(key) {
					case 's':
						queue.showQueue();
						break;
					case 'a':
						System.out.println("输一个数:");
						int value = scanner.nextInt();
						queue.addQueue(value);
						break;
					case 'g':
						try {
							int res = queue.getQueue();
							System.out.println("取出的数据是:" + res);
						} catch (Exception e) {
							System.out.println(e.getMessage());
						}
						break;
					case 'h':
						try {
							int res = queue.headQueue();
							System.out.println("队头的数据是:" + res);
						} catch (Exception e) {
							System.out.println(e.getMessage());
						}
						break;
					case 'e':
						scanner.close();
						loop = false;
						break;
					default:
						break;
					}
				}
				System.out.println("程序退出。。");

	}
}
class CircleArray{
	private int maxSize;//最大容量
	private int front;//队头  初始0 指向第一个元素
	private int rear;//队尾   初始0  指向最后一个元素的下一位置
	private int[] arr;//存放数组,模拟队列
	
	//创建队列构造器
	public CircleArray(int arrMaxSize) {
		maxSize = arrMaxSize;
		arr = new int [maxSize];
	}
	
	//判断队列是否满
	public boolean isFull() {
		return (rear + 1) % maxSize == front;
	}
	
	//判断队列是否空
	public boolean isEmpty() {
		return front == rear;
	}
	
	//队列添加数据
	public void addQueue(int n) {
		if(isFull()) {
			System.out.println("队列已满,无法添加数据");
			return;
		}
		arr[rear] = n;
		rear = (rear + 1) % maxSize;
	}
	
	//获取队列数据,出队列
	public int getQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列空,不能取数据");
		}
		int value = arr[front];
		front = (front + 1) % maxSize;
		return value;
	}
	
	//显示
	public void showQueue() {
		if(isEmpty()) {
			System.out.println("队列空,无数据");
			return;
		}
		for(int i = front; i < front + size(); i++) {
			System.out.printf("arr[%d] = %d\n",i % maxSize, arr[i % maxSize]);
		}
	}
	
	//求出当前队列有效数据个数
	public int size() {
		return (rear + maxSize - front) % maxSize;
	}
	
	//显示队头数据
	public int headQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列空,无数据");
		}
		return arr[front];
	}
}

单链表:

public class SingleLinkedListDemo {
	public static void main(String[] args) {
		//创建节点
		HeroNode heroNode1 = new HeroNode(1, "皮卡丘", "电耗子");
		HeroNode heroNode2 = new HeroNode(2, "杰尼龟", "呆头龟");
		HeroNode heroNode3 = new HeroNode(3, "妙蛙种子", "蒜头王八");
		HeroNode heroNode4 = new HeroNode(4, "小火龙", "猛火");
		
		//创建链表
		SingleLinkedList singleLinkedList = new SingleLinkedList();
		//第一种加入
//		singleLinkedList.add(heroNode1);
//		singleLinkedList.add(heroNode2);
//		singleLinkedList.add(heroNode3);
//		singleLinkedList.add(heroNode4);
		//第二种加入
		singleLinkedList.addByOrder(heroNode1);
		singleLinkedList.addByOrder(heroNode4);
		singleLinkedList.addByOrder(heroNode4);
		singleLinkedList.addByOrder(heroNode3);
		singleLinkedList.addByOrder(heroNode2);
		singleLinkedList.list();
		
		HeroNode newHeroNode = new HeroNode(2, "杰尼龟111", "呆头龟111");
		singleLinkedList.update(newHeroNode);
		System.out.println("修改后的链表:");
		singleLinkedList.list();
		
		singleLinkedList.delete(1);
		singleLinkedList.delete(2);
		singleLinkedList.delete(4);
		System.out.println("删除后链表为:");
		singleLinkedList.list();
	}
}	

class SingleLinkedList{
	//先初始化头节点,不要动
	private HeroNode head = new HeroNode(0, "", "");
	
	
	//添加节点到单向链表
	public void add(HeroNode heroNode) {
		//因为head不能动,因此需要辅助变量temp
		HeroNode temp = head;
		//遍历链表,找到最后
		while(true) {
			if(temp.next == null) {
				break;
			}
			temp = temp.next;
		}
		temp.next = heroNode;
	}
	
	//第二种添加方式  排名
	public void addByOrder(HeroNode heroNode) {
		HeroNode temp = head;
		boolean flag = false;//标志添加的编号是否存在,默认为false
		while(true) {
			if(temp.next == null) {//说明temp已到链表最后
				break;
			}
			if(temp.next.no > heroNode.no) {//找到位置
				break;
			} else if(temp.next.no == heroNode.no) {//说明编号已存在
				flag = true;
				break;
			}
			temp = temp.next;
		}
		//判断flag的值
		if(flag == true) {
			System.out.println(heroNode.no + "编号已存在,不能加入");
		} else {
			//插入到temp后面
			heroNode.next = temp.next;
			temp.next = heroNode;
		}
	}
	
	//修改节点信息,no不能修改
	//1.根据no修改
	public void update(HeroNode newHeroNode) {
		//判断是否为空
		if(head.next == null) {
			System.out.println("链表空");
			return;
		}
		HeroNode temp = head.next;
		boolean flag = false;//表示是否找到该节点
		while(true) {
			if(temp == null) {//遍历完
				break;
			}
			if(temp.no == newHeroNode.no) {
				//找到
				flag = true;
				break;
			}
			temp = temp.next;
		}
		if(flag) {
			temp.name = newHeroNode.name;
			temp.nickname = newHeroNode.nickname;
		} else {//没有找到
			System.out.println("没有找到节点"+ newHeroNode.no);
		}
	}
	
	//删除节点
	public void delete(int no) {
		//判断是否为空
				if(head.next == null) {
					System.out.println("链表空");
					return;
				}
				HeroNode temp = head;
				boolean flag = false;//表示是否找到该节点
				while(true) {
					if(temp == null) {//遍历完
						break;
					}
					if(temp.next.no == no) {
						//找到
						flag = true;
						break;
					}
					temp = temp.next;
				}
				if(flag) {
					temp.next = temp.next.next;
				} else {//没有找到
					System.out.println("没有找到节点"+ no);
				}
	}
	
	//遍历显示
	public void list() {
		//判断链表是否为空
		if(head.next == null) {
			System.out.println("链表为空");
			return;
		}
		//因为头节点不能动,所以需要辅助变量来遍历
		HeroNode temp = head.next;
		while(true) {
			//判断链表是否到最后
			if(temp == null) {
				break;
			}
			//输出节点的信息
			System.out.println(temp);
			//后移
			temp = temp.next;
		}
		
	}
}

class HeroNode{
	public int no;
	public String name;
	public String nickname;
	public HeroNode next;
	//构造器
	public HeroNode(int no, String name, String nickname) {
		this.no = no;
		this.name = name;
		this.nickname = nickname;
	}
	
	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值