用C和JAVA分別創(chuàng)建鏈表的實例
更新時間:2013年10月30日 09:56:36 作者:
使用用C和JAVA分別創(chuàng)建鏈表的方法,創(chuàng)建鏈表、往鏈表中插入數(shù)據(jù)、刪除數(shù)據(jù)等操作。
創(chuàng)建鏈表、往鏈表中插入數(shù)據(jù)、刪除數(shù)據(jù)等操作,以單鏈表為例。
1.使用C語言創(chuàng)建一個鏈表:
typedef struct nd{
int data;
struct nd* next; } node;
//初始化得到一個鏈表頭節(jié)點
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é)點作為鏈表的尾節(jié)點
p->next=new;//將新的節(jié)點鏈接到鏈表尾部
}
//從鏈表中刪除一個節(jié)點,這里返回值為空,即不返回刪除的節(jié)點
void delete(node* head,int data){
if(head==NULL) return ;
node *p=head;
if(head->data==data){//如何頭節(jié)點為要刪除的節(jié)點
head=head->next;//更新鏈表的頭節(jié)點為頭節(jié)點的下一個節(jié)點
free(p);
return;
}
node *q=head->next;
while(q!=NULL){
if(q->data==data){//找到要刪除的節(jié)點q
node *del=q;
p->next=q->next;
free(del);
}
p=q;//不是要刪除的節(jié)點,則更新p、q,繼續(xù)往后找
q=q->next;
}
}
2.Java創(chuàng)建鏈表
創(chuàng)建一個鏈表
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;
}
}
從單鏈表中刪除一個節(jié)點
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語言創(chuàng)建一個鏈表:
復制代碼 代碼如下:
typedef struct nd{
int data;
struct nd* next; } node;
//初始化得到一個鏈表頭節(jié)點
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é)點作為鏈表的尾節(jié)點
p->next=new;//將新的節(jié)點鏈接到鏈表尾部
}
//從鏈表中刪除一個節(jié)點,這里返回值為空,即不返回刪除的節(jié)點
void delete(node* head,int data){
if(head==NULL) return ;
node *p=head;
if(head->data==data){//如何頭節(jié)點為要刪除的節(jié)點
head=head->next;//更新鏈表的頭節(jié)點為頭節(jié)點的下一個節(jié)點
free(p);
return;
}
node *q=head->next;
while(q!=NULL){
if(q->data==data){//找到要刪除的節(jié)點q
node *del=q;
p->next=q->next;
free(del);
}
p=q;//不是要刪除的節(jié)點,則更新p、q,繼續(xù)往后找
q=q->next;
}
}
2.Java創(chuàng)建鏈表
創(chuàng)建一個鏈表
復制代碼 代碼如下:
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;
}
}
從單鏈表中刪除一個節(jié)點
復制代碼 代碼如下:
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;
}
}
相關文章
Spring動態(tài)多數(shù)據(jù)源配置實例Demo
本篇文章主要介紹了Spring動態(tài)多數(shù)據(jù)源配置實例Demo,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
已有的springcloud+mybatis項目升級為mybatis-plus的方法
這篇文章主要介紹了已有的springcloud+mybatis項目升級為mybatis-plus,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
SpringBoot創(chuàng)建RSocket服務器的全過程記錄
RSocket應用層協(xié)議支持 Reactive Streams語義, 例如:用RSocket作為HTTP的一種替代方案。這篇文章主要給大家介紹了關于SpringBoot創(chuàng)建RSocket服務器的相關資料,需要的朋友可以參考下2021-05-05

