用C和JAVA分別創(chuàng)建鏈表的實(shí)例
更新時(shí)間:2013年10月30日 09:56:36 作者:
使用用C和JAVA分別創(chuàng)建鏈表的方法,創(chuàng)建鏈表、往鏈表中插入數(shù)據(jù)、刪除數(shù)據(jù)等操作。
創(chuàng)建鏈表、往鏈表中插入數(shù)據(jù)、刪除數(shù)據(jù)等操作,以單鏈表為例。
1.使用C語(yǔ)言創(chuàng)建一個(gè)鏈表:
typedef struct nd{
int data;
struct nd* next; } node;
//初始化得到一個(gè)鏈表頭節(jié)點(diǎn)
node* init(void){
node* head=(node*)malloc(sizeof(node));
if(head==NULL) return NULL;
head->next=NULL;
return head;
}
//在鏈表尾部插入數(shù)據(jù)
void insert(node* head,int data){
if(head==NULL) return;
node* p=head;
while(p->next!=NULL)
p=p->next;
node* new=(node*)malloc(sizeof(node));
if(new==NULL) return;
new->data=data;
new->next=NULL;//新節(jié)點(diǎn)作為鏈表的尾節(jié)點(diǎn)
p->next=new;//將新的節(jié)點(diǎn)鏈接到鏈表尾部
}
//從鏈表中刪除一個(gè)節(jié)點(diǎn),這里返回值為空,即不返回刪除的節(jié)點(diǎn)
void delete(node* head,int data){
if(head==NULL) return ;
node *p=head;
if(head->data==data){//如何頭節(jié)點(diǎn)為要?jiǎng)h除的節(jié)點(diǎn)
head=head->next;//更新鏈表的頭節(jié)點(diǎn)為頭節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
free(p);
return;
}
node *q=head->next;
while(q!=NULL){
if(q->data==data){//找到要?jiǎng)h除的節(jié)點(diǎn)q
node *del=q;
p->next=q->next;
free(del);
}
p=q;//不是要?jiǎng)h除的節(jié)點(diǎn),則更新p、q,繼續(xù)往后找
q=q->next;
}
}
2.Java創(chuàng)建鏈表
創(chuàng)建一個(gè)鏈表
class Node {
Node next = null;
int data;
public Node(int d) { data = d; }
void appendToTail(int d) {//添加數(shù)據(jù)到鏈表尾部
Node end = new Node(d);
Node n = this;
while (n.next != null) { n = n.next; }
n.next = end;
}
}
從單鏈表中刪除一個(gè)節(jié)點(diǎn)
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) { return head.next; /* moved head */ }
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; /* head didn't change */
} n = n.next;
}
}
1.使用C語(yǔ)言創(chuàng)建一個(gè)鏈表:
復(fù)制代碼 代碼如下:
typedef struct nd{
int data;
struct nd* next; } node;
//初始化得到一個(gè)鏈表頭節(jié)點(diǎn)
node* init(void){
node* head=(node*)malloc(sizeof(node));
if(head==NULL) return NULL;
head->next=NULL;
return head;
}
//在鏈表尾部插入數(shù)據(jù)
void insert(node* head,int data){
if(head==NULL) return;
node* p=head;
while(p->next!=NULL)
p=p->next;
node* new=(node*)malloc(sizeof(node));
if(new==NULL) return;
new->data=data;
new->next=NULL;//新節(jié)點(diǎn)作為鏈表的尾節(jié)點(diǎn)
p->next=new;//將新的節(jié)點(diǎn)鏈接到鏈表尾部
}
//從鏈表中刪除一個(gè)節(jié)點(diǎn),這里返回值為空,即不返回刪除的節(jié)點(diǎn)
void delete(node* head,int data){
if(head==NULL) return ;
node *p=head;
if(head->data==data){//如何頭節(jié)點(diǎn)為要?jiǎng)h除的節(jié)點(diǎn)
head=head->next;//更新鏈表的頭節(jié)點(diǎn)為頭節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
free(p);
return;
}
node *q=head->next;
while(q!=NULL){
if(q->data==data){//找到要?jiǎng)h除的節(jié)點(diǎn)q
node *del=q;
p->next=q->next;
free(del);
}
p=q;//不是要?jiǎng)h除的節(jié)點(diǎn),則更新p、q,繼續(xù)往后找
q=q->next;
}
}
2.Java創(chuàng)建鏈表
創(chuàng)建一個(gè)鏈表
復(fù)制代碼 代碼如下:
class Node {
Node next = null;
int data;
public Node(int d) { data = d; }
void appendToTail(int d) {//添加數(shù)據(jù)到鏈表尾部
Node end = new Node(d);
Node n = this;
while (n.next != null) { n = n.next; }
n.next = end;
}
}
從單鏈表中刪除一個(gè)節(jié)點(diǎn)
復(fù)制代碼 代碼如下:
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) { return head.next; /* moved head */ }
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; /* head didn't change */
} n = n.next;
}
}
您可能感興趣的文章:
- Java 數(shù)據(jù)結(jié)構(gòu)鏈表操作實(shí)現(xiàn)代碼
- Java單鏈表基本操作的實(shí)現(xiàn)
- JAVA實(shí)現(xiàn)鏈表面試題
- java數(shù)據(jù)結(jié)構(gòu)之實(shí)現(xiàn)雙向鏈表的示例
- java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)單鏈表示例(java單鏈表)
- java使用數(shù)組和鏈表實(shí)現(xiàn)隊(duì)列示例
- java單向鏈表的實(shí)現(xiàn)實(shí)例
- java雙向循環(huán)鏈表的實(shí)現(xiàn)代碼
- JAVA 數(shù)據(jù)結(jié)構(gòu)鏈表操作循環(huán)鏈表
相關(guān)文章
深入解析java HashMap實(shí)現(xiàn)原理
這篇文章主要介紹了深入解析java HashMap實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2015-09-09java教程之對(duì)象序列化使用基礎(chǔ)示例詳解
所謂對(duì)象序列化就是將對(duì)象的狀態(tài)轉(zhuǎn)換成字節(jié)流,以后可以通過(guò)這些值再生成相同狀態(tài)的對(duì)象,下面詳細(xì)介紹一下java對(duì)象的序列化使用方法2014-01-01Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo
本篇文章主要介紹了Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01java運(yùn)行windows的cmd命令簡(jiǎn)單代碼
這篇文章主要介紹了java運(yùn)行windows的cmd命令簡(jiǎn)單代碼,有需要的朋友可以參考一下2013-12-12Java 滑動(dòng)窗口最大值的實(shí)現(xiàn)
這篇文章主要介紹了Java 滑動(dòng)窗口最大值,給定一個(gè)數(shù)組 nums,有一個(gè)大小為 k 的滑動(dòng)窗口從數(shù)組的最左側(cè)移動(dòng)到數(shù)組的最右側(cè)。感興趣的可以了解一下2021-05-05已有的springcloud+mybatis項(xiàng)目升級(jí)為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項(xiàng)目升級(jí)為mybatis-plus,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot創(chuàng)建RSocket服務(wù)器的全過(guò)程記錄
RSocket應(yīng)用層協(xié)議支持 Reactive Streams語(yǔ)義, 例如:用RSocket作為HTTP的一種替代方案。這篇文章主要給大家介紹了關(guān)于SpringBoot創(chuàng)建RSocket服務(wù)器的相關(guān)資料,需要的朋友可以參考下2021-05-05