用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)文章
SpringBoot程序打包失敗(.jar中沒(méi)有主清單屬性)
在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運(yùn)行出現(xiàn)了 某某某.jar中沒(méi)有注清單屬性,本文就來(lái)介紹一下原因以及解決方法,感興趣的可以了解一下2023-06-06
spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證詳解
這篇文章主要給大家介紹了關(guān)于spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一學(xué)習(xí)學(xué)習(xí)吧2018-12-12
SpringBoot開(kāi)發(fā)之整合Mybatis詳解
這篇文章主要介紹了SpringBoot開(kāi)發(fā)之整合Mybatis詳解,MyBatis是一個(gè)半自動(dòng)的ORM框架,它允許我們通過(guò)編寫(xiě)SQL語(yǔ)句來(lái)操作數(shù)據(jù)庫(kù),使用MyBatis,我們可以通過(guò)定義映射文件(XML文件)或使用注解的方式將Java對(duì)象與數(shù)據(jù)庫(kù)表進(jìn)行映射,需要的朋友可以參考下2023-09-09
Java接入微信支付超級(jí)詳細(xì)保姆級(jí)教程
這篇文章主要給大家介紹了關(guān)于Java接入微信支付的相關(guān)資料,包括l 準(zhǔn)備開(kāi)發(fā)所需的賬號(hào)和配置信息、準(zhǔn)備環(huán)境、設(shè)置開(kāi)發(fā)參數(shù)以及實(shí)現(xiàn)支付接口,回調(diào)地址的設(shè)置和異步回調(diào)通知的處理也是文章的重點(diǎn)內(nèi)容,需要的朋友可以參考下2024-12-12
spring boot application properties配置實(shí)例代碼詳解
本文通過(guò)代碼給大家介紹了spring boot application properties配置方法,需要的的朋友參考下吧2017-07-07
Java 信號(hào)量Semaphore的實(shí)現(xiàn)
這篇文章主要介紹了Java 信號(hào)量Semaphore的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

