用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)建一個鏈表:
復(fù)制代碼 代碼如下:
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)建一個鏈表
復(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;
}
}
從單鏈表中刪除一個節(jié)點
復(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;
}
}
相關(guān)文章
SpringBoot程序打包失敗(.jar中沒有主清單屬性)
在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運行出現(xiàn)了 某某某.jar中沒有注清單屬性,本文就來介紹一下原因以及解決方法,感興趣的可以了解一下2023-06-06
spring boot+jwt實現(xiàn)api的token認(rèn)證詳解
這篇文章主要給大家介紹了關(guān)于spring boot+jwt實現(xiàn)api的token認(rèn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一學(xué)習(xí)學(xué)習(xí)吧2018-12-12
spring boot application properties配置實例代碼詳解
本文通過代碼給大家介紹了spring boot application properties配置方法,需要的的朋友參考下吧2017-07-07

