線性表java代碼 線性表基本操作代碼
java關(guān)于線性表的編程
package?Test;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileReader;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.io.Reader;
import?java.io.StringBufferInputStream;
import?java.io.StringReader;
import?java.util.Scanner;
class?Test?{
private?static?Node?firstList,?secondList,?resultList;
private?static?void?input(Node?head)?throws?Exception?{
int?a;
int?b;
int?i;
Node?p?=?head;
BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(
System.in));
//?讀取一行信息
String?input?=?reader.readLine();
//?以空格為分隔符,轉(zhuǎn)換成數(shù)組
String[]?numbers?=?input.split("?");
for(i=0;inumbers.length;){
a?=?Integer.parseInt(numbers[i]);
b?=?Integer.parseInt(numbers[i+1]);
p.next?=?new?Node(a,?b);
p?=?p.next;
i+=2;
}
}
public?static?void?main(String[]?args)?throws?Exception?{
firstList?=?new?Node();
secondList?=?new?Node();
resultList?=?new?Node();
Node?p=resultList;
System.out.println("輸入第一個多項(xiàng)式");
input(firstList);
System.out.println("輸入第二個不等式");
input(secondList);
while(firstList.next!=nullsecondList.next!=null){
if(firstList.next.zssecondList.next.zs){
p.next=new?Node(firstList.next.xs,firstList.next.zs);
p=p.next;
firstList=firstList.next;
}else?if(firstList.next.zssecondList.next.zs){
p.next=new?Node(secondList.next.xs,secondList.next.zs);
p=p.next;
secondList=secondList.next;
}else{
p.next=new?Node(firstList.next.xs+secondList.next.xs,firstList.next.zs);
p=p.next;
firstList=firstList.next;
secondList=secondList.next;
}
}
if(firstList!=null){
p.next=firstList.next;
}
if(secondList!=null){
p.next=secondList.next;
}
p=resultList;
while(p.next!=null){
System.out.print(p.next.xs+"x^"+p.next.zs+"+");
p=p.next;
}
System.out.println();
}
}
public?class?Node?{
public?int?xs;//系數(shù)
public?int?zs;//指數(shù)
public?Node?next=null;//指向下一個
public?Node(int?a,int?b)?{
xs=a;
zs=b;
}
public?Node(){
}
}
答案
輸入第一個多項(xiàng)式
5 4 3 2 1 1
輸入第二個不等式
4 4 3 2 1 1
9x^4+6x^2+2x^1
JAVA如何添加判斷線性表為0否則輸出線性表長度
package?ba;
//線性表抽象數(shù)據(jù)類型的java接口定義
interface?List
{
?public?boolean?isEmpty();?????????//接口方法1:判斷線性表是否為空
?public?void?insert(int?i?,int?element)?throws?Exception;?//接口方法2:在線性表的指定位置插入數(shù)據(jù)元素
?public?int?remove(int?i)??throws?Exception;?????//接口方法3:刪除線性表中指定位置的數(shù)據(jù)元素
?public?int?getData(int?i)?throws?Exception;?????//接口方法4:獲取線性表中指定位置的數(shù)據(jù)元素
?public?int?length();??????????//接口方法5:獲取線性表的長度(數(shù)據(jù)元素的個數(shù))
}
//順序表類SeqList,實(shí)現(xiàn)線性表接口
class?SeqList??implements?List?
{
?//在實(shí)現(xiàn)接口的類中定義成員變量
?private?int[]?listArray;??????//定義數(shù)組:listArray,用于保存線性表中的數(shù)據(jù)元素
?final?int?defaultSize=10;??????//定義常量:defaultSize,用于指定線性表的默認(rèn)長度
?private?int?size;?????????//定義變量:size,用于描述線性表中的數(shù)據(jù)元素的個數(shù)(線性表的長度)
?private?int?maxSize;???????//定義變量:maxSize,用于描述線性表中最大數(shù)據(jù)元素的個數(shù)(線性表的最大長度)
??
?//在實(shí)現(xiàn)接口的類中定義成員方法
?//構(gòu)造方法
?//定義函數(shù):initiate()
?public?void?initiate(int?sz)
?{
??maxSize=sz;???????????
??size=0;
??listArray=new??int[sz];??//創(chuàng)建一個整型數(shù)組?。?!
?}
?public?SeqList()?????//不帶參數(shù)的構(gòu)造方法
?{
??initiate(defaultSize);
?}
?public?SeqList(int?capacity)??//帶一個參數(shù)的構(gòu)造方法
?{
??initiate(capacity);
?}
?
?//成員方法:清空線性表
?public?void?clear()
?{
??this.size=0;
?}
?
?//實(shí)現(xiàn)?接口中定義的方法
?
?public?boolean?isEmpty()?????????//接口方法1:判斷線性表是否為空
?{
??return?this.size==0;?//說明:==:是邏輯判斷是否相等,如果相等返回值布爾值:真?true(1),否則返回假?false?0
?}
?
?public?void?insert(int?index?,int?element)?throws?Exception?//接口方法2:在線性表的指定位置插入數(shù)據(jù)元素
???????????//加載異常事務(wù)處理方法????
?{?????
??if(size==maxSize)
??{
???throw?new?Exception("順序表已滿,無法插入!");?//如果程序中需要將存在的異常事件拋出,則必須事先加載異常處理的方法
???//拋出異常
??}
??if(index0?||?indexsize)
??{
???throw?new?Exception("參數(shù)錯誤!");
??}
??for(int?j=size;jindex;j--)
??{
???listArray[j]=listArray[j-1];
??}
??listArray[index]=element;
??size++;?
?}
?
?public?int?remove(int?i)??throws?Exception?????//接口方法3:刪除線性表中指定位置的數(shù)據(jù)元素
?{
??if(size==0)
??{
???throw?new?Exception("順序表已空,無法刪除!");
??}
??if(i0?||?isize-1)
??{
???throw?new?Exception("參數(shù)錯誤!");
??}
??int?it?=listArray[i];
??for(int?j=i;jsize-1;j++)
??{
???listArray[j]=listArray[j+1];
??}
??size--;
??return?it;??//將刪除數(shù)據(jù)元素后新的數(shù)組??
?}
?//獲取線性表中指定位置的數(shù)據(jù)元素
?public?int?getData(int?i)?throws?Exception?????//接口方法4:獲取線性表中指定位置的數(shù)據(jù)元素
?{
??return?listArray[i];
?}
?//求線性表的長度
?public?int?length()??????????//接口方法5:獲取線性表的長度(數(shù)據(jù)元素的個數(shù))
?{
??return?size;
?}
}
public?class?test?{???????//定義public類:(1)在創(chuàng)建項(xiàng)目時指定的類
?public?static?void?main(String?args[])??????//(2)main方法應(yīng)當(dāng)在public類中
?{
??SeqList?seqList?=?new?SeqList(100);
??//定義類的對象。在定義類的對象時,通過調(diào)用構(gòu)造方法對類的成員變量進(jìn)行賦值
??//如果一個類中存在多個構(gòu)造方法時,稱為構(gòu)造方法重載。
??//調(diào)用構(gòu)造方法時,由系統(tǒng)根據(jù)所調(diào)用的構(gòu)造方法的參數(shù)個數(shù)和數(shù)據(jù)類型自動調(diào)用與之相匹配的構(gòu)造方法
??int?n=10;
??try???????//try語句中的內(nèi)容是指:需要對程序運(yùn)行的異常事件進(jìn)行捕獲
??{
???for(int?i=0;i=n;i++)
???{
????seqList.insert(i,(i+1));?//通過類的對象調(diào)用類的成員方法
???}
???seqList.remove(4);
???for(int?i=0;in;i++)
???{
????System.out.println(seqList.getData(i)+"?");
???}
???//請?jiān)诖颂砑优袛嗑€性表是否為空,如果為空,則提示用戶線性表為空,否則輸出線性表的長度
???
??}
??
??catch(Exception?e)???//catch語句的作用是:將try語句中捕獲的異常內(nèi)容拋出
??{
???System.out.println(e.getMessage());
??}
??
?}
}
如何在這程序中添加
java建立一個線性表
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
int numLength = 10;
int deleteNum = 5;
ListInteger list = new ArrayListInteger();
init(numLength,list);
delete(deleteNum,list);
print(list);
}
private static void print(ListInteger list) {
for(int i=0;ilist.size();i++){
System.out.print(list.get(i) +"\t");
}
}
private static void delete(int deleteNum,ListInteger list) {
for (int i=0;ilist.size();i++){
if((int)list.get(i)==deleteNum){
list.remove(i);
}
}
}
private static void init(int numLength,ListInteger list) {
for(int i=1;i=numLength;i++){
list.add(i);
}
}
}
//當(dāng)然你要是把你的代碼貼上來就更好了,可以幫忙找出問題,另外也可以知道你對java了解到哪個程度了呵,給出的幫助可能更實(shí)際一些
上一篇:包含Java11查看源代碼的詞條
欄 目:Java編程
下一篇:沒有了
本文標(biāo)題:線性表java代碼 線性表基本操作代碼
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/17244.html
您可能感興趣的文章
- 04-04包含Java11查看源代碼的詞條
- 04-04控制臺輸入java代碼 java的控制臺輸入命令
- 04-04java代碼注釋的位置 java代碼注釋的位置是什么
- 04-04java重定向和轉(zhuǎn)發(fā)代碼 java 請求轉(zhuǎn)發(fā)和重定向使用場景
- 04-04公司網(wǎng)站模板java代碼 javaweb網(wǎng)站模板
- 04-03java過濾標(biāo)點(diǎn)符號代碼 java過濾標(biāo)點(diǎn)符號代碼大全
- 04-03java代碼執(zhí)行 Java代碼執(zhí)行過程
- 04-03java成績分代碼 java成績表
- 04-03住宿服務(wù)系統(tǒng)java代碼 住宿服務(wù)系統(tǒng)java代碼
- 04-03java常見錯誤代碼 java常見錯誤提示


