package liu;
public class node
public void setdata(int data)
public int getdata()
public node getnext()
public void setnext(node next)
}package liu;
public class singlelistnode
//向鍊錶尾部新增資料
public void add(int n)
//當原鍊錶中有資料時
else
}//向鍊錶中指定位置新增資料
public void insert(int position,int data)
else
else if(position>length)else
pre.setnext(temp);
temp.setnext(aft);
}length++;
}pos=1;
}public int getlength()
//刪除鍊錶中資料
public void dele(int position)
else if(length==1)
else
pre.setnext(aft.getnext());
}pos=1;
length--;
}//對鍊錶中資料進行排序
public void sort()
pre=pre.getnext();
aft=aft.getnext();}}
}//對鍊錶中資料進行反序
public void antisortdisplay()
node temp=anti;
while(temp!=null)
}public void display()
}//主函式測試
public static void main(string args)
}
單鏈表插入刪除
在鍊錶的插入刪除操作上理解起來比順序表更為容易,其不需要變動在i位置前的所有的元素,只需要修改節點指標即可。插入 設在鍊錶的i位置插入新元素,設i 1節點的指標域為p,設插入的節點指標域為s,所以插入操作應該為 s next p next 將s的字尾改為p的字尾,p的字尾是原來的第i個點的指標域,將...
單鏈表 建立插入刪除
建立乙個工程,在其中新增 list.h 標頭檔案,list.cpp原始檔和main.cpp原始檔。標頭檔案list.h中進行資料型別抽象 adt 宣告了乙個結構體和對應的操作,如下 ifndef list h define list h typedef struct list list 函式宣告 l...
單鏈表的插入刪除
include using namespace std struct lnode void creat link lnode head head指標的引用,lnode head 傳遞的是指標,但是對於指標的原值卻發生了copy,這樣你雖然可以對指標指向的記憶體進行修改但是不能對指標進行修改。因此要傳...