導航:首頁 > 源碼編譯 > 什麼是循環演算法

什麼是循環演算法

發布時間:2023-07-23 19:30:03

1. 循環鏈表演算法原理和順序表演算法原理

其中線性表內含順序表和鏈表。順序表較為簡單,僅僅需要一個數組就可以完成,當然最好也要添加一個表示長度的數據成員,如size。而鏈表,顯然比較多變看,小可不才,用了將近三天的時間才能明白,不能不說見笑於大方之家;皆因鏈表之中還有循環鏈表,雙向鏈表,雙向循環鏈表。好了,言歸正傳:循環鏈表的程序奉上:
鏈表,不過增(insert)刪(delete)改(update)查(select)而已。在於Java程序中,還要加上構造(Java有垃圾回收機制,故沒有析構,但可以手動回收)。先看代碼如下:
1、關於構造函數,小生理解到:需要什麼樣的初始化,就寫出什麼樣的構造函數,當然,沒有時類也會人性化的構造一個空的構造函數;本人就節點有一個構造函數
2、在方法中,注意index的具體代表就行。其中,在找上一節點時,很多重復了,可以另外寫入一個函數中。
3、最後只是一個測試形式的,可以自己設置
4、自認為一個比較簡單的程序了
packagelink;

classNode {
publicintnum;
publicNodenext;

publicNode(intnum, Node next) {
this.num= num;
this.next= next;
}
}

publicclassCycleList {
publicNodehead;
publicintsize;

publicvoidinsertHead(intelement){//在頭結點的地方插入 if(size== 0){
head=newNode(element,head);
}else{
Node no =head;
head=newNode(element, no);
}
size++;
}

publicvoidinsert(intindex,intelement) {//插入元素
if(size== 0) {
head=newNode(element,head);
}else{
if(index < 0) {
index = 0;
}
if(index >size) {
index =size;
}
Node no1 =head;
for(inti = 0; i < index - 1; i++) {
no1 = no1.next;
}
Node no2 =newNode(element, no1.next);
no1.next= no2;
}
size++;
}

publicvoiddelete(intindex) { //刪除函數
if(index < 0) {
index = 0;
}
if(index >size) {
index =size;
}
Node no3 =head;
for(inti = 0; i < index - 1; i++) {
no3 = no3.next;
}
no3.next= no3.next.next;

size--;
}

publicvoidselect() { //查詢所有元素
intsizelong =size;
Node no4 =head;
for(inti = 0; i < sizelong; i++) {
System.out.print(no4.num);
no4 = no4.next;
}
}

publicvoipdate(intindex,intelement){//更換index位置的內容
Node no7 =head;
for(inti=0; i<index-1; i++){
no7 = no7.next;
}
no7.num= element;
}

publicvoidsel(intindex){ //查詢index位置的內容
Node no8 =head;
for(inti=0; i<index-1; i++){
no8 = no8.next;
}
System.out.println(no8.num);
}

publicstaticvoidmain(String[] args) {
CycleList cl =newCycleList();
cl.insert(0, 4); // index代表第幾個後面,當然,第0個後面,即為插頭節點
cl.insert(2, 2); //無論插入還是刪除
cl.insert(3, 5); //更改很准確
cl.insert(4, 6); //查詢單個也是可以的
cl.insert(5, 9);

cl.select();
System.out.print(" ----");
cl.insert(0, 8);
cl.select();
System.out.print(" ----");
cl.insertHead(3);
cl.select();
System.out.println("------");
cl.delete(3);
cl.select();
System.out.println("---------");
cl.update(1, 1);
cl.select();
System.out.print("----");
cl.sel(0);
}
}
實現順序表各種基本運算的演算法
2007-04-29 12:44:04| 分類:數據結構程序| 標簽:|字型大小大中小 訂閱

//實現順序表各種基本運算的演算法.h#include<stdio.h>
#include<malloc.h>
#define MaxSize 50
typedef char ElemType;typedef struct
{ElemType elem[MaxSize]; //順序表類型定義
int length;
}SqList;void InitList(SqList *&L) //初始化順序表L
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}void DestroyList(SqList *L) //釋放順序表L.
{
free(L);
}int ListEmpty(SqList *L) //判斷順序表L是否為空表.
{
return(L->length==0);
}
int ListLength(SqList *L)
{
return(L->length);
} void DispList(SqList *L)
{
int i;
if(ListEmpty(L))return;
for(i=0;i<L->length;i++)
printf("%c ",L->elem[i]);
printf("\n");
}int GetElem(SqList *L,int i,ElemType &e)
{
if(i<1 || i>L->length)
return 0;
e=L->elem[i-1];
return 1;}int LocateElem(SqList *L,ElemType e)
{int i=0;
while(i<L->length && L->elem[i]!=e)i++;
if(i>=L->length)
return 0;
else
return i+1;
}int ListInsert(SqList *&L,int i,ElemType e)
{//在順序表L中第i個位置上插入元素e.
int j;
if(i<1 || i>L->length+1)
return 0;
i--;
for(j=L->length;j>i;j--)
L->elem[j]=L->elem[j-1];
L->elem[i]=e;
L->length++;
return 1;
}int ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if(i<1 || i>L->length)
return 0;
i--;
e=L->elem[i];
for(j=i;j<L->length-1;j++)
L->elem[j]=L->elem[j+1];
L->length--;
return 1;
}
//實現順序表各種基本運算的演算法.cpp#include"實現順序表各種基本運算的演算法.h"
#include<stdio.h>void main()
{
SqList *L;
ElemType e;
printf("(1)初始化順序表L:\n");
InitList(L);
printf("(2)依次採用尾插法插入a,b,c,d,e元素\n");
ListInsert(L,1,'a');
ListInsert(L,2,'b');
ListInsert(L,3,'c');
ListInsert(L,4,'d');
ListInsert(L,5,'e');
printf("(3)輸出順序表L:");
DispList(L);
printf("(4)順序表L長度=%d\n",ListLength(L));
printf("(5)順序表L為%s\n",(ListEmpty(L)?"空":"非空"));
GetElem(L,3,e);
printf("(6)順序表L的第3個元素=%c\n",e);
printf("(7)元素a的位置=%d\n",LocateElem(L,'a'));
printf("(8)在第4個元素位置上插入f元素\n");
ListInsert(L,4,'f');
printf("(9)輸出順序表L:");
DispList(L);
printf("(10)刪除L的第3個元素\n");
ListDelete(L,3,e);
printf("(11)輸出順序表L:");
DispList(L);
printf("(12)釋放順序表L\n");
DestroyList(L);
}

閱讀全文

與什麼是循環演算法相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:142
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:732
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:301
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:141
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:890
app轉賬是什麼 瀏覽:163