免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

通過數據結構實現(xiàn)簡易通訊錄

AddressBookTest 是測試類

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站建設、網站制作、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的平陸網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!

package MyADB;import java.util.InputMismatchException;

import java.util.Scanner;class InstructionsMistake extends Exception {

public InstructionsMistake(String mo) {

super(mo);

public class AddressBookTest {

public static void main(String[] args) throws InstructionsMistake{

MyAddressBook AdB = new MyAddressBook();

Scanner rb = new Scanner(System.in);

String name = new String();

String cell = new String();

boolean isNum = false;

int co = 0;

System.out.println("******** 簡易通訊錄管理程序 ********");

System.out.println(" 1. 插入新的聯(lián)系人    ");

System.out.println(" 2. 查詢已有聯(lián)系人    ");

System.out.println(" 3. 更改已有聯(lián)系人    ");

System.out.println(" 4. 刪除已有聯(lián)系人    ");

System.out.println(" 5. 顯示已有聯(lián)系人    ");

System.out.println(" 6. 退出通訊錄程序    ");

do {

System.out.print("\n******** 請輸入你所要操作的代碼 :");

try {

co = rb.nextInt();

} catch (InputMismatchException e) {

throw new InstructionsMistake(" 輸入的操作代碼有誤 ");

}  

if (co == 1) {

System.out.print(" 請輸入新的聯(lián)系人姓名 :");

name = rb.next();

System.out.print(" 請輸入新的聯(lián)系人手機號碼 :");

cell = rb.next();

// 運用正則表達式對手機號碼的輸入進行規(guī)范

isNum = cell.matches("^1[3|5|7|8]\\d{9}$");

while (!isNum) {

System.out.print(" 輸入的手機號碼有誤,請重新輸入 :");

cell = rb.next();

isNum = cell.matches("^1[3|5|7|8]\\d{9}$");

}

AdB.addAdB(name, cell);

System.out.println(" 聯(lián)系人 " + name + " 成功錄入 ");

} else if (co == 2) {

System.out.print(" 請輸入所查詢的聯(lián)系人姓名 :");

name = rb.next();

String str = AdB.searchAdB(name);

if (str == null) {

System.out.println(" 找不到 " + name + " 聯(lián)系人 ");

} else {

System.out.println(" 查找成功 ");

System.out.println(" 該聯(lián)系人的手機號碼為 :" + str);

}

} else if (co == 3) {

System.out.print(" 請輸入要更改的聯(lián)系人姓名 :");

name = rb.next();

String str = AdB.searchAdB(name);

if (str == null) {

System.out.println(" 找不到 " + name + " 聯(lián)系人 ");

} else {

System.out.println("1/ 更改聯(lián)系人的姓名 ");

System.out.println("2/ 更改聯(lián)系人的手機號碼 ");

System.out.print(" 請輸入操作代碼 :");

int cot = rb.nextInt();

if (cot == 1) {

System.out.print(" 請輸入該聯(lián)系人的新姓名 :");

String toName = rb.next();

toName = AdB.ChangeAdBName(name,toName);

System.out.println(" 該聯(lián)系人姓名成功更改為 :" + toName);

} else if (cot == 2) {

System.out.print(" 請輸入該聯(lián)系人的新手機號碼 :");

String toCell = rb.next();

isNum = toCell.matches("^1[3|5|7|8]\\d{9}$");

while (!isNum) {

System.out.print(" 輸入的手機號碼有誤,請重新輸入 :");

toCell = rb.next();

isNum = toCell.matches("^1[3|5|7|8]\\d{9}$");

}

toCell = AdB.ChangeAdBCell(name,toCell);

System.out.println(" 該聯(lián)系人手機號碼成功更改為 :" + toCell)

} else if (co == 4) {

System.out.print(" 輸入要刪除的聯(lián)系人姓名 :");

name = rb.next();

AdB.deleteAdB(name);

} else if (co == 5) {

System.out.println(AdB);

} else if (co == 6){

break;

}

} while (co != 6);

System.out.println("******** 成功退出通訊錄程序 ********");

MyAddressBook 類

package MyADB;

// 雙向

public class MyAddressBook {// 通訊錄

protected Node first;// 第一個聯(lián)系人 ( 通訊錄的管理工具 )

protected Node last;// 最后一個聯(lián)系人

protected int size = 0;// 聯(lián)系人的個數

// 通訊錄中的單個聯(lián)系人

protected class Node {// 聯(lián)系人 ( 內部類 )

Node prev;// 上一個聯(lián)系人

Node next;// 下一個聯(lián)系人

public String name;// 姓名

public String cell;// 手機號碼

public Node(String name, String call) {

this.name = name;

this.cell = call;

// 尾插法

public void addAdB(String name, String call) {

Node node = new Node(name, call);// 新建一個聯(lián)系人

if (size == 0) {

this.first = node;

this.last = node;

} else {

// 把新增聯(lián)系人作為之前最后的聯(lián)系人的下一個

this.last.next = node;

// 把最后一個聯(lián)系人作為新增聯(lián)系人的上一個聯(lián)系人

node.prev = this.last;

// 把新增聯(lián)系人作為通訊錄的最后一個

this.last = node;

}size++;

}// 查找聯(lián)系人

public String searchAdB(String name) {

if (size == 0) {

System.out.println(" 通訊錄為空 ");

return null;

}Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

if (current.next == null) {

// 找不到返回空

return null;

current = current.next;

// 找到后返回該聯(lián)系人的手機號碼

return current.cell;

}// 返回聯(lián)系人自身

public Node retuName(String name) {

if (size == 0) {

System.out.println(" 通訊錄為空 ");

return null;

}Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

current = current.next;

return current;

// 更改聯(lián)系人姓名

public String ChangeAdBName(String name, String toName) {

Node current = retuName(name);

current.name = toName;

return current.name;

}// 更改聯(lián)系人手機號碼

public String ChangeAdBCell(String name, String toCell) {

Node current = retuName(name);

current.cell = toCell;

return current.cell;

}// 刪除指定聯(lián)系人

public void deleteAdB(String name) {

if (size == 0) {

System.out.println(" 通訊錄為空 ");

return;

}// 找到被刪除的聯(lián)系人

Node current = this.first;

for (int i = 0; i < size; i++) {

if (!current.name.equals(name)) {

if (current.next == null) {

System.out.println(" 找不到 " + name + " 聯(lián)系人 ");

return;

current = current.next;

// 進行刪除操作

if (current == first) {// 刪除通訊錄中頂部的一個聯(lián)系人

this.first = current.next;

this.first.prev = null;

} else if (current == last) {// 刪除通訊錄中最底部的一個聯(lián)系人

this.last = current.prev;// 將該聯(lián)系人的上一個聯(lián)系人作為通訊錄的最后一個聯(lián)系人

this.last.next = null;// 最后一個聯(lián)系人對下一個聯(lián)系人引用為空

} else // 將該聯(lián)系人的下一個聯(lián)系人作為該聯(lián)系人的上一個聯(lián)系人的 next

current.next英鎊符號https://www.gendan5.com/currency/GBP.html

current.prev.next = current.next; // 將該聯(lián)系人的上一個聯(lián)系人作為該聯(lián)系人的下一個聯(lián)系人的 prev

current.next.prev = current.prev; }size--; System.out.println(" 已將 " + name + " 移除通訊錄 ");

}public String toString() {

if (size == 0) {

return " 通訊錄為空 ";

}// 拼接字符串

StringBuilder sbBuilder = new StringBuilder(size * 2 + 1);

Node current = this.first;

int counet = 0;

while (current != null) {

sbBuilder.append(" 聯(lián)系人姓名為 :" + current.name + "\n");

sbBuilder.append(" 該聯(lián)系人手機號碼為 :" + current.cell + "\n");

if (counet != size - 1) {

sbBuilder.append("\n");

counet++;

}current = current.next;

}return sbBuilder.toString();

文章名稱:通過數據結構實現(xiàn)簡易通訊錄
URL鏈接:http://m.newbst.com/article38/jeecpp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網頁設計公司網站內鏈、移動網站建設、網站建設、服務器托管搜索引擎優(yōu)化

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站建設