操作系統之資源分配
main包
public class OsResourceAllocationMain {
public static void main(String[] args) {
OsResourceAllocation osResourceAllocation = new OsResourceAllocation();
osResourceAllocation.select();
}
}
dao包
package com.qzz.os_resource_allocation;
import java.util.Scanner;
public class OsResourceAllocation {
Scanner scanner = new Scanner(System.in);
class Node{
int data;
Node next;
public Node() {
this.data = data;
this.next = null;
}
public Node(int val) {
this.data = val;
this.next = null;
}
}
public Node head;
public OsResourceAllocation() {
this.head = new Node();
}
public void select() {
while(true) {
System.out.println();
System.out.println("------------操作系統內存分配------------\n");
System.out.print("請選擇: \n ===================== \n 1、輸入系統資源 \n 2、清除系統資源 \n 3、查看內存資源 \n ===================== \n 4、首次適應算法(FF)\n 5、循環首次適應算法(NF) \n 6、最佳適應算法(BF)\n 7、最壞適應算法(WF)\n ===================== \n\n");
Scanner in = new Scanner(System.in);
int choose = in.nextInt();
Scanner scan = new Scanner(System.in);
switch(choose){
case 1:
insertTail();
break;
case 2:
deleteALL1();
break;
case 3:
print1();
break;
case 4:
ff();
break;
case 5:
nf();
break;
case 6:
bf();
break;
case 7:
wf();
break;
}
}
}
//1.頭插法
public void insertHead(int val) {
Node cur = new Node(val);
cur.next = this.head.next;
this.head.next = cur;
}
//2.尾插法
public void insertTail() {
System.out.println("請輸入內存容量數: ");
int n = scanner.nextInt();
System.out.println("請輸入容量中資源大小");
for (int i = 0; i < n; i++) {
Node cur = this.head;
while (cur.next != null) {//找到最後一個爲空的節點
cur = cur.next;
}
int val = scanner.nextInt();
Node endNode = new Node(val);//生成新節點
cur.next = endNode;//和之前找到的後面爲空的節點鏈接
}
System.out.println("容量設置完成");
new Scanner(System.in).nextLine();
select();
}
//3.得到單鏈表長度(數據節點個數)
public int getLength() {
Node cur = this.head;
int count = 0;
while (cur.next != null) {
cur = cur.next;
count++;
}
return count;
}
//清空內存容量
public void deleteALL1() {
Node cur = this.head.next;
Node curNext= null;
if (this.head.next == null) {
System.out.println("目前並沒有內存容量");
new Scanner(System.in).nextLine();
select();
}
this.head.next = null;
System.out.println("清除成功");
new Scanner(System.in).nextLine();
select();
}
//面板中用的打印,打印結束後返回到主面板
public void print1() {
Node cur = this.head;
boolean flg = false;
while (cur.next != null) {
System.out.print(cur.next.data + " ");
cur = cur.next;
flg = true;
}
if (flg == false) {
System.out.println("目前並沒有內存容量");
new Scanner(System.in).nextLine();
select();
}else {
new Scanner(System.in).nextLine();
System.out.println();
select();
}
}
//算法中用的打印
public void print() {
Node cur = this.head;
boolean flg = false;
while (cur.next != null) {
System.out.print(cur.next.data + " ");
cur = cur.next;
flg = true;
}
if (flg == false) {
System.out.println("目前並沒有內存容量");
new Scanner(System.in).nextLine();
select();
}else {
System.out.println();
}
}
//最佳適應算法
public void bf() {
System.out.println("目前系統所有的資源爲:");
print();
System.out.println();
System.out.println("請輸入所需要的資源數大小:");
int val = scanner.nextInt();
System.out.println("所需的資源容量爲:" + val);
Node cur = this.head;
while (cur.next != null) {
if (cur.next.data < val) {
cur = cur.next;
}else {
int result = cur.next.data - val;
cur.next.data = result;
System.out.println("申請到資源爲:" + val);
System.out.println("分配後的資源:");
print();
System.out.println();
System.out.println("更新分配後的資源:");
sortBubble();
print();
System.out.println();
System.out.println("-------是否需要繼續分配--------");
System.out.println("請輸入: 是 / 否");
String n = scanner.next();
if (n.equals("是")){
bf();
}else {
System.out.println("分配結束");
new Scanner(System.in).nextLine();
select();
}
}
}
System.out.println("沒有合適的資源");
new Scanner(System.in).nextLine();
select();
}
//最壞適應算法
public void wf() {
System.out.println("目前系統所有的資源爲:");
sortBubble1();
print();
System.out.println();
System.out.println("請輸入所需要的資源數大小:");
int val = scanner.nextInt();
System.out.println("所需的資源容量爲:" + val);
Node cur = this.head;
while (cur.next != null) {
if (cur.next.data < val) {
cur = cur.next;
}else {
int result = cur.next.data - val;
cur.next.data = result;
System.out.println("申請到資源爲:" + val);
System.out.println("分配後的資源:");
print();
System.out.println();
System.out.println("更新分配後的資源:");
sortBubble1();
print();
System.out.println();
System.out.println("-------是否需要繼續分配--------");
System.out.println("請輸入: 是 / 否");
String n = scanner.next();
if (n.equals("是")){
wf();
}else {
System.out.println("分配結束");
new Scanner(System.in).nextLine();
select();
}
}
}
System.out.println("沒有合適的資源");
new Scanner(System.in).nextLine();
select();
}
//首次適應
public void ff() {
Node cur = this.head;
System.out.println("目前系統所有的資源爲:");
print();
System.out.println();
System.out.println("請輸入所需要的資源數大小:");
int val = scanner.nextInt();
System.out.println("所需的資源容量爲:" + val);
while (cur.next != null) {
if (cur.next.data < val) {
cur = cur.next;
}else {
int result = cur.next.data - val;
cur.next.data = result;
System.out.println("申請到資源爲:" + val);
System.out.println("分配後的資源:");
print();
System.out.println();
System.out.println("更新分配後的資源:");
print();
System.out.println();
System.out.println("-------是否需要繼續分配--------");
System.out.println("請輸入: 是 / 否");
String n = scanner.next();
if (n.equals("是")){
ff();
}else {
System.out.println("分配結束");
new Scanner(System.in).nextLine();
select();
break;
}
break;
}
}
System.out.println("沒有合適的資源");
new Scanner(System.in).nextLine();
select();
}
//最佳適應
public void nf() {
Node cur = this.head;
System.out.println("目前系統所有的資源爲:");
print();
System.out.println();
System.out.println("請輸入所需要的資源數大小:");
int val = scanner.nextInt();
System.out.println("所需的資源容量爲:" + val);
boolean flg = true;
while (cur.next != null) {
if (cur.next.data < val) {
cur = cur.next;
}else {
flg = false;
int result = cur.next.data - val;
cur.next.data = result;
System.out.println("申請到資源爲:" + val);
System.out.println("分配後的資源:");
print();
System.out.println();
System.out.println("更新分配後的資源:");
print();
System.out.println();
System.out.println("-------是否需要繼續分配--------");
System.out.println("請輸入: 是 / 否");
String n = scanner.next();
if (n.equals("是")){
System.out.println("請輸入所需要的資源數大小:");
int newVal = scanner.nextInt();
val = newVal;
continue;
}else {
System.out.println("分配結束");
new Scanner(System.in).nextLine();
select();
break;
}
}
}
if (flg == false) {
System.out.println("第一次從頭到尾結束");
nf();
}
System.out.println("沒有合適的資源");
new Scanner(System.in).nextLine();
select();
}
//對鏈表進行排序(冒泡)從小到大
public void sortBubble() {
if (this.head.next == null) {
System.out.println("目前並沒有內存容量");
new Scanner(System.in).nextLine();
select();
}
Node cur;
Node runByRun;
int temp;
for (cur = this.head.next; cur.next != null; cur = cur.next) {
for (runByRun = this.head.next; runByRun.next != null; runByRun = runByRun.next) {
if (runByRun.data > runByRun.next.data) {
temp = runByRun.data;
runByRun.data = runByRun.next.data;
runByRun.next.data = temp;
}
}
}
}
//對鏈表進行排序(冒泡) 從大到小
public void sortBubble1() {
if (this.head.next == null) {
System.out.println("目前並沒有內存容量");
new Scanner(System.in).nextLine();
select();
}
Node cur;
Node runByRun;
int temp;
for (cur = this.head.next; cur.next != null; cur = cur.next) {
for (runByRun = this.head.next; runByRun.next != null; runByRun = runByRun.next) {
if (runByRun.data < runByRun.next.data) {
temp = runByRun.data;
runByRun.data = runByRun.next.data;
runByRun.next.data = temp;
}
}
}
}
}
2018.12.05/週三
by 922