閱讀排行
本欄相關(guān)
- 04-04線性表java代碼 線性表基本操作代碼
- 04-04包含Java11查看源代碼的詞條
- 04-04控制臺輸入java代碼 java的控制臺輸入
- 04-04java代碼注釋的位置 java代碼注釋的位
- 04-04java重定向和轉(zhuǎn)發(fā)代碼 java 請求轉(zhuǎn)發(fā)和
- 04-04公司網(wǎng)站模板java代碼 javaweb網(wǎng)站模板
- 04-03java過濾標(biāo)點(diǎn)符號代碼 java過濾標(biāo)點(diǎn)符
- 04-03java代碼執(zhí)行 Java代碼執(zhí)行過程
- 04-03java成績分代碼 java成績表
- 04-03住宿服務(wù)系統(tǒng)java代碼 住宿服務(wù)系統(tǒng)
隨機(jī)閱讀
- 01-10C#中變量、常量、枚舉、預(yù)處理器指令
- 08-05織夢dedecms默認(rèn)采集功能只能采集第一
- 01-10如何基于JavaScript判斷圖片是否加載完
- 01-10華為筆試算法題匯總
- 01-10深入解析C#中的交錯數(shù)組與隱式類型的
- 08-05織夢文檔arcrank和ismake兩個字段的含義
- 01-10iSlider手機(jī)端圖片滑動切換插件使用詳
- 01-10C#網(wǎng)絡(luò)請求與JSON解析的示例代碼
- 01-11織夢dede模板內(nèi)怎么加入php代碼的方法
- 08-05織夢autoindex從02開始到10的寫法