Java演算法(順序表操作執行個體)

來源:互聯網
上載者:User

Java演算法(順序表操作執行個體)

原始碼如下:

package com.xu.main;

import java.util.Scanner;

public class P2_1 {

/**
* @功能:順序表操作執行個體
* @作者:
* @日期:2012-10-15
*/
public static void main(String[] args) {

int i;
SLType SL = new SLType(); // 定義順序表變數
DATA pdata; // 定義結點儲存引用變數
String key; // 儲存關鍵字

System.out.println("順序表操作示範!");

SL.SLInit(SL); // 初始化順序表
System.out.println("初始化順序表完成");

Scanner input = new Scanner(System.in);

do {
System.out.println("輸入添加的結點(學好     姓名     年齡):");
DATA data = new DATA();
data.key = input.next();
data.name = input.next();
data.age = input.nextInt();

if (data.age != 0) {
if (SL.SLAdd(SL, data) == 0) // 若添加結點失敗
{
break; // 退出死迴圈
}
} else // 若年齡為0
{
break; // 退出死迴圈
}

} while (true);
System.out.println("順序表中的結點順序為:");
SL.SLAll(SL); // 顯示所有結點資料

System.out.println("要取出結點的序號:");
i = input.nextInt(); // 輸入結點的序號
pdata = SL.SLFindByNum(SL, i); // 按序號尋找結點
if (pdata != null) {
System.out.printf("第%d個結點為:(%s,%s,%d)\n", i, pdata.key, pdata.name,
pdata.age);
}

System.out.println("要尋找的結點的關鍵字:");
key = input.next(); // 輸入關鍵字
i = SL.SLFindByCount(SL, key); // 按關鍵字尋找
pdata = SL.SLFindByNum(SL, i); // 按序號尋找,返回結點引用
if (pdata != null) {
System.out.printf("第%d個結點為:(%s,%s,%d)\n", i, pdata.key, pdata.name,
pdata.age);
}
}
}

class DATA {
String key; // 節點的關鍵字
String name;
int age;
}

class SLType // 定義順序表的結構數組
{
static final int MAXLEN = 100;
DATA[] ListData = new DATA[MAXLEN + 1]; // 儲存順序表的結構數組
int ListLen; // 順序表已存節點的數量

void SLInit(SLType SL) // 初始化順序表
{
SL.ListLen = 0; // 初始化為空白表
}

int SLLength(SLType SL) {
return SL.ListLen; // 返回順序表的元素數量
}

int SLInsert(SLType SL, int n, DATA data) {
int i;
if (SL.ListLen >= MAXLEN) // 順序表節點數量已超過最大數量
{
System.out.println("順序表已滿,不能插入結點!");
return 0;
}
if (n < 1 || n > SL.ListLen - 1) // 插入節點序號不正確
{
System.out.println("插入元素序號錯誤,不能插入元素!");
return 0;
}
for (i = SL.ListLen; i >= n; i--) // 將順序表中的資料向後移動
{
SL.ListData[i + 1] = SL.ListData[i];
}
SL.ListData[n] = data; // 插入結點
SL.ListLen++; // 順序表結點數量增加1
return 1; // 成功插入,返回1
}

int SLAdd(SLType SL, DATA data) // 增加元素到順序表尾部
{
if (SL.ListLen >= MAXLEN) // 順序表已滿
{
System.out.println("順序表已滿,不能插入結點!");
return 0;
}
SL.ListData[++SL.ListLen] = data;
return 1;
}

int SLDelete(SLType SL, int n) // 刪除順序表中的資料元素
{
int i;
if (n < 1 || n > SL.ListLen + 1) // 刪除節點序號不正確
{
System.out.println("刪除結點序號錯誤,不能刪除元素!");
return 0;
}
for (i = n; i < SL.ListLen; i++) // 將順序表中的資料向前移動
{
SL.ListData[i] = SL.ListData[i + 1];
}
SL.ListLen--; // 順序元素數量減1
return 1; // 成功刪除,返回1
}

DATA SLFindByNum(SLType SL, int n) // 根據序號返回資料元素
{
if (n < 1 || n > SL.ListLen + 1) {
System.out.println("結點序號錯誤,不能返回結點!");
return null;
}
return SL.ListData[n];

}

int SLFindByCount(SLType SL, String key) // 按關鍵字查詢結點
{
int i;
for (i = 1; i <= SL.ListLen; i++) {
if (SL.ListData[i].key.compareTo(key) == 0) // 如果找到所需結點
{
return i;
}
}
return 0; // 搜尋整個表後仍沒有找到,則返回0
}

int SLAll(SLType SL) // 顯示順序表中的所有結點
{
int i;
for (i = 1; i <= SL.ListLen; i++) {
System.out.printf("(%s,%s,%d)\n", SL.ListData[i].key,
SL.ListData[i].name, SL.ListData[i].age);
}
return 0;
}
}

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.