环形队列相关操作
注:需要判断条件,需要front指向队尾 rear指向队头
public boolean isFull() {
return (rear + 1) % maxsize == front;
}
public boolean isEmpty() {
return rear == front;
}
1.添加数据进队列
public void addQueue(int num) {
if (isFull()) {
System.out.println("队列已满...");
return;
}
arr[rear] = num;
rear = (rear + 1) % maxsize;
}
2.出队列
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空...");
}
int val = arr[front];
front = (front + 1) % maxsize;
return val;
}
3.获取有效数据个数
public int size() {
return (rear + maxsize - front) % maxsize;
}
4.显示队列头信息
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空...");
}
return arr[front];
}
5.显示所有数据
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]);
}
}
单链表
注:每个节点不一定连续存储
每个节点都包含data域和next域指向下一个节点
分情况带头节点或不带头节点
1.首先初始化一个头节点
//初始化头节点 不存储数据
private HeroNode head = new HeroNode(1, "", "");
2.添加节点到链表
public void add(HeroNode heroNode) {
//头节点不能动
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
3.遍历链表
public void list() {
//头节点不能动
HeroNode temp = head.next;
if (head.next == null) {
System.out.println("该链表为空");
return;
}
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
4.按照节点编号添加进链表
public void addByOrder(HeroNode heroNode) {
boolean flag = false;
HeroNode temp = head;
while (true) {
if (temp.next == null) break;
if (temp.next.no > heroNode.no) break;
else if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.printf("%d 编号已经存在\n", heroNode.no);
} else {
heroNode.next = temp.next; //已经到链表末尾
temp.next = heroNode;
}
}
5.修改节点信息
public void update(HeroNode heroNode) {
if (head.next == null) {
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) break;
if (temp.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.nickName = heroNode.nickName;
temp.name = heroNode.name;
} else System.out.printf("未找到 %d \n", heroNode.no);
}
6.删除节点
public void delete(int no) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) break;
if (temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else System.out.printf("此编号 %d 不存在", no);
}
7.获取链表长度
public int length() {
HeroNode temp = head;
if (temp.next == null) {
return 0;
}
int len = 0;
while (temp.next != null) {
len++;
temp = temp.next;
}
return len;
}
8.获取链表倒数第k个值
public HeroNode findLastNode(HeroNode head, int index) {
if (head == null)
return null;
int size = 4;
if (index <= 0 || index > size)
return null;
HeroNode cur = head.next;
for (int i = 0; i < size - index; i++) {
cur = cur.next;
}
return cur;
}
此处应该用方法获取size但由于太懒...
9.链表反转
public void reserveList() {
if (head.next == null || head.next.next == null) return;
//创建一个新链表
HeroNode reverseHead = new HeroNode(0, "", "");
//遍历之前的链表
HeroNode cur = head.next;
HeroNode next ;
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;//将下一个节点指向新链表最前端
reverseHead.next = cur;//将cur定为新头
cur = next;//后移
}
head.next = reverseHead.next; //将原链表头指向新链表
}
此处有必要说明一下
cur.next = reverseHead.next;
是将原来链表cur的下一个节点,指向新链表头节点的下一个节点。
然后再用
reverseHead.next = cur;
将cur节点连接到新链表
10.链表逆序打印
public void reservePrint(){
if (head.next==null) return;
Stack<HeroNode> stack = new Stack<>();
HeroNode temp=head.next;
while (temp!=null){
stack.push(temp);
temp=temp.next;
}
while (stack.size()>0) {
System.out.println(stack.pop());
}
}
11.按照编号合并两个链表
public static HeroNode mergeOrderList(HeroNode head1, HeroNode head2) {
HeroNode NewHead = new HeroNode(0, "", "");
HeroNode tail = NewHead;
NewHead.next=tail;
HeroNode temp1 = head1.next;
HeroNode temp2 = head2.next;
while (temp1 != null && temp2 != null) {
Integer flag=compareNode(temp1,temp2);
if (flag.equals(1)){ //节点1大于节点2
HeroNode next=temp2.next;
temp2.next=null;
tail.next=temp2;
tail=temp2;
temp2=next;
}else if (flag.equals(0)) {//节点1小于节点2
HeroNode next=temp1.next;
temp1.next=null;
tail.next=temp1;
tail=temp1;
temp1=next;
}
else { //相等
HeroNode next1=temp1.next;
HeroNode next2=temp2.next;
temp1.next=null;
tail.next=temp1;
tail=temp1;
temp1=next1;
temp2=next2
}
}
if(temp1 ==null) tail.next=temp2;
if(temp2 ==null) tail.next=temp1;
return NewHead;
}