線性表結構

來源:互聯網
上載者:User

標籤:

定義:按照一定的邏輯順序依次存放在電腦的這一組連續的儲存單元。

以數組的方式存放資料,其中在插入或者刪除節點元素是需要判斷該節點的位置。

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.print("順序表已滿,不能插入結點!\n");
return 0; //返回0表示插入不成功
}
if(n<1 || n>SL.ListLen-1) //插入結點序號不正確
{
System.out.print("插入元素序號錯誤,不能插入元素!\n");
return 0; //返回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.print("順序表已滿,不能再添加結點了!\n");
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.print("刪除結點序號錯誤,不能刪除結點!\n");
return 0; //刪除不成功,返回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.print("結點序號錯誤,不能返回結點!\n");
return null; //不成功,則返回0
}
return SL.ListData[n];
}

int SLFindByCont(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.